German Collegiate Programming Contest 2015: K. Upside down primes

Last night, I must have dropped my alarm clock. When the alarm went off in the morning, it showed 51:8051:80instead of 08:1508:15. This made me realize that if you rotate a seven segment display like it is used in digital clocks by 180180 degrees, some numbers still are numbers after turning them upside down.

My favourite numbers are primes, of course. Your job is to check whether a number is a prime and still a prime when turned upside down.

Input Format

One line with the integer NN in question (1 \le N \le 10^{16})(1N1016)NN will not have leading zeros.

Output Format

Print one line of output containing "yes" if the number is a prime and still a prime if turned upside down, "no"otherwise.

样例输入1
151
样例输出1
yes
样例输入2
23
样例输出2
no
样例输入3
18115211
样例输出3
no

注意刚开始也要判断这个数是不是素数

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue> 
#include<stdlib.h>
#include<cmath>
using namespace std;  
typedef long long ll;
using namespace std;  
/*ll Quick_Mod(ll a, ll b, ll mod)
{    
    ll res = 1,term = a % mod;    
    while(b)    
    {    
        if(b & 1) res = (res * term) % mod;    
        term = (term * term) % mod;    
        b >>= 1;    
    }    
    return res;    
}    
bool Is_Prime(ll n)    
{    
   int i;    
   srand(time(0));  
   for(i = 0;i < 30;i++)   
       if(Quick_Mod(1+rand()%(n-1) ,n - 1,n) != 1)    
         break;    
   if(i == 30) return true;    
   return false;    
}*/
bool isPrime(long long num) {
    if (num == 2 || num == 3) {
        return true;
    }

    //如果不在6的倍数附近,肯定不是素数
    if (num % 6 != 1 && num % 6 != 5) {
        return false;
    }
    //对6倍数附近的数进行判断
    int fz=sqrt(num)+1;
    for (int i = 5; i <=fz ; i += 6) {
        if (num % i == 0 || num % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}
int main(){
	char s[20];
	scanf("%s",s);
	long long sum=0;
	int len=strlen(s);
	long long sum1=0;
	for(int i=0;i<len;i++){
		sum1=sum1*10+s[i]-'0';
	}
	if(!isPrime(sum1)){
		printf("no\n");
		return 0;
	}
	for(int i=len-1;i>=0;i--){
		if(s[i]=='0'||s[i]=='2'||s[i]=='5'||s[i]=='8'||s[i]=='1')
			sum=sum*10+ s[i]-'0';
		else if(s[i]=='6') sum=sum*10+9;
		else if(s[i]=='9') sum=sum*10+6;
		else{
			printf("no\n");
			return 0;
		}
	} 
	//cout<<sum<<endl;
	if(sum==1){
		printf("no\n");
		return 0;
	}
	if(isPrime(sum)){
		printf("yes\n");
	}
	else printf("no\n");
	return 0;
}



欢迎大家加入 早起学习群,一起学习一起进步!(群号:642179511)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值