zoukankan      html  css  js  c++  java
  • 1015 Reversible Primes (20)(20 point(s))

    problem

    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
    

    answer

    #include<bits/stdc++.h>
    using namespace std;
    
    int prime[100000] = {2, 3};
    set<int> s;
    
    void Prime(){
    	s.insert(2);
    	s.insert(3);
    	int i, j, flag, ta = 2;
    	for(i = 5; i < 100010; i+=2){
    		for(j = 0, flag = 1; prime[j]*prime[j] <= i; j ++){
    			if(i % prime[j] == 0) {
    				flag = 0; break;
    			}
    		}
    		if(flag){
    			prime[ta++] = i;
    			s.insert(i);
    		}
    	}
    }
    
    bool isPrime(int num)
    {
    	set<int>::iterator it;
    	it = s.find(num);
    	if(it != s.end()) return true;
    	else return false;
    }
    
    int Reverse(int a, int d){
    	vector<int > s;
    	while(a > 0){
    		s.push_back(a%d);
    		a/=d;
    	}
    	int num = 0;
    	reverse(s.begin(), s.end());
    	for(int i =0;  i< s.size(); i++){
    		num += s[i]*pow((float)d, i);
    //		cout<<s[i];
    	}
    	return num;
    }
    int main(){
    	ios::sync_with_stdio(false);
    //	freopen("test.txt", "r", stdin);
    	
    	Prime();
    	int N, M;
    	while (cin>>N){
    		if(N < 0)  break;
    		cin>>M;
    		
    		if(!isPrime(N) || !isPrime(Reverse(N,M))) {
    			cout<<"No"<<endl;
    		}else{
    			cout<<"Yes"<<endl;
    		}
    	}
    	return 0;
    }
    

    experience

    • 素数晒法,背下模板。
  • 相关阅读:
    理解盒子模型
    Jackson 框架,轻易转换JSON
    JAVA仿百度分页
    最干净,最便捷的卸载Mysql
    Mysql 6.0安装过程(截图放不上去)
    开发JSP自定义标签
    JAVA实现文件上传
    开发过程中常用工具类
    JQUERY 简单易用的提示框插件
    什么是Bash Shell的内建(build in)命令
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9307870.html
Copyright © 2011-2022 走看看