2026.4.12
T750563
题意
若将一个正整数的二进制数中,我们将数字 1 的个数多于数字 0 的个数的这类二进制数称为 A 类数,否则就称其为 B 类数。
目的
求出 1~
思路
通过按位与(&)和循环求出各个二进制位上的数,统计出来后判断并统计 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
题意
现在有正整数
- 「ACCEPT」:花费 1 的代价,将
n 二进制按位或上 一个正整数。 - 「BOTH」:花费 1 的代价,将
n 二进制按位与上一个正整数。 两种操作均可使用多次(或不用)。
目的
求将
思路
-
x+y=n -
目的
按第一个数字从小到大输出所有满足条件的质数对。
思路
利用质数筛求出
代码
#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之间的数字。接着每一头奶牛
目的
输出每一头奶牛需要拍打的牛的数量。
思路
利用桶将所有编号的奶牛数量存储起来,随后进行循环,在每一次循环中,将某一个编号的所有倍数所需要拍的奶牛数量加上拥有这个编号的奶牛数量,最后输出每一个奶牛所需要拍的奶牛的数量。
代码
#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
题意
有
目的
判断第
思路
想要两个质数的异或值为 1,只能为 2 和 3,所以只要判断
代码
#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
题意
题目规定,所有大于等于
目的
给出
思路
从
代码
#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;
}
错因
未使用二分查找导致超时。