题解1

· · 个人记录

T750563 T1

题目描述

若将一个正整数化为二进制数,在此二进制数中,我们将数字 1 的个数多于数字 0 的个数的这类二进制数称为 A 类数,否则就称其为 B 类数。

题意分析

分析一个数转换为二进制后是A (1多于0)还是B 类数(0多于1)

是啊,做什么

求一个数化为二进制数后1和0的数量

思路

while(x!=0){
            if((x&1)==1){
                sum1++;
            }
            else{
                sum0++;
            }
            x>>=1;
        }

使用位运算中的&找数的1或0,用>>一位一位删

code

#include<bits/stdc++.h>
using namespace std;
//t1
int n,a,b;
int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        int x=i,sum1=0,sum0=0;
        while(x!=0){
            if((x&1)==1){
                sum1++;
            }
            else{
                sum0++;
            }
            x>>=1;
        }
        if(sum1>sum0){
            a++;
        }
        else{
            b++;
        }
    }
    cout<<a<<" "<<b;
    return 0;
} 

错因

/

T750565 T2

题目描述

现在有正整数 n,有以下操作;

两种操作均可使用多次(或不用),请求出将 n 变为 m 最小的代价。

帮助:什么是按位与和按位或

题意分析

使一个数n转变为m

思路

找一个数经过&还是|后能达成目标(1),否则2

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,n,m;
signed main(){
    cin>>t;
    while(t--){
        cin>>n>>m;
        if(n==m){
            cout<<0<<endl;
        }
        else if((n|m)==m||(n&m)==m){
            cout<<1<<endl;
        }
        else cout<<2<<endl;
    }
    return 0;
} 

没做出来

T750566 T3

题目描述

求 1,2,\cdots,N 中素数的个数。

思路&题意

找质数,注意时间,用埃筛秒了

c!o!d!e!

#include<bits/stdc++.h>
using namespace std;
//t3
#define int long long
int n,sum;
bool a[100000005];
void su(int x){
    for(int i=2; i*i<=x; i++){
        if(a[i]==0){
            for(int j=i*i;j<=x; j+=i){
                a[j]=1;
            }
        }
    }
    return ;
}
signed main(){
    cin>>n;
    su(n+1);
    a[1]=1;a[0]=1;
    for(int i=2; i<=n; i++){
        if(a[i]==0){
            sum++;
        }
    }
    cout<<sum;
    return 0;
} 

错因

忘开long long了
十年oi一场空,___

T750574 T4

题目描述

小田爱上了质数。

给定一个正整数 n,如果有两个正整数 x 和 y 满足以下条件,则认为两个数字是一个质数对。

请输出所有满足条件的质数对,按第一个数字从小到大输出。

题意

找“质数对”

思路

埃筛+if判断

if(a[j]==0&&a[i]==0&&i<=j&&i+j==n)

code

#include<bits/stdc++.h>
//t4
using namespace std;
#define int long long
int n;
bool a[10000005];
void su(int x){
    for(int i=2; i*i<=x; i++){
        if(a[i]==0){
            for(int j=i*i;j<=x; j+=i){
                a[j]=1;
            }
        }
    }
    return ;
}
signed main(){
    cin>>n;
    su(n+1);
    a[1]=1;a[0]=1;
    int f=0;
    for(int i=2; i<=n; i++){
        int j=n-i;
            if(a[j]==0&&a[i]==0&&i<=j&&i+j==n){
                cout<<i<<" "<<j;
                f++;
                cout<<endl;
            }

    }
    if(f==0)cout<<"-1 -1";
    return 0;
} 

错因

没想出来

T750567 T5

题目描述

今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏.

贝茜让N头奶牛(编号1到N,1≤N≤100,000)坐成一个圈。

除了1号与N号奶牛外,i号奶牛与i-1号和i+1号奶牛相邻,N号奶牛与1号奶牛相邻。

农夫约翰用很多纸条装满了一个桶,每一张纸条中包含一个1到1000000之间的数字。

接着每一头奶牛i从桶中取出一张纸条,纸条上的数字用Ai表示。

所有奶牛都选取完毕后,每头奶牛轮流走上一圈,当走到一头奶牛身旁时,如果自己手中的数字能够被该奶牛手中的数字整除,则拍打该牛的头。

牛们希望你帮助他们确定,每一头奶牛需要拍打的牛的数量。

又臭又长

题意

共有N个整数A1,A2,...,AN,对于每一个数Ai,求其他的数中有多少个是它的约数

思路

暴力显然不行

使用桶记录奶牛的编号,然后将编号的倍数的奶牛计数

code

#include<bits/stdc++.h>
using namespace std;
#define int long long  
int n,a[1000005],c[1000005],maxx=INT_MIN;
int  b[1000050];
signed main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>a[i];
        b[a[i]]++;
        maxx=max(a[i],maxx);
    }
    for(int i=1; i<=maxx; i++){
        if(b[i]!=0){
            for(int j=1;i*j<=maxx;j++){
                c[i*j]+=b[i];
            }
        }
    }
    for(int i=1; i<=n; i++){
        cout<<c[a[i]]-1<<endl;
    }
    return 0;
} 

错因

没想出来

T750568 T6

题目描述

小卡有 T(1\le T\le 10^5) 组询问。每次询问给你两个正整数 x,y(1\le x,y\le 10^9)。

小卡想知道,第 x 个质数和第 y 个质数是否满足 p_x \oplus p_y =1,即第 x 个质数和第 y 个质数的异或值是否是1。

题意

第 x 个质数和第 y 个质数的异或值是否是1

思路

核心

x==1&&y==2 || x==2&&y==1

实际上是x和y之间的问题,找质数必爆

code

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
 ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 int t;
 cin>>t;
 while(t--)
 {
 int x,y;
 cin>>x>>y;
 if(x==1&&y==2 || x==2&&y==1)
 {
 cout<<"Yes"<<endl;
 } 
 else
 {
 cout<<"No"<<endl;
 }
 } 
 return 0;
}

错因

直接找质数了好似