SGU 200. Cracking RSA( 高斯消元 )
2015-12-14 13:23
573 查看
把每个数唯一分解, 要让乘积是完全平方数, 那就得让每个质数是偶数次方, 列出t条方程然后解它们在mod 2意义下的自由元个数v(异或方程组). 答案就是2^v-1(空集不算), 高精度...
---------------------------------------------------------------------------------------
#include<cstring>#include<cstdio>#include<algorithm>#include<bitset> using namespace std; const int maxn = 109; int ans[maxn], n;int p[maxn], pn = 0;int N, M;bitset<maxn> mat[maxn], F; void Init() { F.reset(); for(int i = 2; i <= 600; i++) { if(!F[i]) p[pn++] = i; for(int j = 0; j < pn && i * p[j] < 600; j++) { F[i * p[j]] = 1; if(i % p[j] == 0) break; } } scanf("%d%d", &N, &M); for(int i = 0; i < N; i++) mat[i].reset(); for(int i = 0; i < M; i++) { int v; scanf("%d", &v); for(int j = 0; j < N && v != 1; j++) for(; v % p[j] == 0; v /= p[j]) mat[j][i] = mat[j][i] ^ 1; } } void Write(int v) { n = 0; memset(ans, 0, sizeof ans); ans[n++] = 1; while(v--) { for(int i = 0; i < n; i++) ans[i] <<= 1; for(int i = 0; i < n; i++) if(ans[i] >= 10) ans[i] -= 10, ans[i + 1]++; if(ans
) n++; } ans[0]--; for(int i = 0; i < n; i++) if(ans[i] < 0) ans[i] += 10, ans[i + 1]--; if(!ans[n - 1]) n--; if(!n) putchar('0'); else { while(n--) putchar(ans
+ '0'); } puts("");} void Solve() { int v = 0; for(int i = 0; i < M; i++) { for(int j = v; j < N; j++) if(mat[j][i]) { if(j != v) swap(mat[j], mat[v]); for(int k = v; ++k < N; ) if(mat[k][i]) mat[k] ^= mat[v]; v++; break; } if(v >= N) break; } Write(M - v);} int main() { Init(); Solve(); return 0;}---------------------------------------------------------------------------------------
memory limit per test: 65536 KB[/align][align=center]input: standard
output: standard[/align]
[align=left]The following problem is somehow related to the final stage of many famous integer factorization algorithms involved in some 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 where p and q are large unknown primes needed to be found out, then if v2=w2(mod n), u ≠ v (mod n) and u ≠ -v (mod n), then gcd(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 that all their prime factors are from the set of first t primes, the task is to find such a subset S of {1, 2, ..., m} that product of bi for i from S is a perfect square i.e. equal to u2 for some integer u. Given such S we get one pair for testing (product of S elements stands for v when w is known from other steps of algorithms which are of no interest to us, testing performed is 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 be to calculate the number of all such sets. This is exactly your task. [/align][align=left]
Input[/align][align=left]
The first line of the input file contains two integers t and m (1 ≤ t ≤ 100, 1 ≤ m ≤ 100). The second line of the input file contains m integer numbers bi such that all their prime factors are from t first primes (for example, if t = 3 all their prime factors are from the set {2, 3, 5}). 1 ≤ bi ≤ 109 for all i. [/align][align=left]
Output[/align][align=left]
Output the number of non-empty subsets of the given set {bi}, the product of numbers from which is a perfect square
[/align][align=left]
Sample test(s)[/align][align=left]
Input[/align][align=left][/align][align=left][/align][align=left]
3 4
9 20 500 3 [/align][align=left][/align][align=left][/align][align=left]
Output[/align][align=left][/align][align=left][/align][align=left]
3 [/align][align=left][/align][align=left][/align][align=left][submit][/align][align=left][forum][/align][align=left][/align]
---------------------------------------------------------------------------------------
#include<cstring>#include<cstdio>#include<algorithm>#include<bitset> using namespace std; const int maxn = 109; int ans[maxn], n;int p[maxn], pn = 0;int N, M;bitset<maxn> mat[maxn], F; void Init() { F.reset(); for(int i = 2; i <= 600; i++) { if(!F[i]) p[pn++] = i; for(int j = 0; j < pn && i * p[j] < 600; j++) { F[i * p[j]] = 1; if(i % p[j] == 0) break; } } scanf("%d%d", &N, &M); for(int i = 0; i < N; i++) mat[i].reset(); for(int i = 0; i < M; i++) { int v; scanf("%d", &v); for(int j = 0; j < N && v != 1; j++) for(; v % p[j] == 0; v /= p[j]) mat[j][i] = mat[j][i] ^ 1; } } void Write(int v) { n = 0; memset(ans, 0, sizeof ans); ans[n++] = 1; while(v--) { for(int i = 0; i < n; i++) ans[i] <<= 1; for(int i = 0; i < n; i++) if(ans[i] >= 10) ans[i] -= 10, ans[i + 1]++; if(ans
) n++; } ans[0]--; for(int i = 0; i < n; i++) if(ans[i] < 0) ans[i] += 10, ans[i + 1]--; if(!ans[n - 1]) n--; if(!n) putchar('0'); else { while(n--) putchar(ans
+ '0'); } puts("");} void Solve() { int v = 0; for(int i = 0; i < M; i++) { for(int j = v; j < N; j++) if(mat[j][i]) { if(j != v) swap(mat[j], mat[v]); for(int k = v; ++k < N; ) if(mat[k][i]) mat[k] ^= mat[v]; v++; break; } if(v >= N) break; } Write(M - v);} int main() { Init(); Solve(); return 0;}---------------------------------------------------------------------------------------
200. Cracking RSA
[align=center]time limit per test: 0.25 sec.memory limit per test: 65536 KB[/align][align=center]input: standard
output: standard[/align]
[align=left]The following problem is somehow related to the final stage of many famous integer factorization algorithms involved in some 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 where p and q are large unknown primes needed to be found out, then if v2=w2(mod n), u ≠ v (mod n) and u ≠ -v (mod n), then gcd(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 that all their prime factors are from the set of first t primes, the task is to find such a subset S of {1, 2, ..., m} that product of bi for i from S is a perfect square i.e. equal to u2 for some integer u. Given such S we get one pair for testing (product of S elements stands for v when w is known from other steps of algorithms which are of no interest to us, testing performed is 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 be to calculate the number of all such sets. This is exactly your task. [/align][align=left]
Input[/align][align=left]
The first line of the input file contains two integers t and m (1 ≤ t ≤ 100, 1 ≤ m ≤ 100). The second line of the input file contains m integer numbers bi such that all their prime factors are from t first primes (for example, if t = 3 all their prime factors are from the set {2, 3, 5}). 1 ≤ bi ≤ 109 for all i. [/align][align=left]
Output[/align][align=left]
Output the number of non-empty subsets of the given set {bi}, the product of numbers from which is a perfect square
[/align][align=left]
Sample test(s)[/align][align=left]
Input[/align][align=left][/align][align=left][/align][align=left]
3 4
9 20 500 3 [/align][align=left][/align][align=left][/align][align=left]
Output[/align][align=left][/align][align=left][/align][align=left]
3 [/align][align=left][/align][align=left][/align][align=left][submit][/align][align=left][forum][/align][align=left][/align]
Author: | Andrew Stankevich |
Resource: | Petrozavodsk Winter Trainings 2003 |
Date: | 2003-02-06 |
相关文章推荐
- 第一个hibernate+spring+struts2项目
- Android消息机制源码解析(二)——消息的执行者Handler
- Android 欢迎页TimerTask的使用,及注意事项
- 室外定位訓練場景dataset
- 每个Android开发者必须知道的内存管理知识
- BZOJ 1968 约数研究
- HTTP协议-状态码详解(转)
- 值栈
- applicationContext.xml xxx-servlet.xml
- textField: wordWrap与autoSize的关系
- JAVA中生成C#中的GUID作为主键
- 通知:逆天异常库 V1.0版本支持下载了~~
- nodejs+express递归实现树形结构demo
- 查询是谁在用挂载的硬盘
- 工具类YJUtils
- php 去除空格
- 强大的Android参数模拟器,自由修改手机型号、SDK版本号等信息
- jsp-status 404错误的解决方法汇总
- 电商--订单设计(2)
- 介绍Docker