当前位置:网站首页>PAT甲级 - 1015 Reversible Primes (进制转换&素数判断)
PAT甲级 - 1015 Reversible Primes (进制转换&素数判断)
2022-06-22 09:21:00 【S atur】
题意:判断一个数N是否是D进制内的翻转素数,即其在D进制内翻转后依旧是素数。
思路:思路很简单,但是题意有点容易被误解,即当前N为十进制数,首先N必须是质数;再者其转换成D进制数后翻转,再变成十进制数后依旧得是素数才满足。
代码实现:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5+10;
int x, d;
string radix_to_ten(int radix, string s){ // radix->10
int res = s[0]-'0';
for(int i = 1; i < s.size(); i ++){
res *= radix;
res += s[i]-'0';
}
return to_string(res);
}
string ten_to_radix(string s, int radix){ // 10->radix
int x = stoi(s);
string res;
while(x){
res += (x%radix)+'0';
x /= radix;
}
reverse(res.begin(), res.end());
return res;
}
;
bool prime[N];
void is_prime() {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for(int i = 2; i < N; i++) {
if(prime[i]) {
for(int j = i + i; j < N; j += i) {
prime[j] = false;
}
}
}
}
signed main()
{
is_prime();
while(cin >> x){
if(x<0) break;
cin >> d;
bool a = prime[x];
string x_tmp = ten_to_radix(to_string(x), d); // 再转成d进制
reverse(x_tmp.begin(), x_tmp.end()); // 再翻转
int xx = stoi(radix_to_ten(d, x_tmp)); // 再转成10进制
bool b = prime[xx];
cout << (a&&b?"Yes":"No") << endl;
}
return 0;
}
边栏推荐
- IS_ ERR()
- See how much volatile you know
- Lexical Sign Sequence
- 两个线程各执行100次i++,得到的可能值
- 通过docker安装mysql(5.7+8.0)并配置主从复制(GTID+增强半同步)
- 5 interview questions, grasp the underlying principle of string!
- Embedded development terminology concept summary
- Debian10配置RSyslog+LoganAlyzer日志服务器
- ModuleNotFoundError: No module named ‘rospy‘,pip也找不到安装包
- [node] please accept the crawler. We won't worry about the data any more
猜你喜欢
![[node] node+ SMS API to realize verification code login](/img/92/acd2abf73a7b0459127dd19bf32a7c.png)
[node] node+ SMS API to realize verification code login

Network security Kali penetration learning how to conduct Nessus vulnerability detection

DOM programming

Logistic regression and linear regression

Summary and future prospect of transfer learning | community essay solicitation
![[学习笔记] 回滚莫队](/img/19/d374dd172b9609a3f57de50791b19e.png)
[学习笔记] 回滚莫队

Kali Trojan invades win7 system

機器學習|nltk_Data下載錯誤|nltk的stopwords語料下載錯誤解决方法

机器学习|nltk_Data下载错误|nltk的stopwords语料下载错误解决方法

一个老开源人的自述-如何干好开源这件事
随机推荐
mknod
架设多个web站点
A readme of an old open source person - how to do open source well
Function summary (1)
一个老开源人的自述-如何干好开源这件事
Why use gradient descent method
copy_ from_ User and copy_ to_ user
Brush questions in C language | judge whether a certain year is only a leap year (15)
day260:只出现一次的数字 III
How C processes use non static methods
Common SQL statements in MySQL
day500:键盘行
编译basalt时出现的报错
Find the size of cosine
招募令|数据可视化开发平台“FlyFish”「超级体验官」招募啦
SQL编程task06作业-秋招秘籍ABC
Servlet的生命周期
It is hoped that more and more women will engage in scientific and technological work
container_of
Sparse array ^ create ^ restore ^ save ^ fetch -- family bucket