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 ...
随机推荐
- gulp.js 的安装以及使用
首先:电脑需要安装 Node.js 一个大绿色的安装按钮,点击就可以. 但还是推荐,点击download选中一款适合电脑配置的版本. Node安装过程,就是下一步 and 下一步~~ 测试手否安装成功 ...
- FPGA学习笔记. DDS
DDS原理 直接数字式频率合成器(Direct Digital Synthesizer) 频率计算公式 Fout = FW * Fclk / 2^N Fout 输出频率, Fw 频率控制字, N 位数 ...
- 使用sp_executesql
建议您在执行字符串时,使用 sp_executesql 存储过程而不要使用 EXECUTE 语句.由于此存储过程支持参数替换,因此 sp_executesql 比 EXECUTE 的功能更多:由于 S ...
- list(列表)操作【五】
L表示从左边(头部)开始插与弹出,R表示从右边(尾部)开始插与弹出. 一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(l ...
- python3之Splash
Splash是一个javascript渲染服务.它是一个带有HTTP API的轻量级Web浏览器,使用Twisted和QT5在Python 3中实现.QT反应器用于使服务完全异步,允许通过QT主循环利 ...
- docker之容器访问和网络连接(三)
前言 当一台服务器上部署了多个应用容器,它们直接可能需要相互通信,比如web应用容器需要访问mysql数据库容器. 主机访问容器 通过映射端口的形式我们可以在外部访问容器内的服务 # 将主机的127. ...
- vue项目里的日期格式化
在项目中,我们经常需要把后台传回的日期进行格式化,可以在common里定义一个公共的js export function formatDate (date, fmt) { if (/(y+)/.tes ...
- TcxGrid 复选框
- IOC入门
Spring六大模块 1.SpringCore spring的核心功能:IOC容器,解决对象的创建及依赖关系 2.SpringWeb spring对Web模块的支持 3.SpringDAO s ...
- Intellij IDEA调试功能总结
public class Demo { public static void f1() { System.out.println("one"); System.out.printl ...