题意:
要求你把给的这个数按照他给的进制反转下,然后判断是否是素数。是就输出yes,否no
坑点:
1.1不是素数
2.题目说的是10^5,千万别写成10000.。。。。。嘛,这个算眼睛不敏感,要多练。
#include<iostream>
#include<string>
#include<fstream>
#include<cmath>
using namespace std;
string s;
int number;
int r;
bool isprime[111111];
string ans;
void deep(int radix, int num)
{
if (num < radix)
{
ans = (char)(num + '0');
return;
}
deep(radix, num / radix);
ans += (char)(num%radix + '0');
}
int change(int radix, string numm)
{
int length = numm.length();
int sum = 0;
int temp = 1;
for (int i = length - 1; i >= 0; i--)
{
if (numm[i] >= 'a'&&numm[i] <= 'z')
{
sum += (numm[i] - 'a' + 10)*temp;
}
else
{
sum += (numm[i] - '0')*temp;
}
temp *= radix;
}
return sum;
}
void doprime()
{
for (int i = 2; i < sqrt((double)111111); i++)
{
if (!isprime[i])
{
for (long long j = i*i; j < 111111; j += i)
{
isprime[j] = true;
}
}
}
}
void solve()
{
int length = ans.length();
s = "";
for (int j = length - 1; j >= 0; j--)
{
s += ans[j];
}
//cout << s << endl;
long long number = change(r, s);
if (!isprime[number]&&number!=1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void input()
{
doprime();
while (cin >> number)
{
if (number<0)
break;
cin >> r;
if (number == 1)
{
cout << "Yes" << endl;
continue;
}
if (isprime[number])
{
cout << "No" << endl;
continue;
}
ans = "";
deep(r, number);
solve();
}
}
int main()
{
input();
return 0;
}