T1
未对
思路:
题目说会有,一个n的数字,q次询问,每次会给出运算的符号和数字,和n去做运算,要注意的是每次要输出并把数字存起来,再和下一个运算。
错误代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,q;
cin >> n >> q;
for(int i = 1 ; i <= q ; i++){
string c;
int a;
cin >> c >> a;
if(c == "&"){
n = n&=a;
cout << n;
}
if(c == "|"){
n = n|=a;
cout << n;
}
if(c == "^"){
n = n^=a;
cout << n;
}
if(c == "<<"){
n = (n<<=a);
cout << n;
}
if(c == ">>"){
n = (n>>=a);
cout << n;
}
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,q;
cin >> n >> q;
for(int i = 1 ; i <= q ; i++){
string c;
int a;
cin >> c >> a;
if(c == "&"){
n = n&=a;
}
if(c == "|"){
n = n|=a;
}
if(c == "^"){
n = n^=a;
}
if(c == "<<"){
n = (n<<=a);
}
if(c == ">>"){
n = (n>>=a);
}
cout << n << "\n";
}
return 0;
}
错因:
输出的时候没有加空格
T2
已对
思路:
题目说要看是否正确,用判断奇偶的方法,要看这几个数在二进制的进制下有多少个一,这些一加起来判断奇偶,我们有两种解决方法:第一种先转换再去统计,第二种是在转换中就统计,意思是每次转换不是模2吗,余下0或1,在这个阶段统计即可。
正确代码:
#include<bits/stdc++.h>
using namespace std;
int f(int x){
int ans = 0;
while(x){
if(x % 2 == 1){
ans++;
}
x/=2;
}
return ans;
}
int main(){
int n,sum = 0;
cin >> n;
while(n--){
int a;
cin >> a;
sum += f(a);
}
if(sum % 2 == 1){
cout << sum << " " << 1;
}else{
cout << sum << " " << 0;
}
return 0;
}
T3
未对
思路:
题目要求1到n之间的素数。这道题的难点在于内存,需要两个1e8的循环,而int的内存是4,乘起来内存会超,这下就可以用bool类型,因为埃筛的tong里只有0和1代表素数和不是素数,bool的内存是2,内存不会超。
错误代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e8;
int tong[N];
void f(){
tong[0] = tong[1] = 1;
for(int i = 2 ; i <= 1e8 ; i++){
if(tong[i] == 0){
for(int j = 2 ; j*i <= 1e8 ; j++){
tong[i*j] = 1;
}
}
}
}
int main(){
f();
int n,sum = 0;
cin >> n;
for(int i = 1 ; i <= n ; i++){
if(tong[i] == 0){
sum++;
}
}
cout << sum;
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e8;
bool tong[N];
void f(){
tong[0] = tong[1] = 1;
for(int i = 2 ; i <= 1e8 ; i++){
if(tong[i] == 0){
for(int j = 2 ; j*i <= 1e8 ; j++){
tong[i*j] = 1;
}
}
}
}
int main(){
f();
int n,sum = 0;
cin >> n;
for(int i = 1 ; i <= n ; i++){
if(tong[i] == 0){
sum++;
}
}
cout << sum;
return 0;
}
错因:
没有透彻理解埃筛的性质
T4
已对
思路:
题目说给定n,1到n有几个符合要求的素数对,我们可以直接枚举,但会超时,因为两层循环,有两个1e7,加起来肯定会超时
二十分代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
int tong[N];
void f(){
tong[0] = tong[1] = 1;
for(int i = 2 ; i*i <= 1e7 ; i++){
if(tong[i] == 0){
for(int j = 2 ; j*i <= 1e7 ; j++){
tong[j*i] = 1;
}
}
}
}
int main(){
f();
int n,k = 0;
cin >> n;
for(int i = 2 ; i <= n ; i++){
for(int j = i ; j <= n ; j++){
if(tong[i] == 0 && tong[j] == 0 && i + j == n){
cout << i << " " << j << "\n";
k = 1;
}
}
}
if(k == 0){
cout << -1 << " " << -1;
}
return 0;
}整体结果不变,我们可以来再改量一下,我们二十分代码用了两层循环,我们要省去一层循环,那我们可以去计算,比如说计算j,由于条件说x+y=n,我们用倒推法,推出y=x-n,y其实就是我说的j,最后记得要写i<=j,不然会出现重复的
正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7;
int tong[N];
void f(){
tong[0] = tong[1] = 1;
for(int i = 2 ; i <= 1e7 ; i++){
if(tong[i] == 0){
for(int j = 2 ; j*i <= 1e7 ; j++){
tong[i*j] = 1;
}
}
}
}
int main(){
f();
int n,a = 1;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int j = n-i;
if(tong[i] == 0 && tong[j] == 0 && i <= j){
cout << i << " " << j << "\n";
a = 0;
}
}
if(a == 1){
cout << -1 << " " << -1;
}
return 0;
}
T5
未做
思路:
题目给出x和y,问第x个质数和第y个质数的异或值是否为1,两个质数异或值为1一定是二进制下只有末尾不同的数,那我们可以来枚举一下
2 10
3 11
13 1101
17 0001 0001
19 0001 0011 …
我们可以发现,只有二的后面是0,因为质数只有二是偶数,所以成功必须得是一奇一偶
测试:
可以看到只有(2^3)的结果为一,那我们就确定了,就是a = 1,b = 2或a = 2,b = 1的时候才行,因为题目没有说一定是先输入a,再输入b
正确代码:
#include<bits/stdc++.h>
using namespace std;
signed main(){
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int x,y;
cin >> x >> y;
if(x == 1 && y == 2 || x == 2 && y == 1){
cout << "Yes" << "\n";
}else{
cout << "No" << "\n";
}
}
return 0;
}
T6
已对
思路:
题目说达成条件的数为2017数,给出q次循环,而且求的是区间,数据是1e5,两个1e5会超时,当然要用到二分。我们可以在埃筛的函数里再开一个循环,1e5枚举一下每个数是否达成条件,再调用即可
正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
int tong[N],s[N];
void f(){
tong[0] = tong[1] = 1;
for(int i = 2 ; i <= 1e5 ; i++){
if(tong[i] == 0){
for(int j = 2 ; j*i <= 1e5 ; j++){
tong[i*j] = 1;
}
}
}
for(int i = 1 ; i <= 1e5 ; i++){
s[i] = s[i-1];
if(tong[i] == 0 && tong[(i+1)/2] == 0){
s[i]++;
}
}
}
int main(){
f();
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int l,r;
cin >> l >> r;
cout << s[r] - s[l-1] << "\n";
}
return 0;
}
T7
未做
思路:
看到题目第一时间肯定是想枚举,理解了之后可以做出这样的代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ans[N],a[N];
int main(){
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
for(int i = 1 ; i <= n ; i++){
int cnt = 0;
for(int j = 1 ; j <= n ; j++){
if(a[i]%a[j] == 0){
ans[i]++;
}
}
}
for(int i = 1 ; i <= n ; i++){
cout << ans[i]-1 << "\n";
}
return 0;
}而我们这次考试内容是“位运算”“埃筛”,那位运算肯定用不上,那么埃筛的本质是什么?找到一个质数,把这个质数的因子全部筛掉,那么这道题的题目是什么?是即共有N个整数A1,A2,…,AN,对于每一个数Ai,求其他的数中有多少个是它的约数,那这两能串起来吧
那我们加上埃筛的思路来试一试:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ans[N],a[N];
int main(){
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
for(int i = 1 ; i <= n ; i++){
int num = a[i];
for(int j = 1 ; j*num <= 1e6 ; j++){
ans[num*j]++;
}
}
for(int i = 1 ; i <= n ; i++){
cout << ans[a[i]]-1 << "\n";
}
return 0;
}还是不对,那怎么办,还是回到一开始的埃筛本质,既然直接埃筛没过,那就要想想个数的问题了,我们可以加上tong来进行判断,这样有些数就不用每次循环了,加的时候要加上tong,因为tong是代表个数来的
正确代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N],ans[N],tong[N];
int main(){
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
tong[a[i]]++;
}
for(int i = 1 ; i <= 1e6 ; i++){
int num = i;
if(tong[num] != 0){
for(int j = 1 ; j*num <= 1e6 ; j++){
ans[j*num]+= tong[num];
}
}
}
for(int i = 1 ; i <= n ; i++){
int k = a[i];
cout << ans[k]-1 << "\n";
}
return 0;
}