跳到正文

个人记录

2026/11/16总结

lipinfeng约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年11月19日
收录
2026年11月21日
更新
2026年11月21日
篇幅
约 4 分钟

版本

  1. v1当前

题目详细

T850020

题意 求 1,2,⋯,N 中素数的个数

解题思路 先用埃氏筛求出1到1e8+5之间的素数,再用for一个一个加,tong要用bool。

原来的错误代码 #include<bits/stdc++.h> #define int long long #define endl ‘\n’ int tong[1000005], s[100005]; void f(int n) { tong[0]=tong[1]=1; for(int i=1;i<=n;i++) { if(tong[i]==0) { for(int bs=2;bsi<=n;bs++) { tong[bsi]=1; } } } } using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,sum=0; cin >> n; f(n); for(int i=1;i<=n;i++) { sum+=tong[i]^1; } cout<<sum; return 0; }

正确代码 #include<bits/stdc++.h> #define int long long #define endl ‘\n’ bool tong[100000005]; void f(int n) { tong[0]=tong[1]=1; for(int i=1;i<=n;i++) { if(tong[i]==0) { for(int bs=2;bsi<=n;bs++) { tong[bsi]=1; } } } } using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,sum=0; cin >> n; f(n); for(int i=1;i<=n;i++) { sum+=tong[i]^1; } cout<<sum; return 0; }

错因 用了int没有用bool。

T850021

题意 求出第x个质数^第y个质数的值是否是1。

解题思路 因为异或是相同为0,不同为1,所以两个数都不能超过2,如果都是2,结果为0,都是1,结果为0,所以只能为2 1或1 2。

原来的错误代码 #include<bits/stdc++.h> #define endl ‘\n’ using namespace std; int tong[1000000005], s[1000000005]; void f() { tong[0]=tong[1]=1; int x=1; for(int i=1;i<=1e9;i++) { if(tong[i]==0) { s[x]=i; x++; for(int bs=2;bsi<=1e9;bs++) { tong[bsi]=1; } } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; f(); while(T—) { int a, b; cin >> a >> b; if((s[a]^s[b])==1) cout << “Yes” << endl; else cout << “No” << endl; } return 0; }

正确代码 #include<bits/stdc++.h> #define endl ‘\n’ using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while(T—) { int a, b; cin >> a >> b; if(a==1&&b==2||a==2&&b==1) cout << “Yes” << endl; else cout << “No” << endl; } return 0; }

错因 没有发现只有1,2才能行。

原文