P11212 『STA - R8』挑战 Goldbach 猜想
题意:
\(q\) 次询问,每次给一个正整数 \(n\),问有多少个不超过 \(n\) 的正整数 \(i\) 使得 \(i\) 和 \(n\bmod i\) 都是质数。
挺有趣一道题,一开始以为是打表的,然后发现有代码长度限制。
转化一下题意发现求的是有多少对 \(i,j\) 使得 \(n=i\times k+j\),其中 \(i,j\) 均为质数。
直接筛一下质数然后枚举就做完了。
不明白为什么洛谷评蓝,建议至少降到黄。
点击查看代码
#include<bits/stdc++.h>
#define p_b push_back
#define e_b emplace_back
#define ll long long
#define pii pair<int,int>
#define fir first
#define sec second
#define il inline
#define ios ios::sync_with_stdio(0),cin.tie(0)
using namespace std;
const int N=2e5+1,inf=1e9,M=2e4+1;
int p[M],tot,ans[N],q,n;
bool b[N];
signed main(){
ios;
for(int i=2;i<N;i++)if(!b[i]){for(int j=2;j*i<N;j++)b[j*i]=1;p[++tot]=i;}
for(int i=1;i<=tot;i++)for(int j=1;j<i;j++)for(int k=1;k*p[i]+p[j]<N;k++)ans[k*p[i]+p[j]]++;
cin>>q;
while(q--){cin>>n;cout<<ans[n]<<'\n';}
}