POJ-2635 the embarrassed cryptographer big number modulo

Source: Internet
Author: User

Given a large number, this number is obtained by multiplying two prime numbers. Now, you can ask whether there is a prime number smaller than the given number in the decomposition of this large number. Since the given number L is less than 10 ^ 6, we can create a table to 10 ^ 6. It is better to make it bigger (the smallest prime number larger than 10 ^ 6 ), in this way, you can easily jump out of the loop.

Next, we will try to divide all prime numbers whose number is smaller than l to see if the remainder is zero. Here we use the modulo operation of a large number, which is a big number pair of an int type. The process is like this. We can put a number (ABC... XYZ) as 10 * (ABC... XY) + z --> 10*(10 * (ABC... x) + Y) + Z. Keep doing this. We can start with modulo from the innermost part.

The Code is as follows:

#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define eps (1e-6)#define MAXN 1100000using namespace std;bool p[MAXN+5];int rec[90000], idx;void getprime() {    idx = -1;    for (int i = 2; i <= MAXN; ++i) {        if (!p[i]) {            rec[++idx] = i;        }        for (int j = 0; rec[j]*i <= MAXN; ++j) {            p[rec[j]*i] = 1;            if (i % rec[j] == 0) {                break;            }        }    }}int Mod(char key[], int L) {    int len = strlen(key+2), ret = 0, beg = 2;    if (len % 3 == 1) {        beg = 0;    } else if (len % 3 == 2) {        beg = 1;    }    len += 2;    for (int i = beg; i < len; i+=3) {        int temp = (key[i]-'0')*100+(key[i+1]-'0')*10+(key[i+2]-'0');        ret = ((long long)ret*1000 + temp) % L;    }    return ret;}int main() {    getprime();    int L, ret, flag;    char key[150] = {'0', '0'};    while (scanf("%s %d", key+2, &L), (key[2]-'0') | L) {        flag = 0;        for (int i = 0; rec[i] < L; ++i) {            if (Mod(key, rec[i]) == 0) {                ret = rec[i];                flag = 1;                break;            }        }        if (flag) {            printf("BAD %d\n", ret);        } else {            puts("GOOD");        }    }    return 0;    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.