POJ 2635 The Embarrassed Cryptographer 高精度

题目地址: http://poj.org/problem?id=2635


题意:给出一个n和L,一直n一定可以分解成两个素数相乘。

让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的那个素数。


从1到1000000的素数求出来,然后一个一个枚举到L,看能否被n整除,能的话就输出BAD+改素数

都不行的话,说明两个素数都大于等于L,输出GOOD


AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef long long LL;
const int N=1000005;
const LL II=100000000;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

LL pri[N/100];
bool num[N];
int xx;
char s[105];
LL x[100];

void prime()
{
    LL i,j;
    int k=0;
    memset(num,0,sizeof(num));
    for(i=2;i<N;i++)
    {
        if(!num[i])
        {
            pri[++k]=i;
            for(j=i;j<N;j+=i)
                num[j]=1;
        }
    }
    xx=k;
}

void toint(char *s,LL *t,int &k)
{
    int len=strlen(s),j;
    char x[10]={0};
    k=0;
    for(;len/8;len-=8)
    {
        strncpy(x,s+len-8,8);
        LL sum=0;
        for(j=0;j<8;j++)
            sum=sum*10+x[j]-'0';
        t[k++]=sum;
    }
    if(len)
    {
        strncpy(x,s,len);
        LL sum=0;
        for(j=0;j<len;j++)
            sum=sum*10+x[j]-'0';
        t[k++]=sum;
    }
}

bool modd(LL p,int len)
{
    int i;
    LL xh=0;
    for(i=len-1;i>=0;i--)
        xh=(xh*II+x[i])%p;
    if(xh==0)
        return true;
    return false;
}

int main()
{
    int i,j,L;
    prime();
    while(scanf("%s%d",s,&L))
    {
        if(strcmp(s,"0")==0&&L==0)
            break;
        int len;
        toint(s,x,len);
        int p=1,flag=0;
        while(pri[p]<L)
        {
            if(modd(pri[p],len))
            {
                flag=1;
                printf("BAD %lld
",pri[p]);
                break;
            }
            p++;
        }
        if(flag==0)
            printf("GOOD
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/dyllove98/p/3198824.html