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 ...
随机推荐
- [转] 解决RegexKitLite编译报错
本文永久地址为http://www.cnblogs.com/ChenYilong/p/3984254.html ,转载请注明出处. 在编译RegexKitLite的时候,报错如下: Undefined ...
- 记关于webpack4下css提取打包去重复的那些事
注意使用vue-cli3(webpack4),默认小于30k不会抽取为公共文件,包括css和js,已测试 经过2天的填坑,现在终于有点成果 环境webpack4.6 + html-webpack-pl ...
- oracle 建用户
create user username identified by password; grant dba to username; 注意当对用户赋予resource角色时将同时赋予unlimite ...
- 过滤掉文本中的javascript标签代码
2014年1月21日 11:51:19 php代码: $content = preg_replace('#<\s*[script].*>#', '', $a);//有些攻击可以在scrip ...
- [原创]jQuery Validation范例
上班无事,学习jQuery Validation,于是手写一公共范例,并收藏以便后用 验证操作类formValidatorClass.js }); 测试页index.html * {} ...
- selenium玩转svg操作
今天写脚本发现页面有svg结构,里面的元素无法定位,查找很多资料,然后就记录下来 初步尝试直接在页面中获取svg中包含元素的xpath,直接利用selenium方法访问,无法捕获到相关元素信息. SV ...
- Mybatis 接口传入多个参数 xml怎么接收
mybatis 在接口上传入多个参数 1.如果传入的参数类型一样. Map<String, String> queryDkpayBindBankCidByOriBindAndBankCid ...
- 常用RAID级别的介绍
随时科技的进步,各种各样的技术也层出不穷,当然RAID的组合也一样,嘻嘻,下面跟大家一起来学习下常用的RAID RAID的全称廉价磁盘冗余阵列(Redundant Array of Inexpensi ...
- VC++一些开发心得与调试技巧
1.如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框,选择Release状态.C/C++标签中的Category选Gen ...
- day10--协成\异步IO\缓存
协成(Gevent) 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.CPU只认识线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将 ...