POJ - 2635 E - The Embarrassed Cryptographer
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
Output
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 题意:求给出的一个数,求k中是否有比L小的的因子,其中K是大数 思路:因为我们L范围只有1e6,所以我们找寻因子可以直接遍历1-(L-1),然后我们进行大数取余操作,直接看余数是否为0即可
但是这样的时间复杂度就超时了,字符串长度是100 * L范围1e6 * 20组数据 = 2*1e9 (超时)
这个时候我们就要进行优化20组数据我们无法优化,只能从大数取余和找因子这里入 1,找因子
我们可以只找素因子,如果素因子都不可以说明其他因子也行不通,所以我们可以进行素数筛法打表 2.大数取余
因为我们在进行找因子的时候我们每次都要进行大数取余,重复这个操作,我们就可以进行大数转换千进制,就可以在进行大数取余操作的时候大大缩减时间复杂度
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAX 1000100
using namespace std;
int n;
int prime[MAX+];
char str[];
void primetable()//素数打表
{
int ps=;
prime[ps++]=;
for(int i=;i<=MAX;i+=)
{
int flag=;
for(int j=;prime[j]*prime[j]<=i;j++)
if(!(i%prime[j]))
{
flag=;
break;
}
if(flag)
prime[ps++]=i;
}
}
int main()
{
primetable();
while(scanf("%s%d",str,&n)!=EOF)
{
if(str[]==''&&n==) break;
int len=strlen(str);
int ans=;
int a[];
for(int i=len-;i>=;)//转换成千进制
{
if(i<){
if(i==)
a[ans++]=str[i]-'';
else a[ans++]=str[i]-''+(str[i-]-'')*;
i=-;
}
else{
a[ans++]=str[i]-''+(str[i-]-'')*+(str[i-]-'')*;
i-=;
}
}
int flag=;
for(int i=;prime[i]<n;i++)//遍历素因子
{
long long sum=;
for(int j=ans-;j>=;j--)
{
sum=(sum*+a[j])%prime[i];
}
if(sum==)
{
printf("BAD %d\n",prime[i]);
flag=;
break;
}
}
if(flag==)
{
printf("GOOD\n");
}
}
}
POJ - 2635 E - The Embarrassed Cryptographer的更多相关文章
- POJ 2635 The Embarrassed Cryptographer
大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
- (POJ2635)The Embarrassed Cryptographer(大数取模)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...
- POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)
The Embarrassed Cryptographer DescriptionThe young and very promising cryptographer Odd Even has imp ...
- poj2635The Embarrassed Cryptographer(同余膜定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15069 A ...
- HDU 2303 The Embarrassed Cryptographer
The Embarrassed Cryptographer 题意 给一个两个素数乘积(1e100)K, 给以个数L(1e6), 判断K的两个素数是不是都大于L 题解 对于这么大的范围,素数肯定是要打表 ...
- poj 2635 The Embarrassed Cryptographer(数论)
题目:http://poj.org/problem?id=2635 高精度求模 同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...
- POJ 2635 The Embarrassed Cryptographer 大数模
题目: http://poj.org/problem?id=2635 利用同余模定理大数拆分取模,但是耗时,需要转化为高进制,这样位数少,循环少,这里转化为1000进制的,如果转化为10000进制,需 ...
随机推荐
- MP4文件格式
MP4文件格式详解(ISO-14496-12/14) Author:Pirate Leo Email:codeevoship@gmail.com 一.基本概念 1. 文件,由许多Box和FullBox ...
- android--------实现Activity和Fragment通信的面向对象的万能接口
前言 开发一个app时,常用Activity和Fragment,由于操作方便Fragment越来越受欢迎,这样就避免不了Activity和Fragment.Fragment和Fragment之间的通信 ...
- Remove Duplicates From Sorted Array leetcode java
算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- csu oj 1341 string and arrays
Description 有一个N*N的字符矩阵,从上到下依次记为第1行,第2行,……,第N行,从左至右依次记为第1列,第2列,……,第N列. 对于这个矩阵会进行一系列操作,但这些操作只有两类: (1) ...
- linux创建定时任务,定时执行sql
终于弄清楚一个问题了.linux创建定时任务,定时执行sql,其中分为两个case. case-1 sql语句较少,因此直接在 shell脚本中 写sql语句.如下: [oracle@Oracle11 ...
- Leetcode 131
class Solution { public: vector<vector<string>> partition(string s) { vector<string&g ...
- sql语句的各种模糊查询语句
一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1.%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况 ...
- Python面向对象之继承
前言: 继承是面向对象的3大特性之一,对于继承一点要注意一下4点. 一.基本查找 如果子类继承了父类,子类的实例化对象,没有的方法和属性会去父类找 class Parent(object): #父类 ...
- java关键字总结
static: 用来修饰成员变量和成员方法,也可以形成静态static代码块,可以形成静态内部类,也可以用于静态导包. 1.静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实 ...
- python 多进程练习 调用 os.system命令
import sys import getopt import os import multiprocessing def list_all_file(path): """ ...