SGU 200. Cracking RSA (Gaussian elimination + high precision)

Source: Internet
Author: User
Tags integer numbers

Title effect: In view of the M integer, all the factors that existed before the T primes. Q: How many subsets are there? Their product is the sum of squares of quantity.

Problem Solving Ideas:

The total number of squares is to require that the exponent of each mass factor is an even number of times.
Establish an equation for each of the mass factors.

into a linear equation set of modulo 2.
How many free-radicals are there in solving this equation set? The answer is 2^p-1. (-1 is the case of removing the empty set)

Note that because the 2^p is beyond the data range, high-precision algorithms are also required.

Cracking Rsatime Limit per test:0.25 sec.
Memory limit per test:65536 Kbinput:standard
Output:standard


The following problem is somehow related to the final stage of many famous integer factorization algorithms involved in so Me cryptoanalytical problems, for example cracking well-known RSA public key system.

The most powerful of such algorithms, so called quadratic sieve descendant algorithms, utilize the fact that if n = PQ whe Re p and Q is large unknown primes needed to is found out, then if V2=W2 (mod n), u≠v (mod n) and u≠-v (mod n), then G CD (v + W, N) is a factor of n (either p or Q).

Not getting further in the details of these algorithms, let us consider our problem. Given m integer Numbers b1, b2, ..., bm such, all their prime factors was from the set of first T-primes, the task is To find such a subset S of {1, 2, ..., m} that product of the BI for I from S was a perfect square i.e. equal to U2 for some in Teger U. Given such S we get one pair for testing (product of the elements stands for V when W was known from other steps of algorithm s which be of no interest to us, testing performed was checking whether pair is nontrivial, i.e. U≠V (mod n) and U≠-v (mod n)). Since we want to factor n with maximum possible probability, we would like to get as many such sets as possible. So the interesting problem could is to calculate the number of all such sets. This is exactly your task.

Input
The first line of the input file contains integers t and M (1≤t≤100, 1≤m≤100). The second line of the input file contains m integer numbers bi such, all their prime factors is from T first primes (for example, if T = 3 all their prime factors is from the set {2, 3, 5}). 1≤bi≤109 for all I.

Output
Output the number of non-empty subsets of the given set {BI}, the product of numbers from which is a perfect square


Sample Test (s)
Input
3 4 9 3Output

3

problem=200 "style=" Color:rgb (34,68,136); Text-decoration:none ">[submit]

#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <map> #include <set> #define EPS 1e-10///#define M 1000100#define ll __int64///#define LL Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((Fabs (x) <eps)? 0:x) const int MAXN = 210;using namespace Std;bool f[maxn+1000];int k[maxn+1000];int a[maxn][maxn];int num[maxn];int equ, var;c    Har Str1[maxn], str2[maxn];void Add (char a[], Char b[], char c[]) {int len1 = strlen (a);    int len2 = strlen (b);    int n = max (len1, len2);    int add = 0;        for (int i = 0; i < n; i++) {int cnt = 0;        if (I < len1) cnt + = a[i]-' 0 ';        if (I < len2) cnt + = b[i]-' 0 ';        CNT + = add;        add = CNT/10;    C[i] = cnt%10+ ' 0 ';    } if (add) c[n++] = add+ ' 0 '; C[n] = 0;}void Sub_1 (char a[]) {int s = 0;    while (a[s] = = ' 0 ') s++;    a[s]--;    for (int i = 0; i < s; i++) a[i] = ' 9 ';    int len = strlen (a);    while (Len > 1 && a[len-1] = = ' 0 ') len--; A[len] = 0;}    void Prime () {int t = 0;    Memset (f, false, sizeof (f));        for (int i = 2; I <= 1005; i++) {if (!f[i]) k[t++] = i;            for (int j = 0; J < T; j + +) {if (I*k[j] > 1005) break;            F[I*K[J]] = true;        if (i%k[j] = = 0) break;    }}}int Gauss () {int row, col;    int max_r;    row = col = 0;        while (Row < equ && Col < var) {max_r = row;        for (int i = row+1; i < equ; i++) {if (A[i][col]) max_r = i;            } if (A[max_r][col] = = 0) {col++;        Continue        if (max_r! = row) {for (int j = col; J <= var; j + +) Swap (A[max_r][j], a[row][j]); } for(int i = row+1; i < equ; i++)            {if (a[i][col] = = 0) continue;        for (int j = col; J <= var; j + +) A[i][j] ^= a[row][j];        } col++;    row++; } return Var-row;}    int main () {Prime ();    int n, m;        while (Cin >>n>>m) {memset (A, 0, sizeof (a));        for (int i = 0; i < m; i++) Cin >>num[i];        EQU = n;        var = m;                for (int i = 0, i < n; i++) {for (int j = 0; J < m; J + +) {int ans = 0;                    while (num[j]%k[i] = = 0) {ans + +;                Num[j]/=k[i];            } if (ans%2) a[i][j] = 1;        }} int N = Gauss ();        strcpy (str1, "1");            for (int i = 0; i < N; i++) {Add (str1, str1, str2);        strcpy (str1, str2);        } sub_1 (STR1);        for (int i = strlen (str1)-1; I >= 0; i--) cout<<str1[i]; Cout<<endl; } return 0;}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

SGU 200. Cracking RSA (Gaussian elimination + high precision)

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.