The Embarrassed Cryptographer

Description
The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of users, which is now in use in his company. The cryptographic keys are created from the product of two primes, and are believed to be secure because there is no known method for factoring such a product effectively.
What Odd Even did not think of, was that both factors in a key should be large, not just their product. It is now possible that some of the users of the system have weak keys. In a desperate attempt not to be fired, Odd Even secretly goes through all the users keys, to check if they are strong enough. He uses his very poweful Atari, and is especially careful when checking his boss' key.
Input
The input consists of no more than 20 test cases. Each test case is a line with the integers 4 <= K <= 10100 and 2 <= L <= 106. K is the key itself, a product of two primes. L is the wanted minimum size of the factors in the key. The input set is terminated by a case where K = 0 and L = 0.
Output
For each number K, if one of its factors are strictly less than the required L, your program should output "BAD p", where p is the smallest factor in K. Otherwise, it should output "GOOD". Cases should be separated by a line-break.
Sample Input
143 10
143 20
667 20
667 30
2573 30
2573 40
0 0
Sample Output
GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31

题目大意:

    给定一个大数N,它是又两个素数a,b相乘生成的。在给定一个c,判断是否 c<min(a,b). 若c大于min(a,b),输出BAD min(a,b);否则输出GOOD

解题思路:

    给定的c小于等于10^6。故将10^6以内的素数用筛选法筛选出来,在判断是否存在N%prime==0。

    由于N的范围为10^100。不能直接判断。

    举个例子:123%6

    ->((1%6*10+2)%6*10+3)%6

    可以分步取余。

    PS:一位一位取余数会超时。三位三位的取也不会超int型,且效率不会超时。

Code:

 /*************************************************************************
> File Name: poj2635.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月29日 星期三 01时13分50秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 1000010
using namespace std;
bool is_prime[MAXN];
char num[];
int num_int[];
int N;
void init()
{
is_prime[]=is_prime[]=;
for (int i=;i<=MAXN-;i++)
if (!is_prime[i])
for (int j=i+i;j<=MAXN-;j+=i)
is_prime[j]=;
}
int main()
{
init();
while (cin>>num>>N)
{
int len=strlen(num);
if (len==&&num[]==''&&N==) break;
int k=;
for (int i=len-; i>=;)
{
int tmp=;
tmp+=num[i--]-'';
if (i>=) tmp+=*(num[i--]-'');
if (i>=) tmp+=*(num[i--]-'');
num_int[k++]=tmp;
}
k--;
int t=k;
bool ok=;
for (int i=; i<N; i++)
if (!is_prime[i])
{
k=t;
int ans=num_int[k--]%i;
while (k>=) ans=(ans*+num_int[k--])%i;
if (!ans) {ok=;printf("BAD %d\n",i);break;}
}
if (!ok)
printf("GOOD\n");
}
return ;
}

POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)的更多相关文章

  1. HDU-2303 The Embarrassed Cryptographer 高精度算法(大数取模)

    题目链接:https://cn.vjudge.net/problem/HDU-2303 题意 给一个大数K,和一个整数L,其中K是两个素数的乘积 问K的是否存在小于L的素数因子 思路 枚举素数,大数取 ...

  2. (POJ2635)The Embarrassed Cryptographer(大数取模)

    The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...

  3. The Embarrassed Cryptographer(高精度取模+同余模定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11435   Accepted: 3040 Description The ...

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

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  5. matlab取模与取余

    mod函数采用floor,rem函数采用fix函数.那么什么是floor和fix? fix(x):截尾取整.如: >> fix([3.4 , -3.4]) ans = 3 -3 floor ...

  6. POJ 2635 The Embarrassed Cryptographer 高精度

    题目地址: http://poj.org/problem?id=2635 题意:给出一个n和L,一直n一定可以分解成两个素数相乘. 让你判断,如果这两个素数都大于等于L,则输出GOOD,否则输出最小的 ...

  7. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  8. HDU 2303 The Embarrassed Cryptographer

    The Embarrassed Cryptographer 题意 给一个两个素数乘积(1e100)K, 给以个数L(1e6), 判断K的两个素数是不是都大于L 题解 对于这么大的范围,素数肯定是要打表 ...

  9. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

随机推荐

  1. C++编译期判断是否能够转型

    #include <iostream> #include <vector> using namespace std; template<class T,class U&g ...

  2. Linux ---> 简单socket

    #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> ...

  3. Repeater和Gridview前台显示行号的方法

    Repeater : Container.ItemIndex (行号从零开始,如果想改为从1开始,那么可以将以上的代码改为Container.ItemIndex + 1),见下示例: <asp: ...

  4. 利用cglib生成动态java bean

    cglib详细学习 http://blog.csdn.net/u010150082/article/details/10901641 cglib-nodep jar报下载 http://grepcod ...

  5. [DevExpress]ChartControl之SeriesTemplate示例

    关键代码: using System; using System.Data; using System.Windows.Forms; using CSharpUtilHelpV2; using Dev ...

  6. 01_mvc保存时出错

    修改实体类报错 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项.   原因是 数据表中的自增主键列未赋值.

  7. The main roles of LTE eNodeB.

    eNB的功能包括: RRM(无线资源管理) IP头压缩 用户数据流加密 UE附着时的MME选择 寻呼信息的调度传输 广播信息的调度传输 设置和提供eNB的测量 CMC(连接移动性控制) 动态资源分配 ...

  8. 第2章 Git安装与设置

    2.1 安装Git 略 2.2 设置Git 通过命令git config,用户可以把此类信息提供给本地版本库. 全局变量:在这台计算机上使用任何Git版本库时,这些全局变量的值都起作用.在相关命令中加 ...

  9. EXTJS 4.2 日期控件

    { xtype: "fieldcontainer", layout: "hbox", items: [{ fieldLabel: '开始时间', name: ' ...

  10. Ext Grid 加载超时设置timeout: 180000

    var insideGridStore = Ext.create('Ext.data.Store', { model: 'CarComponents',//这个地方CarComponents不是一个对 ...