1015 Reversible Primes (20分) PAT 筛法求素数+进制转换

反手就是一波欧拉筛,忘记给0和1赋值为1坑死我了


#include<iostream>
#include<vector>
using namespace std;
const int N=1e5+10;
int vis[N];
int prime[N];
void getp(){
	int cnt=0;
	vis[0]=vis[1]=1;
	for (int i = 2; i < N; ++i)
	{
		if(!vis[i]){
			prime[cnt++]=i;
		}
		for (int j = 0; j<cnt&&i*prime[j]<N; ++j)
		{
			vis[i*prime[j]]=1;
			if(i%prime[j]==0)
				break;
		}
	}
}
int covert(int a, int radix){
	int res=0;
	while(a>0){
		res = res*radix+(a%radix);
		a/=radix;
	}
	return res;
}
int main(int argc, char const *argv[])
{
	int a,b;
	getp();
	vector<string> ans;
	while(cin>>a,a>=0){
		cin>>b;
		if(vis[a]||vis[covert(a,b)])
			ans.push_back("No");
		else
			ans.push_back("Yes");
	}
	for (int i = 0; i < ans.size(); ++i)
	{
		cout<<ans[i];
		if(i<ans.size()-1)
			cout<<endl;
	}
	return 0;
}
posted @ 2020-05-11 22:08  CrosseaLL  阅读(142)  评论(0编辑  收藏  举报