HDU 2303 The Embarrassed Cryptographer

检查有无小于L的素数能整数K

因为L只有10的六次打一个10的六次内的素数表

因为  X*Y%mod==(X%mod*Y%mod)%mod

可以将K分解再取mod

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 11111
#include <queue>
#include <vector>
#define LL long long
const int INF = 999999;
int pri[1000003],prim[333334];
char s[222];
int main()
{
    //freopen("in.txt","r",stdin);
    int k,num=0;
    memset(pri,0,sizeof(pri));
    for(int i=2; i<=1000000; i++){
        if(pri[i]==0){
            prim[num++]=i;
            for(int j=i*2; j<=1000000; j+=i)
                pri[j]=1;
        }
    }
    while(scanf("%s %d",s,&k))
    {
        if(k==0&&s[0]=='0') break;
        int len=strlen(s);
        int sum=0,minn=10000003,i;
        for( i=0;i<num; i++)
        {
            sum=0;
            for( int j=0;j<len;j++)
                sum=(sum*10+(s[j]-'0'))%prim[i];//mod
            if(sum==0)//getans
                break;
        }
        if(i!=num&&prim[i]<k)//out
            printf("BAD %d\n",prim[i]);
        else printf("GOOD\n");
    }
    return 0;
}

时间: 2024-08-28 21:33:19

HDU 2303 The Embarrassed Cryptographer的相关文章

HDOJ 题目2303 The Embarrassed Cryptographer(数学)

The Embarrassed Cryptographer Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 563    Accepted Submission(s): 172 Problem Description The young and very promising cryptographer Odd Even has impl

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

poj2635(The Embarrassed Cryptographer)

题目地址:The Embarrassed Cryptographer 题目大意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数. 解题思路: 高精度求模+同余模定理 同余模定理: 例如要验证123是否被3整除,只需求模124%3 但当123是一个大数时,就不能直接求,只能通过同余模定理对大数“分块”间接求模 具体做法是: 先求1%3 = 1 再求(1*10+2)%3 = 0 再求 (0*10+4)% 3 = 1 那

POJ 2635 The Embarrassed Cryptographer(高精度取模 + 同余模定理)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12905   Accepted: 3472 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

poj 2635 The Embarrassed Cryptographer

题目链接:http://poj.org/problem?id=2635 思路:当看到K的最大值为 10100 的第一想法就是用java打大数,建立一个素数表,然后再在素数表中去找,看是否有符合条件的. code: import java.math.*; import java.util.*; public class ggg { public static void main(String[] args) { int len=0; Scanner cin=new Scanner(System.i

POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)

The Embarrassed Cryptographer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2635 Appoint description:  System Crawler  (2015-05-28) Description The young and very promising cryptographer Odd E

poj2635--The Embarrassed Cryptographer(数论篇1,大数取模)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12496   Accepted: 3330 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

POJ 2635 The Embarrassed Cryptographer (同余线性方程+素数筛)

题目地址:POJ 2635 先用素数筛把10^6万以内素数筛出来.然后把输入的那个大数转化成数组,并且每三位存成一个数,这样可以节约内存和时间,然后利用同余线性的原理,对那个小整数以内的所有素数枚举,然后判断是否整除,找到最小的能被整除的. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer

题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are created from the product of two primes) 问构成K的最小素数是否绝对小于L,若是,则输出BAD p,p为最小素数,否则输出GOOD; 分析:从小到大枚举1~10^6内的素数p,while(p<L)时,判断K是否能被p整除,若能则证明构成K的最小素数绝对小于L,反之则大于L