Hunan University Programming Contest freshman season (to reproduce the race) 2020.1.11

A question:

https://ac.nowcoder.com/acm/contest/3674/A

answer:

M and n are given, respectively, corresponding to the m-th Fibonacci number sequence lease term and the n entries, seeking the greatest common divisor of two.
Note: (1 ≤m, n ≤ 231 , GCD (m, n) ≤ 45)
for gcd (m, n) is relatively small, direct conclusions can be set.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#define MAX 105
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int t,a,b,f[50];

int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    f[0]=0,f[1]=1;
    for(int i=2;i<=50;i++){
        f[i]=f[i-1]+f[i-2];
    }
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&a,&b);
        printf("%d\n",f[gcd(a,b)]);
    }
    return 0;
}







Problem B:

https://ac.nowcoder.com/acm/contest/3674/B

answer:

The answer to this question in the B and C actually there are many solutions to, do not confine thought to this question must have a unique solution, we want to seek out this unique solution.
For example: A = a, B = 5a , C = 7a is a kind of solution. Note that when a = 0, is no solution, the other cases are solvable.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#define MAX 105
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
 
ll a,b,c;
 
int main(){
    scanf("%lld",&a);
    if(a==0){//a==1时没有解
        printf("-1");
        return 0;
    }
    a=abs(a);
    printf("%lld %lld",5*a,7*a);
    return 0;
}







Problem C:

https://ac.nowcoder.com/acm/contest/3674/C

answer:

Bfs title search criteria, the search for each point in four directions, to find out the number of coins have the two largest blocks of communication, and the output thereof.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 505
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int n,m;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};//四个方向
char mp[MAX][MAX];//mp表示地图
priority_queue<int> p;

struct node{
    int x,y;
    node(int xx,int yy){
        x=xx;
        y=yy;
    }
};

bool judge(int x,int y){//判断是否越界或者已经走过
    if(x<0||x>n+1||y<0||y>m+1||mp[x][y]=='#'){
        return false;
    }
    return true;
}

int bfs(int i,int j){
    int ans=0;
    queue<node> q;
    node tmp(i,j);
    q.push(tmp);
    while(!q.empty()){
        node d=q.front();
        q.pop();
        int x=d.x,y=d.y;
        if(mp[x][y]=='$') ans++;
        mp[x][y]='#';//标记已经走过
        for(int i=0;i<4;i++){//四个方向
            node next(x+dir[i][0],y+dir[i][1]);
            if(judge(next.x,next.y)){
                q.push(next);
            }
        }
    }
    return ans;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",mp[i]+1);
    }
    for(int j=0;j<=m+1;j++){//标记周围是可走的
        mp[0][j]=mp[n+1][j]='.';
    }
    for(int i=0;i<=n+1;i++){//标记周围是可走的
        mp[i][0]=mp[i][m+1]='.';
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(mp[i][j]!='#'){
                int tmp=bfs(i,j);//求一个连通块的金币数
                p.push(tmp);
            }
        }
    }

    int sum=0;
    sum+=p.top();
    p.pop();
    if(p.size()!=0) sum+=p.top();
    printf("%d",sum);//输出最大金币数量的两块的总金币数
    return 0;
}







F topic:

https://ac.nowcoder.com/acm/contest/3674/F

answer:

Some balloons are given x, y coordinates, determines whether they are in a straight line.
Note: The balloon is small, a lot of balloons might have the same x, y coordinates.
the case where n = 1 or 2, certainly Yes, n> 2, we can first calculate the slope of the first two balloons connection k, k = -1 DESCRIPTION slope does not exist, and a recording position of the balloon Dir .
After each read position a balloon, it will calculate the slope of the first balloon connection k1, if k1 == k, described in a straight line.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 505
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int n,x,y;

int main(){
    scanf("%d",&n);
    if(n==1||n==2){
        printf("Yes");
        return 0;
    }
    int x1,y1;
    double k;
    bool flag=true,tag=false;
    for(int i=0;i<n;i++){
        scanf("%d%d",&x,&y);
        if(i==0){x1=x,y1=y;continue;}
        if((x!=x1||y!=y1)&&tag==false){
            if(x==x1){k=-1;}//说明斜率不存在
            else k=(y-y1)/(x-x1);
            tag=true;
            continue;
        }
        if(x!=x1||y!=y1){
            if(x==x1){
                if(k!=-1){flag=false;break;}
            }else{
                if((y-y1)/(x-x1)!=k){flag=false;break;}
            }
        }
    }
    if(flag) printf("Yes");
    else printf("No");
    return 0;
}







G title:

https://ac.nowcoder.com/acm/contest/3674/G

answer:

Do you have some money n, there are two options, you can buy 10 yuan, or 3 key 3 yuan a key, the key need to buy at least in the case of all spend their own money.
It is not difficult to think of greedy principle, as much as 10 yuan to buy three keys.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 505
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int n;

int main(){
    scanf("%d",&n);
    int num1=n/10;//num1是买10元的次数,num1越大越好
    int res=n-num1*10;//余下的钱
    if(res%3==0){
        int ans=num1*3+res/3;
        printf("%d",ans);
        return 0;
    }
    bool flag=false;
    while(res%3!=0&&num1!=0){//若余下的钱不是3的倍数,就尝试少买一次10元的
        num1--;
        res=n-num1*10;
        if(res%3==0){
            flag=true;
            break;
        }
    }
    if(flag){//若在尝试中满足是3的倍数
        int ans=num1*3+res/3;
        printf("%d",ans);
    }else{
        printf("orz");
    }
    return 0;
}







H questions:

https://ac.nowcoder.com/acm/contest/3674/H

answer:

* Calculation is 0.5 + 0.5 + 0.5 + 0.5 ... n-

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 505
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;


int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        double ans=0.5,res=0.0;
        for(int i=1;i<=n;i++){
            res+=pow(0.5,i);
        }
        printf("%.4lf\n",res);
    }
    return 0;
}







I 题:

https://ac.nowcoder.com/acm/contest/3674/I

answer:

In fact, the problem row is all wrong, do not control the input array A [] is. Pay attention to use long long, an int is too can not.
All wrong problem link row: all wrong row problem

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 100005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

ll n,d[MAX],a[MAX],mod=1e9+7;

int main(){
    d[1]=0,d[2]=1;
    for(int i=3;i<=100005;i++){
        d[i]=((i-1)*((d[i-1]+d[i-2])%mod))%mod;
    }
    scanf("%lld",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i]);
    }
    ll tot=1;
    for(int i=1;i<=n;i++){
        tot=(tot*i)%mod;
    }
    if(tot<=d[n]){
        printf("%lld",tot+mod-d[n]);
    }else{
        printf("%lld",(tot-d[n])%mod);
    }

    return 0;
}







J title:

https://ac.nowcoder.com/acm/contest/3674/J

answer:

game theory. Rules are as follows:
there n heap of candy, the number of each pile of candy is ai, the last time who did not take after the rest of the candy, then that person wins.
(1) Dada can only be removed from a stack of an even number of candy, the candy and when the number of all the stack is less than 2, Data can not take the candy, which is equivalent to a space over the round
(2) Tutu only from a Remove the heap Confectionery odd
(. 3) to take Dada
when n = 1, if the number is even the candy, the Dada can take once finished, if it is odd, the last person must be taken Tutu
when n> 1, the Data certainly can not take all the candy at once, so long as there is a pile of candy Tutu is an odd number, it has not taken this pile, until finally the pile is sure to take the winning
sum up:
only when n = 1 and the number of candy is even, Data wins, otherwise Tutu wins.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 50005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

ll a[MAX];

int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i]);
    }
    if(n==1&&a[0]%2==0){
        printf("DaDa");
    }else{
        printf("TuTu");
    }
    return 0;
}







L title:

https://ac.nowcoder.com/acm/contest/3674/L

answer:

The other is the number of prime numbers within a range of requirements, but also pay attention to this question 1 as a prime number.

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 100005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

int l,r;
bool isprime[MAX];

void eratos(){//线性筛素数
    for(int i=0;i<=MAX;i++){
        isprime[i]=true;
    }
    isprime[0]=false,isprime[1]=true;
    for(int i=2;i*i<=MAX;i++){//留下i,删除i的倍数
      if(isprime[i]){
          int j=i+i;
          while(j<=MAX){
              isprime[j]=false;
              j=j+i;
          }
      }
    }
}

int main(){
    eratos();//线性筛素数
    scanf("%d%d",&l,&r);
    int cnt=0;
    for(int i=l;i<=r;i++){
        if(isprime[i]) cnt++;
    }
    printf("%d",cnt);
    return 0;
}







M title:

https://ac.nowcoder.com/acm/contest/3674/M

answer:

And seeking the exclusive OR section.
From 1 to n, XOR and is regular:
n = 1 2. 3. 4. 5. 6. 7. 8. 9 10
The following is the xor and:
13041708111
rule is:
when n% 4 when == 0, and n-=;
when n% 4 == 1, and a = 1;
when n% 4 == 2, and = n + 1;
when n% 4 == 3, and = 0;
as we know, ask [l, r] of the exclusive oR may be converted and for the sake of [1, l-1] and [1, r] of the exclusive oR and

code show as below:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<map>
#define MAX 505
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

ll f(ll n){
    if(n%4==0){
        return n;
    }else if(n%4==1){
        return 1;
    }else if(n%4==2){
        return n+1;
    }else if(n%4==3){
        return 0;
    }
}

int main(){
    ll l,r;
    cin>>l>>r;
    cout<<(f(l-1)^f(r))<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zzh1582188532/p/12207880.html