2026.4.12

· · 个人记录

T750563

题意

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

目的

求出 1~n 之中(1 \le n \le 1000)A、B 两类数的个数。

思路

通过按位与(&)和循环求出各个二进制位上的数,统计出来后判断并统计 A、B 类的个数。

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, a = 0, b = 0;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int t = i, cnt1 = 0, cnt2 = 0;
        do
        {
            if(t & 1)
            {
                cnt1++;
            }
            else
            {
                cnt2++;
            }
            t >>= 1;
        }
        while(t);
        if(cnt1 > cnt2)
        {
            a++;
        }
        else
        {
            b++;
        }
    }
    cout << a << ' ' << b;
    return 0;
}

T750565

题意

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

目的

求将 n 变为 m 最小的代价。

思路

## 代码 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while(T--) { int x, y; cin >> x >> y; if(x == y) { cout << 0; } else if(x < y) { if((x | y) == y) { cout << 1; } else { cout << 2; } } else { cout << 1; } cout << endl; } return 0; } ``` ## $AC$ 代码 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while(T--) { long long x, y; cin >> x >> y; if(x == y) { cout << 0; } else if((x & y) == y || (x | y) == y) { cout << 1; } else { cout << 2; } cout << endl; } return 0; } ``` ## 错因 未开 long long 且对 $n \; \& \; m$ 的情况考虑不当。 # T750566 ## 题意(目的) 求 $[1,N]$ 中素数的个数。 ## 思路 利用质数筛求出 $[1,N]$ 中所有的素数,并利用循环统计素数的个数。 ## 代码 ```cpp #include <bits/stdc++.h> using namespace std; bool a[100000005]; void f(int n) { for(int i = 2; i * i <= n; i++) { if(!a[i]) { for(int j = i * i; j <= n; j += i) { a[j] = 1; } } } return ; } int main() { int n, ans = 0; cin >> n; f(n); for(int i = 2; i <= n; i++) { ans += !a[i]; } cout << ans; return 0; } ``` # T750574 ## 题意 给定一个正整数 $n$,如果有两个正整数 $x$ 和 $y$ 满足以下条件,则认为两个数字是一个质数对: - $1 \le x \le y \le n

目的

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

思路

利用质数筛求出 [1,n] 中的所有质数,随后通过循环来判断 i 和 n-i 是否都为质数,随后输出这个质数对。重复此操作直到循环结束。

代码

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

T750567

题意

N头奶牛坐成一个圈。有个用很多纸条装满的一个桶,每一张纸条中包含一个1到1000000之间的数字。接着每一头奶牛 i 从桶中取出一张纸条,纸条上的数字用 A_i 表示。所有奶牛都选取完毕后,每头奶牛轮流走上一圈,当走到一头奶牛身旁时,如果自己手中的数字是该奶牛手中数字的倍数,则拍打该牛的头。

目的

输出每一头奶牛需要拍打的牛的数量。

思路

利用桶将所有编号的奶牛数量存储起来,随后进行循环,在每一次循环中,将某一个编号的所有倍数所需要拍的奶牛数量加上拥有这个编号的奶牛数量,最后输出每一个奶牛所需要拍的奶牛的数量。

代码

#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = i + 1; j <= n; j++)
        {
            if(!(a[i] % a[j]))
            {
                b[i]++;
            }
            if(!(a[j] % a[i]))
            {
                b[j]++;
            }
        }
        cout << b[i] << endl;
    }
    return 0;
}

AC 代码

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

错因

因为使用暴力枚举而超时。

T750568

题意

有 T 组询问。每次询问给你两个正整数 x,y。

目的

判断第 x 个质数和第 y 个质数的异或值是否是1,是则输出,否则输出。

思路

想要两个质数的异或值为 1,只能为 2 和 3,所以只要判断 x 是否为 1,且 y 是否为 2 或者 x 是否为 2,且 y 是否为 1 即可。

代码

#include <bits/stdc++.h>
using namespace std;
bool a[10000000005];
int b[1000000005];
void f(int n)
{
    int idex = 1;
    for(int i = 2; idex <= n; i++)
    {
        if(!a[i])
        {
            b[idex++] = i;
            for(int j = i * i; j <= n; j += i)
            {
                a[j] = 1;
            }
        }
    }
    return ;
}
int main()
{
    int n;
    cin >> n;
    f(n);
    while(n--)
    {
        int x, y;
        cin >> x >> y;
        if((b[x] ^ b[y]) == 1)
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    return 0;
}

AC 代码

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

错因

思路过于复杂。

T750569

题意

题目规定,所有大于等于 a 的完全平方数都是超级幸运数,所有超级幸运数的倍数都是幸运数。对于一个非幸运数,可以将它一直 +1,直到它变成一个幸运数。我们把这个过程叫做幸运化。

目的

给出 N 个数,首先判断它们是不是幸运数;接着,对于非幸运数,再将它们幸运化并输出。

思路

从 \lceil \sqrt a \rceil 开始循环,将 a^2 和 i \cdot a^2 标记为幸运数,并重复此操作直到循环结束。随后读入 N 个数,并依次根据标记来判断他们是否为幸运数,如果不是,利用二分查找寻找离他最近的大于等于它的幸运数。

代码

#include <bits/stdc++.h>
using namespace std;
bool a[10000005];
void f(int x)
{
    const int n = 10000000;
    for(int i = 1; i * i <= n; i++)
    {
        if(i * i >= x)
        {
            for(int j = i * i; j <= n; j += i * i)
            {
                a[j] = 1;
            }
        }
    }
    return ;
}
int main()
{
    int n, x;
    cin >> x >> n;
    f(x);
    while(n--)
    {
        int t;
        cin >> t;
        if(a[t])
        {
            cout << "lucky" << endl;
        }
        else
        {
            for(; !a[t]; t++);
            cout << t << endl;
        }
    }
    return 0;
}

AC 代码

#include <bits/stdc++.h>
using namespace std;
bool a[10000005];
int b[10000005];
void f(int x)
{
    const int n = 10000000;
    for(int i = ceil(sqrt(x)); i * i <= n; i++)
    {
        for(int j = 1; j * i * i <= n; j++)
        {
            a[j * i * i] = 1;
        }
    }
    return ;
}
int main()
{
    int n, x, idex = 1;
    cin >> x >> n;
    f(x);
    for(int i = 1; i <= 10000000; i++)
    {
        if(a[i])
        {
            b[idex++] = i;
        }
    } 
    while(n--)
    {
        int t;
        cin >> t;
        if(a[t])
        {
            cout << "lucky" << endl;
        }
        else
        {
            int l = 0, r = idex, mid = (r + l) / 2;
            while(l != r - 1)
            {
                if(b[mid] < t)
                {
                    l = mid;
                }
                else if(b[mid] > t)
                {
                    r = mid;
                }
                mid = (r + l) / 2;
            }
            cout << b[r] << endl;
        }
    }
    return 0;
}

错因

未使用二分查找导致超时。