4.12总结
第一题:T1
题目简述
将一个十进制数转化为二进制后,如果N,问:1 ~ N中有多少个
题目分析
每次枚举一个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
有两种操作:
- 花费
1 次,将N按位或上一个数字 - 花费
1 次,将N按位与上一个数字 帮助:什么是按位与和按位或题目分析
经过精密的推算得知: 一个数,最少0次,最多两次可以用按位与和按位或变成另一个想要的数字
- 当
N == M时,不需要任何操作,即为0次 - 当
N & M == M或者N | M == M的时候,只需要一次操作 - 其他情况,最多两次,因为可以先按位与上一个
0然后在按位或上M一定可行 最后写出代码即可 :::success[正确代码]{close}#include <bits/stdc++.h> using namespace std; int main() { long long t, n, m; cin >> t; while (t --) { cin >> n >> m; if (n == m) { cout << 0 << endl; } else if ((n & m) == m or (n | m) == m) { cout << 1 << endl; } else { cout << 2 << endl; } } }:::: 错误原因:没有想到思路
第三题:T3
题目简述
求
1~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; }然后在用从1~N枚举 如果当前数是个质数,那么ans ++ 最后输出 ::::success[正确代码]{close}
#include <bits/stdc++.h> using namespace std;
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
题目简述
有
题目分析
首先用桶统计每个数字有多少,即如果
1, 2, 1, 3, 4, 7, 5, 6, 5
那么桶数组就是
t[1] = 2, t[2] = 1, t[3] = 1, t[4] = 1, t[5] = 2 , ...
然后求出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
题目简述
给定
题目分析
只有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;
}
}
::::