当前位置:网站首页>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;
}
边栏推荐
- [Luogu] P2887 Sunscreen G
- Solutions to prompt non standard import in go language
- Volumedetect of ffmpeg
- mknod
- Machine learning | nltk_ Data download error |nltk's stopwords corpus download error solution
- [ZOJ] P3228 Searching the String
- The difference between single bracket and double bracket in shell
- Debian10配置RSyslog+LoganAlyzer日志服务器
- cocoscreator编译报错记录 Could not write cache value 。。。.gradle\daemon\4.10.3\regi
- Process status summary
猜你喜欢

Fuzzy query and aggregate function

在ensp上做多出口服務器映射

Embedded development terminology concept summary

在ensp上做防火墙的双机热备

Apprentissage automatique | nltk Erreur de téléchargement des données | solution d'erreur de téléchargement du corpus stopwords pour nltk

Win+sublime Text3 + go 1.9.2 environment setup diagram

Detr: end to end object detection with transformers (from Li Mu and Zhu)

Debian10 LVM逻辑卷

day367:有效的完全平方数

Byte/byte?别搞晕了!
随机推荐
Final典型案例
Threejs implementation of simple panoramic view demo
C language question brushing | three item operation to realize capital judgment (16)
scnprintf和snprintf的区别
Detr: end to end object detection with transformers (from Li Mu and Zhu)
==Classic interview questions
Win+sublime Text3 + go 1.9.2 environment setup diagram
C语言刷题 | 输入一个数输出对应的值(13)
在ensp上做多出口服務器映射
How C processes use non static methods
Classic & Cases
IS_ ERR()
Philosopher‘s Walk Gym 分治+分形
逻辑回归和线性回归
pytorch的模块使用:线性模型(未完成)
通过docker安装mysql(5.7+8.0)并配置主从复制(GTID+增强半同步)
Mapping Multi - export Server on ENSP
Volumedetect of ffmpeg
container_ of
5道面试题,拿捏String底层原理!