-
Notifications
You must be signed in to change notification settings - Fork 0
/
pta1015.cpp
81 lines (72 loc) · 1.47 KB
/
pta1015.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h> //导入万能库
#define MAX 501 //set the maxsize
#define INF 0x7fffffff //set the edge of INF
using namespace std;
bool jug_prime(int n);
string to_num(int num,int radix);
int to_s(string num,int radix);
int main(void)
{
int radix,num;
while(1)
{
cin>>num;
if(num<0)
break;
else
{
cin>>radix;
}
if(jug_prime(num))
cout<<"No"<<endl;
else
{
string to_pri=to_num(num,radix);
reverse(to_pri.begin(),to_pri.end());
int as=to_s(to_pri,radix);
if(jug_prime(as))
cout<<"No"<<endl;
else
{
cout<<"Yes"<<endl;
}
}
}
return 0;
}
bool jug_prime(int n)
{
if(n==1)
return 1;
for(int i=2;i<=(int)sqrt(n);++i)
{
if(n%i==0)
return 1;
}
return 0;
}
string to_num(int num,int radix) 十进制转radix
{
stack<int>sta;
while(num)
{
sta.push(num%radix);
num/=radix;
}
string s="";
while(!sta.empty())
{
s.push_back(sta.top()+'0');
sta.pop();
}
return s;
}
int to_s(string num,int radix) //转为十进制
{
int sum=0;
for(int i=0;i<num.size();++i)
{
sum=sum*radix+num[i]-'0';
}
return sum;
}