4.12总结

· · 个人记录

第一题:T1

题目简述

将一个十进制数转化为二进制后,如果1的个数多于0的个数时,则称其为A类数 相反,如果0的个数多于1的个数或等于1的个数时,则称其为B类数 给定N,问:1 ~ N中有多少个A类数和多少个B类数

题目分析

每次枚举一个1~N中的数字,用二进制数位分离也就是

while(x)
{
    //此时x&1就是x的最后一位
    x >>= 1;
}

然后再统计即可完成

:::success[正确代码]{close}

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int a = 0, b = 0;
    for(int i = 1; i <= n; i ++){
        int n = i;
        int o = 0, z = 0;
        while (n) {
            if ((n & 1) == 1) o ++; //统计1的出现次数
            if ((n & 1) == 0) z ++; //统计0的出现次数
            n >>= 1;
        }
        if(o > z) a ++; //1 > 0则判为a类
        else b ++; //否则判b类
    }
    cout << a << ' ' << b;
}

::::

第二题:T2

题目简述

现在有一个整数N,问在最少多少次内将N变为M 有两种操作:

const int N = 1e8 + 10; bool a[N];

void f(int n) { a[0] = a[1] = 1; for (int i = 2; i i <= n; i ++) { if (a[i] == 0) { for (int j = i i; j <= n; j += i) { a[j] = 1; } } } return; }

int main() { int n; cin >> n; int ans = 0; f(n + 5); for (int i = 1; i <= n; i ++) { if(a[i] == 0) ans ++; } cout << ans;

}

::::

# 第四题:[T4](https://www.luogu.com.cn/problem/T750574?contestId=320393)
## 题目简述
我们规定,当有一对满足以下条件的称之为***质数对***
1. $1 \leq x \leq y\leq n$
2. $x + y == n$
3. x是质数,y也是质数
给定n,请找出所有满足条件的质数对
## 题目分析
可以用两根指针l和r找
l和r用while靠近,直到`l > r`为止
每次靠近都要使得l + r == n,也就是一开始的初始值是
`l = 1, r = n - 1`,然后每次循环都`l ++, r --`
这样就可以了,最后只要在循环里面判断他们是不是质数就可以了
最后如果没找到就输出-1 -1

::::success[正确代码]{close}
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 20;
bool a[N];

void f(int n) {
    a[0] = a[1] = 1;
    for (int i = 2; i * i <= n; i ++) {
        if (a[i] == 0) {
            for (int j = i * i; j <= n; j += i) {
                a[j] = 1;
            }
        }
    }
    return;
}

int main() {
    int n;
    cin >> n;
    f(n + 5);
    int x = 1, y = n - 1;
    int flag = 0;
    while (x <= y) {
        if (!a[x] && !a[y]) {
            cout << x << ' ' << y << endl;
            flag = 1;
        }
        x ++, y --; //指针不停靠近
    }
    if(!flag) cout << -1 << ' ' << -1 << endl;
    return 0;
}

::::

第五题:T5

题目简述

有N个整数A_1,A_2, ... ,A_N ,对于每一个整数A_i,求其他的数中有多少个是它的约数

题目分析

首先用桶统计每个数字有多少,即如果A是

1, 2, 1, 3, 4, 7, 5, 6, 5

那么桶数组就是

t[1] = 2, t[2] = 1, t[3] = 1, t[4] = 1, t[5] = 2 , ...

然后求出A中元素最大值 接下来就是遍历从1到最大值,然后将ans[i * j] += t[i] (这里i是从1遍历到maxx,j是从1遍历到i * j <= maxx) 最后按照原顺序输出ans[a[i]] - 1即可

::::success[正确代码]{close}

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, M = 1e7 + 10;
int a[N], ans[M], t[M];
int main()
{
    int n;
    cin >> n;
    int maxx = INT_MIN;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        maxx = max(maxx, a[i]);
        t[a[i]] ++;
    }

    for(int i = 1; i <= maxx; i ++)
    {
        if(t[i] == 0) continue;
        for(int j = 1; i * j <= maxx;j ++)
        {
            ans[i * j] += t[i];
        }
    }   
    for(int i = 1; i <= n; i ++)
    {
        cout << ans[a[i]] - 1 << endl;
    }
    return 0;
}

::::

第六题:T6

题目简述

给定x和y,问第x个质数和第y个质数是否满足P_x \oplus P_y = 1 即第 x 个质数和第 y 个质数的异或值是否是1

题目分析

只有2 和 3的异或值为1 所以只要判断xy是否各为2、3 或 3、2即可

::::success[正确代码]{close}

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int x, y;
        cin >> x >> y;
        if(x == 2 && y == 1 or x == 1 and y == 2) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

::::