这个是我自己的,也不知道是哪里有问题,在AcWing上面过不了。因为测试集比较大我只能看到一部分。
+++new+++
知道哪里错了……Yes,我输出YES;No,我输出NO……
y总的代码真的是很整洁明了
emm照着y总的代码在平台上过了一遍
#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
LL N, D;
// x -> 10进制
int check(LL x) {
if(x == 1 || x == 0) return -1;
if(x == 2 || x == 3) return 1;
for (LL i = 2; i * i <= x; i ++ ) {
if(x % i == 0) return -1;
}
return 1;
}
string num_str(LL x, LL radix) {
string ans = "";
do{
LL t = x % radix;
if(t >= 0 && t <= 9) ans += (t + '0');
else ans += (t - 10 + 'a');
x /= radix;
} while ( x != 0 ); //使用do{}while()以防止输入为0的情况
return ans;
}
LL chance(string c, LL radix) {
LL tmp = 0;
for (int i = 0; i < c.size(); i ++ ) {
if(c[i] <= '9') {
tmp = tmp * radix + 1L * (c[i] - '0');
}
else if(c[i] <= 'z' && c[i] >= 'a'){
tmp = tmp * radix + 1L * (c[i] - 'a' + 10);
}
else if(c[i] <= 'Z' && c[i] >= 'A'){
tmp = tmp * radix + 1L *(c[i] - 'A' + 10);
}
}
return tmp;
}
int main()
{
cin >> N;
while(N >= 1) {
cin >> D;
// N不是质数
if(check(N) == -1) cout << "NO" << endl;
else {
string s = num_str(N, D);
// cout << s << endl;
LL res = chance(s, D);
// cout << "res: " << res << endl;
if(check(res) == 1) cout << "YES" << endl;
else cout << "NO" << endl;
}
cin >> N;
}
return 0;
}