POJ2635-The Embarrassed Cryptographer-大整数素因子

计算一个大整数(10^100)中有没有一个小于L的素因子。这个大整数是两个素数的乘积。其实就是RSA加密。

做法是把大整数表示成千进位,用数组存储,然后一位一位地取模。

/*--------------------------------------------------------------------------------------*/
//        Helica‘s header
//        Second Editions
//        2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

//debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=0;j<(M);j++){printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std;

int N,M,T,L;
char save[110];
int num[110];

const int MAXN = 1000100;
int prime[MAXN];

void getPrime()
{
    memset(prime,0,sizeof prime);
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i]) prime[++prime[0]] = i;
        for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)
        {
            prime[prime[j]*i] = 1;
            if(i%prime[j] == 0) break;
        }
    }
}

bool mod(int p,int len)
{
    int r = 0;
    for(int i=len-1;i>=0;i--)
        r = (r*1000 + num[i])%p;

    if(!r) return false;
    else return true;
}

int main()
{
    getPrime();

    while(scanf("%s%d",save,&L) && L)
    {
        int flag = 1;
        int len = strlen(save);

        memset(num,0,sizeof num);

        for(int i=0;i<len;i++)
        {
            int p = (len+2-i)/3-1;
            num[p] = num[p]*10+(save[i]-‘0‘);
        }
        len = (len+2)/3;

        for(int i=1;prime[i]<L;i++)
        {
            int p = prime[i];
            //printf("p=%d\n",p);
            if(!mod(p,len))
            {
                printf("BAD %d\n",p);
                flag = false;
                break;
            }
        }
        if(flag) printf("GOOD\n");
    }
}
时间: 2024-08-19 01:30:19

POJ2635-The Embarrassed Cryptographer-大整数素因子的相关文章

POJ2635 The Embarrassed Cryptographer 简单数论

题目链接 看到这题的示意图也是醉了~题意:给你一个k,他是两个素数之积,然后给了一个数字L,然后找到具体是哪两个素数相乘等于k,较小的那个素数是否小于L,若小于L就输出 "BAD"外加较小的那个素数,否则就输出"GOOD", 刚拿到这题目,有些钻牛角尖外加题意没看清楚,一开始纠结于 K很大,若想具体找出两个素数不可能,因为总有一个很大很大,求出其中一个素数 是否在10^6内是可以的,但是那时候我觉得还需要证明 k/prime  的值必须也是素数才符合要求,其实题目都

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 那

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

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

试除法整数分解 筛法整数分解 PollardRho大整数分解【模板】

试除法整数分解 int factor[11000]; //记录素因子 int ct; //记录素因子个数 void Divide(int N) { ct = 0; for(int i = 2; i <= sqrt(N*1.0); ++i) { while(N % i == 0) { factor[ct++] = i; N /= i; } } if(N != 1) factor[ct++] = N; } 筛法整数分解 const int MAXN = 11000; int Prime[MAXN],

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

light oj 1024 - Eid 大整数乘法

In a strange planet there are n races. They are completely different as well as their food habits. Each race has a food-eating period. That means the ith race eats after every xi de-sec (de-sec is the unit they use for counting time and it is used fo

第二大整数

第二大整数 问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来.说明:(1)0表示输入结束,它本身并不计入这组整数中.(2)在这组整数中,既有正数,也可能有负数.(3)这组整数的个数不少于2个. 输入格式:输入只有一行,包括若干个整数,中间用空格隔开,最后一个整数为0. 输出格式:输出第二大的那个整数. 输入输出样例 样例输入 5 8 -12 7 0 样例输出 7 #include <stdio.h

初涉算法——大整数类

处理方法:用数组存储整数. C++好处:重载运算符,使大整数类可以像int一样使用. 结构体BigInteger可用于储存高精度非负整数: 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<iostream> 5 using namespace std; 6 7 struct BigInteger { 8 static const int BASE = 100000000