20180711 K. Upside down primes

标准答案:

#include <iostream>

#include <string>
#include <cstdlib>


using namespace std;


string s;


bool prime(string n) {
long long num = atol(n.c_str());
if (num == 2) return true;
if (num == 1 || num % 2 == 0) return false;
for (long long i = 3; i*i <= num; i += 2) if (num % i == 0) return false;
return true;
}


string trans() {
string ret = "";
for (char c : s) {
if (c == '6') c = '9';
else if (c == '9') c = '6';
ret = c + ret;
}
return ret;
}


bool check() {
for (char c : s) if (c == '3' || c == '4' || c == '7') return false;
return prime(s) && prime(trans());
}


int main() {
cin >> s;
if (check()) cout << "yes" << endl;
else cout << "no" << endl;
return 0;

}


自己的:

1:(4组未通过)

#include <iostream>
#include <string>
#include <cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;


char s[100];


int pimer(long long k)
{
    if(k==1||k%2==0)
        return 0;
    if(k==2)
        return 1;
    for(long long  i=3; i*i<=k; i=i+2)
        if(k%i==0)
            return 0;
    return 1;
}


void zhuan()
{
    char temp;
    for(int i=0,l=strlen(s)-1; i<=l/2; i++)
    {
        temp=s[i];
        s[i]=s[l-i];
        s[l-i]=temp;
    }

    for(int i=0; i<strlen(s); i++)
        if(s[i]=='6')
            s[i]='9';
        else if(s[i]=='9')
            s[i]='6';
    //printf("%s\n",s);
}
int check()
{
    for(int i=0; i<strlen(s); i++)
    {
        if(s[i]=='3'||s[i]=='4'||s[i]==7)
            return 0;
    }
}
int main()
{
    long long n,m;
    cin>>s;
    n=atol(s);
    int flag1=1,flag2=1,flag3=1;
    flag1=pimer(n);


    zhuan();
    if(check()==0)
    {
        flag3=0;
    }
    if(flag3==0)
    {
        cout<<"no"<<endl;
        return 0;
    }
    m=atol(s);
    flag2=pimer(m);
    if(flag1==1&&flag2==1)
        cout<<"yes"<<endl;
    else
    {
        cout<<"no"<<endl;
        //printf("%d %d %s\n",flag1,flag2,s);
    }
    return 0;
}

2:(用了网上的模板,害怕大质数超时,通过了)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;


const int times = 20;
int number = 0;


map<long long, int>m;
long long Random( long long n ) //生成[ 0 , n ]的随机数
{
    return ((double)rand( ) / RAND_MAX*n + 0.5);
}


long long q_mul( long long a, long long b, long long mod ) //快速计算 (a*b) % mod
{
    long long ans = 0;
    while(b)
    {
        if(b & 1)
        {
            b--;
            ans =(ans+ a)%mod;
        }
        b /= 2;
        a = (a + a) % mod;


    }
    return ans;
}


long long q_pow( long long a, long long b, long long mod ) //快速计算 (a^b) % mod
{
    long long ans = 1;
    while(b)
    {
        if(b & 1)
        {
            ans = q_mul( ans, a, mod );
        }
        b /= 2;
        a = q_mul( a, a, mod );
    }
    return ans;
}


bool witness( long long a, long long n )//miller_rabin算法的精华
{
    //用检验算子a来检验n是不是素数
    long long tem = n - 1;
    int j = 0;
    while(tem % 2 == 0)
    {
        tem /= 2;
        j++;
    }
    //将n-1拆分为a^r * s


    long long x = q_pow( a, tem, n ); //得到a^r mod n
    if(x == 1 || x == n - 1) return true; //余数为1则为素数
    while(j--) //否则试验条件2看是否有满足的 j
    {
        x = q_mul( x, x, n );
        if(x == n - 1) return true;
    }
    return false;
}


bool miller_rabin( long long n )  //检验n是否是素数
{


    if(n == 2)
        return true;
    if(n < 2 || n % 2 == 0)
        return false; //如果是2则是素数,如果<2或者是>2的偶数则不是素数


    for(int i = 1; i <= times; i++)  //做times次随机检验
    {
        long long a = Random( n - 2 ) + 1; //得到随机检验算子 a
        if(!witness( a, n )) //用a检验n是否是素数
            return false;
    }
    return true;
}




int main()
{
    long long n,n1;
    long long m=0;
    char szw[100]= {0};
    cin>>n;
    n1=n;


    int flag1=0;
    int flag2=0;
    if( miller_rabin(n1))
    {
        flag1=1;
    }
    int q=0;
    while(n)
    {
        szw[q++]=n%10+48;
        n=n/10;
    }
    //cout <<szw<<endl;
    char qqq;
    for(int i=0,k=strlen(szw)-1; i<strlen(szw); i++)
    {
        qqq=szw[k-i];
        szw[k-i]=szw[i];
        szw[i]=qqq;
    }


    for(int i=0; i<strlen(szw); i++)
    {
        if(szw[i]=='3'||szw[i]=='4'||szw[i]=='7')
        {
            goto here;
        }


        if(szw[i]=='6')
            szw[i]='9';
        else if(szw[i]=='9')
            szw[i]='6';
    }
//cout <<szw<<endl;
    for(int i=0,k=strlen(szw)-1; i<strlen(szw); i++)
    {
        m=m+(szw[k-i]-48)*pow(10,i);
    }
    // cout<<m<<endl;


    if(miller_rabin(m))
        flag2=1;
//printf("%d %d\n",flag1,flag2);
here:
    if(flag1==1&&flag2==1)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;


    return 0;
}


猜你喜欢

转载自blog.csdn.net/z20172123/article/details/81037667