题目链接: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 ;
}