#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e8+5;
int pri[5761460],tot;
bitset<N> vis;
void pre(){
for(int i=2;i<=N;i++){
if(!vis[i]) pri[++tot]=i;
for(int j=1;j<=tot;j++){
if(pri[j]*i>N) break;
vis[pri[j]*i]=1;
if(i%pri[j]==0) break;
}
}
}
bool check(int x) {
int res=0;
while(x){res+=x%10;x/=10;}
return !vis[res];
}
signed main(){
vis[0]=1;
vis[1]=1;
pre();
int t,l,r;
cin>>t;
while(t--){
int ans=0;
cin>>l>>r;
for(int i=l;i<=r;i++)if(!vis[i] && check(i))ans++;
cout<<ans<<"\n";
}
return 0;
}
这段代码的提交结果
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e8+5;
int pri[5761460],tot;
bitset<N> vis;
void pre(){
for(int i=2;i<=N;i++){
if(!vis[i]) pri[++tot]=i;
for(int j=1;j<=tot;j++){
if(pri[j]*i>N) break;
vis[pri[j]*i]=1;
if(i%pri[j]==0) break;
}
}
}
bool check(int x) {
int res=0;
while(x){res+=x%10;x/=10;}
return !vis[res];
}
signed main(){
vis[0]=1;
vis[1]=1;
pre();
int t,l,r;
cin>>t;
while(t--){
int ans=0;
cin>>l>>r;
for(int i=l;i<=r;i++)if(!vis[i] && check(i))ans++;
cout<<ans<<"\n";
}
return 0;
}
这段代码的提交结果
#define ll long long
#define int long long