SGU 200. Cracking RSA (高斯消元求自由变元个数)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=200
200. Cracking RSA
memory limit per test: 65536 KB
output: standard
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.
9 20 500 3
这题就是给出了m个数,这m个数的质因子都是前t个质数构成的。
问有多少个这m个数的子集,使得他们的乘积是完全平方数。
完全平方数就是要求每个质因子的指数是偶数次。
对每个质因子建立一个方程。 变成模2的线性方程组。
求解这个方程组有多少个自由变元,答案就是 2^ret - 1 ,去掉空集的情况!
/* ***********************************************
Author :kuangbin
Created Time :2014-1-20 9:19:03
File Name :E:\2014ACM\SGU\SGU200.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
using namespace std; //高精度加法
void add(char a[],char b[],char c[])
{
int len1 = strlen(a);
int len2 = strlen(b);
int len = max(len1,len2);
int up = ;
for(int i = ;i < len;i++)
{
int tmp = ;
if(i < len1) tmp += a[i] - '';
if(i < len2) tmp += b[i] - '';
tmp += up;
c[i] = tmp% + '';
up = tmp/;
}
if(up)
c[len++] = up + '';
c[len] = ;
}
void SUB_ONE(char a[])
{
int id = ;
while(a[id] == '')id++;
a[id]--;
for(int i = ;i < id;i++)
a[i] = '';
int len = strlen(a);
while(len > && a[len-] == '')len--;
a[len] = ;
} int equ,var;
int a[][];
int x[];
int free_x[];
int free_num; //返回值为-1表示无解,为0是唯一解,否则返回自由变元个数
int Gauss()
{
int max_r, col, k;
free_num = ;
for(k = , col = ; k < equ && col < var; k++, col++)
{
max_r = k;
for(int i = k+ ; i < equ; i++)
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
if(a[max_r][col] == )
{
k--;
free_x[free_num++] = col; //自由变元
continue;
}
if(max_r != k)
{
for(int j = col; j < var+; j++)
swap(a[k][j],a[max_r][j]);
}
for(int i = k+; i < equ;i++)
if(a[i][col] != )
for(int j = col; j < var+;j++)
a[i][j] ^= a[k][j];
}
for(int i = k;i < equ;i++)
if(a[i][col] != )
return -;
if(k < var)return var-k;
for(int i = var-; i >= ;i--)
{
x[i] = a[i][var];
for(int j = i+; j < var;j++)
x[i] ^= (a[i][j] && x[j]);
}
return ;
} const int MAXN = ;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i%prime[j] == )break;
}
}
} int b[];
char str1[],str2[]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
getPrime();
int t,m;
while(scanf("%d%d",&t,&m) != EOF)
{
for(int i = ;i < m;i++)
scanf("%d",&b[i]);
equ = t;
var = m;
for(int i = ;i < t;i++)
for(int j = ;j < m;j++)
{
int cnt = ;
while(b[j]%prime[i+] == )
{
cnt++;
b[j] /= prime[i+];
}
a[i][j] = (cnt&);
}
for(int i = ;i < t;i++)
a[i][m] = ;
int ret = Gauss();
strcpy(str1,"");
for(int i = ;i < ret;i++)
{
add(str1,str1,str2);
strcpy(str1,str2);
}
SUB_ONE(str1);
int len = strlen(str1);
for(int i = len-;i >= ;i--)
printf("%c",str1[i]);
printf("\n");
}
return ;
}
SGU 200. Cracking RSA (高斯消元求自由变元个数)的更多相关文章
- SGU 200.Cracking RSA(高斯消元)
时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...
- SGU 200. Cracking RSA(高斯消元+高精度)
标题效果:鉴于m整数,之前存在的所有因素t素数.问:有多少子集.他们的产品是数量的平方. 解题思路: 全然平方数就是要求每一个质因子的指数是偶数次. 对每一个质因子建立一个方程. 变成模2的线性方程组 ...
- SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax
275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are ...
- Acdream1217 Cracking' RSA(高斯消元)
题意:给你m个数(m<=100),每个数的素因子仅来自于前t(t<=100)个素数,问这m个数的非空子集里,满足子集里的数的积为完全平方数的有多少个. 一开始就想进去里典型的dp世界观里, ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- 【BZOJ2137】submultiple 高斯消元求伯努利数
[BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...
- SPOJ HIGH(生成树计数,高斯消元求行列式)
HIGH - Highways no tags In some countries building highways takes a lot of time... Maybe that's bec ...
随机推荐
- Spring Mvc + Maven + yuicompressor 使用 profile 来压缩 javascript ,css 文件; (十)
profile相关知识点: 在开发项目时,设想有以下场景: 你的Maven项目存放在一个远程代码库中(比如github),该项目需要访问数据库,你有两台电脑,一台是Linux,一台是Mac OS X, ...
- 第11月第20天 sqlite3_open xcode mysql connector
1. sqlite3_open 死锁 * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP frame ...
- 原生JS获取元素的位置与尺寸
1.内高度.内宽度: 内边距 + 内容框 element.clientWidth element.clientHeight 2.外高度,外宽度: 边框 + 内边距 + 内容框 element.offs ...
- 跳过复制错误——slave_skip_errors、slave_exec_mode
这一篇写写复制错误处理相关的另两个参数slave_skip_errors.slave_exec_mode,基本环境参考<复制错误处理——sql_slave_skip_counter> 一. ...
- Markdown基础教程
标题 Markdown支持6种级别的标题,对应html标签 h1 ~ h6
- jquery.form.js 让表单提交更优雅
jquery.form.js 让表单提交更优雅.可以页面不刷新提交表单,比jQuery的ajax提交要功能强大. 1.引入 <script src="/src/jquery-1.9.1 ...
- python 多进程的启动和代码执行顺序
对照着廖雪峰的网站学习Python遇到些问题: 在进程中,父进程创建子进程时发现,显示不是按照顺序显示,疑问? 参照代码如下: from multiprocessing import Pool imp ...
- 初识 Asp.Net数据验证控件
在我们建立一个Asp.Net Web应用程序的时候我一般都会注意我们工具如图
- yum安装tomcat
http://www.cnblogs.com/liaolongjun/p/5638740.html http://www.awspack.com/os/linux/yum-install-tomcat ...
- AngularJs(SPA)单页面SEO以及百度统计应用(下)
苍苍之天不得久视,堂堂之地不得久履 当你小心翼翼的开启服务端渲染的同时,一个问题不得不注意,使用内存模式去保存渲染过的页面,这样服务断掉重启后,缓存也没有了,所以这里我们使用mongdodb进行本地化 ...