当前位置:网站首页>Pat class A - 1015 reversible primes
Pat class A - 1015 reversible primes
2022-06-23 01:21:00 【S atur】
The question : Judge a number N Whether it is D Inverted prime number in base , That is to say, it is in D It is still a prime number after the decimal inversion .
Ideas : The idea is simple , But the meaning of the title is a little easy to be misunderstood , At present N It's a decimal number , First N Must be prime ; Furthermore, it is converted into D Flip after hexadecimal number , When it becomes a decimal number, it still has to be a prime number to be satisfied .
Code implementation :
#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); // Again into d Base number
reverse(x_tmp.begin(), x_tmp.end()); // Flip again
int xx = stoi(radix_to_ten(d, x_tmp)); // Again into 10 Base number
bool b = prime[xx];
cout << (a&&b?"Yes":"No") << endl;
}
return 0;
}
边栏推荐
- Voice network multiplayer video recording and synthesis support offline re recording | Nuggets technology solicitation
- Software construction course ADT and OOP understanding
- [sliding window] leetcode992 Subarrays with K Different Integers
- How to refine permissions to buttons?
- Add expiration time for localstorage
- [UVM] don't say that your VIP can't use ral model
- You can also do NLP (classification)
- Get the start and end dates of the current week
- Development status of full color LED display
- Js--- SVG to png
猜你喜欢

Steps to implement a container global component

Local deployment and problem solving of IIS in ArcGIS JS 4.23

Sélecteur de hiérarchie

最安全的现货白银的心理分析

3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World

How do beginners get started quickly and learn deeply?

OSPF综合实验

Development status of full color LED display

62. 不同路径

What is the storage structure and mode of data in the database?
随机推荐
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
Extend your kubernetes API using the aggregation API
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
Figure what are the uses and applications of neural networks?
崔鹏团队:万字长文梳理「稳定学习」全景图
C serializabledictionary serialization / deserialization
Development status of full color LED display
cadence SPB17.4 - allegro - 优化指定单条电气线折线连接角度 - 折线转圆弧
Tidb monitoring upgrade: a long way to solve panic
Flowable global listener monitors the start and end of a process
leetcode 91. Decode Ways 解码方法(中等)
New progress in the construction of meituan's Flink based real-time data warehouse platform
Hello, is the securities account presented by the Business School of qiniu business school safe? How can I open a safe stock account to speculate in stocks
You can also do NLP (classification)
Population standard deviation and sample standard deviation
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
What is the storage structure and mode of data in the database?
Vector 6 (inheritance)
數據庫中數據的儲存結構和方式是什麼?