题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=200

200. Cracking RSA

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard
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. 

Input
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. 
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 20 500 3 
 
 
Output
 
 
 
 
 

这题就是给出了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 (高斯消元求自由变元个数)的更多相关文章

  1. SGU 200.Cracking RSA(高斯消元)

    时间限制:0.25s 空间限制:4M 题意: 给出了m(<100)个数,这m个数的质因子都是前t(<100)个质数构成的. 问有多少个这m个数的子集,使得他们的乘积是完全平方数. Solu ...

  2. SGU 200. Cracking RSA(高斯消元+高精度)

    标题效果:鉴于m整数,之前存在的所有因素t素数.问:有多少子集.他们的产品是数量的平方. 解题思路: 全然平方数就是要求每一个质因子的指数是偶数次. 对每一个质因子建立一个方程. 变成模2的线性方程组 ...

  3. 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 ...

  4. Acdream1217 Cracking' RSA(高斯消元)

    题意:给你m个数(m<=100),每个数的素因子仅来自于前t(t<=100)个素数,问这m个数的非空子集里,满足子集里的数的积为完全平方数的有多少个. 一开始就想进去里典型的dp世界观里, ...

  5. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  6. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  8. 【BZOJ2137】submultiple 高斯消元求伯努利数

    [BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...

  9. SPOJ HIGH(生成树计数,高斯消元求行列式)

    HIGH - Highways no tags  In some countries building highways takes a lot of time... Maybe that's bec ...

随机推荐

  1. Spring Mvc 一个接口多个继承; (八)

    在 spring 注解实现里,一个接口一般是不能多继承的! 除非在 bean 配置文件里有 针对这个 实现类的配置: <beans:bean id="icService" c ...

  2. iOS-Socket编程体验

    CHENYILONG Blog Socket编程体验 Socket编程体验  技术博客http://www.cnblogs.com/ChenYilong/新浪微博http://weibo.com/lu ...

  3. ios TextField限制输入两位小数

    只需要实现textField的这个代理方法就可以实现 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: ...

  4. HDU 4627 The Unsolvable Problem 杭电多校联赛第三场1009 数学题

    题意描述:给出一个n,要求在所有满足n = a+b的a和b里面求a和b的最小公倍数最大的两个数的最小公倍数. 解题报告:比赛的时候看到这个题的第一反应就是寻找这两个数一定是在a和b比较接近的地方找,这 ...

  5. 【腾讯云】自己搭建的腾讯云服务器JavaEE环境

    0.安装SSH登录 1.生成公钥对 ssh-keygen -t rsa -P '' -P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车.它在/home/ch ...

  6. linux笔记_day09

    1.运算器.控制器.存储器.输入输出(IO) 地址总线:内存寻址 数据总线:传输数据 控制总线:控制指令 寄存器:cpu暂时存储器 2.系统设定 默认输出设备:标准输出,STDOUT,1(描述符)(显 ...

  7. 基于 Apache 在本地配置多个虚拟主机

    如何使用 Apache 在本地配置出多个虚拟主机呢?而且使用不同的“域名”来访问本地不同的站点呢? 一般情况下,咱们都使用 localhost 来访问本机上的服务器,在我们的 C:/WINDOWS/s ...

  8. 【LOJ】 #2308. 「APIO2017」商旅

    题解 分数题可以想到分数规划,我们预处理出从i到j卖什么货物赚的最多,然后把每条边的边权改成"利润 - 效率 × 时间" 用spfa找正环即可 代码 #include <bi ...

  9. bzoj 1151: [CTSC2007]动物园zoo

    思路:因为每个人最多只能看到五个动物,我们考虑将其状压,f[ i ][ s ] 表示到了第 i 个位置, i, i + 1, i + 2, i + 3, i + 4这四个动物的状态为s, 此时的最大值 ...

  10. python 包详解

    包 包是一种管理 Python 模块命名空间的形式,采用"点模块名称". 比如一个模块的名称是 A.B, 那么他表示一个包 A中的子模块 B . 就好像使用模块的时候,你不用担心不 ...