【HDU 5833】Zhu and 772002(异或方程组高斯消元讲解)
题目大意:给出n个数字a[],将a[]分解为质因子(保证分解所得的质因子不大于2000),任选一个或多个质因子,使其乘积为完全平方数。求其方法数。
学长学姐们比赛时做的,当时我一脸懵逼的不会搞……所以第二天上午花了一上午学习了一下线性代数。
题目思路:
任选一个或多个质因子,起乘积为完全数m,因为组成它的均为素数,假设组成m的素数的种类为n,那么这n类素数中每类素数的个数应为偶数。
可设:a[i][j]=0代表第i种素数可在a[j]中分离出的个数为偶数,a[i][j]=1代表第i种素数可在a[j]中分离出的个数为奇数数。
b[i]=1代表选择这类素数,b[i]=0代表不选择这类素数。
列出线性方程组:
a11x1+a12x2+...+a1nxn=0
a21x1+a22x2+...+a2nxn=0
...
an1x1+an2x2+...+annxn=0
求解的个数 ans
转化为矩阵形式:
矩阵A=
a11 a12 a13 …………a1n
a21 a22 a23 …………a2n
……………………………………
……………………………………
an1 an2 an3 …………ann
通过初等变换可将矩阵A换成类似下面矩阵B的形式
a11 a12 a13 ……a1n
0 a22 a23 ……a2n
0 0 a33……a3n
0 0 0 ……arn
再将矩阵B转化成线性方程组 可求出最后一组方程的解,倒着往回求可求出所有方程解
可解出的方程组共 r 个,由秩的定义可知 r等矩阵A的秩
由定理:
对于n元齐次线性方程组如果r<n,则方程组含n-r个自由未知量。
解的个数ans=2^(n-r)-1(减去全0解)
具体操作:
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f
#define MAX 2105
#define MOD 1000000007 using namespace std; long long c[MAX][],p[MAX],v[MAX],a[MAX],cnt,n;//c存矩阵,p存素数表,cnt代表素数的个数 void MakeTab()//打素数表
{
int i,j;
memset(v,,sizeof(v));
memset(p,,sizeof(p));
cnt=;
for(i=; i<=; i++)
{
if(!v[i])
{
p[++cnt]=i;
for(j=i; j<=; j+=i)
{
v[j]=;
}
}
}
} int Rank()//计算秩
{
int i,j,k,r,u;
i=;
j=;
while(i<=cnt && j<=n)
{
r=i;
while(!c[r][j] && r<=cnt)
r++;
if(c[r][j])
{
swap(c[i],c[r]);//如果发现了第r行第j列为1,就讲r行和i行行互换(初等行变换)
for(u=i+; u<=cnt; u++)
{
if(c[u][j])
{
for(k=i; k<=n; k++)
{
c[u][k]=c[u][k]^c[i][k];//每找到一个未知数就对其进行亦或处理,去掉系数c[i][k]
} }
}
i++;
}
j++;
}
return i;
} int main()
{
MakeTab();
int i,j,cns=,T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
memset(c,,sizeof(c));
for(i=; i<=n; i++)
{
scanf("%lld",&a[i]);
} for(i=; i<=n; i++)
{
for(j=; j<=cnt; j++)
{
long long num=a[i];
if(num%p[j]==)
{
while(num%p[j]==)
{
num/=p[j];
c[j][i]=c[j][i]^;
}
}
}
} long long k=(n-Rank());
long long ans=;
for(i=; i<=k; i++)
ans=(ans*)%MOD;
printf("Case #%d:\n",cns++);
printf("%lld\n",ans-);//去掉全0的解
}
return ;
}
【HDU 5833】Zhu and 772002(异或方程组高斯消元讲解)的更多相关文章
- hdu 5833 Zhu and 772002 异或方程组高斯消元
ccpc网赛卡住的一道题 蓝书上的原题 但是当时没看过蓝书 今天又找出来看看 其实也不是特别懂 但比以前是了解了一点了 主要还是要想到构造异或方程组 异或方程组的消元只需要xor就好搞了 数学真的是硬 ...
- 【HDU 5833】Zhu and 772002(异或方程组高斯消元)
300个最大质因数小于2000的数,选若干个它们的乘积为完全平方数有多少种方案. 合法方案的每个数的质因数的个数的奇偶值异或起来为0. 比如12=2^2*3,对应的奇偶值为01(2的个数是偶数为0,3 ...
- 3364 Lanterns (异或方程组高斯消元)
基本思路.首先构造一个n*(m+1)的矩阵,同时标记一个行数row,row从零开始,然后找出每一列第一个非零的数,和第row行互换, 然后对row到n行,异或运算.最终的结果为2^(m-row) #i ...
- hdu 5833 Zhu and 772002 ccpc网络赛 高斯消元法
传送门:hdu 5833 Zhu and 772002 题意:给n个数,每个数的素数因子不大于2000,让你从其中选则大于等于1个数相乘之后的结果为完全平方数 思路: 小于等于2000的素数一共也只有 ...
- HDU 5833 Zhu and 772002
HDU 5833 Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5833 Zhu and 772002 (高斯消元)
Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...
- hdu 5833 Zhu and 772002 高斯消元
Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...
- HDU 2262 Where is the canteen 期望dp+高斯消元
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2262 Where is the canteen Time Limit: 10000/5000 MS ...
- 【HDU 3949】 XOR (线性基,高斯消元)
XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- hdu_5883_The Best Path(欧拉路)
题目链接:hdu_5883_The Best Path 题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 题解: 节点 i 的贡献为((du[i] +1/ 2 ...
- JS的一些常见验证代码
1//檢查空串 2function isEmpty(str){ 3 if((str == null)||(str.length == 0)) return (true); 4 else retu ...
- mac 上面安装mysql-python
安装过程中一直报错: EnvironmentError: mysql_config not found 最终下面的方式解决: 58down voteaccepted +200 Ok, well, fi ...
- Unity3D 回合制 网上源码 目前还在研究构思
我们已将回合制的战斗模式讲解得很清楚了.那么,如果在Unity3D游戏中实现一个回合制游戏呢?我们从最简单的一对一模式来设计回合制游戏的原型.我们可以游戏的状态划分为下面三种状态: 1. ...
- Sersync同步过滤.svn文件夹
Sersync同步过滤.svn文件夹 <filter start="true"> <exclude expression="(.*).svn(.*)&q ...
- Mac 命令行中进入带有空格的文件夹
http://blog.sina.com.cn/s/blog_5e8392b10100jkvg.html 今天在公司用mac的时候,有个文件夹的名字有空格,怎么都进不去,在网上一查原来不能直接cd 文 ...
- mysql5.7.16安装
系统:centOS6.5 mysql: 5.7.16 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glib ...
- Alyona and a tree
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Docker: Usage instruction
Install docker from official site, in windows. or install docker from repo as official site told, in ...
- Boxes in a Line(移动盒子)
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...