日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【题目解析】1015 Reversible Primes (20 分)_27行代码AC

發布時間:2024/2/28 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【题目解析】1015 Reversible Primes (20 分)_27行代码AC 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


PAT甲級最優題解——>傳送門


A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<10^5) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:
For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:
73 10
23 2
23 10
-2

Sample Output:
Yes
Yes
No


題意:如果數n是素數, 將n轉化成d進制數,翻轉后在轉換回來, 判斷是否為負數。

舉例:輸入23 2

  • 23為素數,繼續
  • 23轉為二進制:10111
  • 10111翻轉:11101
  • 11101轉回十進制:29
  • 29為素數,輸出Yes
  • 本題用了素數的快速判定。

    具體原理見博客——>傳送門


    #include <bits/stdc++.h> using namespace std;bool isPrime(int n) {if(n == 0 || n == 1) return false;if(n == 2) return true;if(n%6 != 1 && n%6 != 5) return false; //不在6的周圍一定不是 int temp = sqrt(n); //在6的周圍也可能不是 for(int i = 5; i <= temp; i += 6) if(n%i==0 || n%(i+2)==0) return false;return true; }int main() {int n, d; while(cin>>n && n>=0 && cin>>d) {int n1 = n;string s;while(n1) {s += (char)(n1%d+'0');n1 /= d;}int sum = 0, siz = s.size()-1;for(auto i : s) sum += (i-'0')*pow(d, siz--);cout << ((isPrime(n) && isPrime(sum)) ? "Yes" : "No") << '\n';} return 0; }

    耗時:

    總結

    以上是生活随笔為你收集整理的【题目解析】1015 Reversible Primes (20 分)_27行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。