UVa 11542 Square (高斯消元)
题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500。
析:从题目素因子不超过 500,就知道要把每个数进行分解。因为结果要是完全平方数,也就是说每个素因子都得出现偶数次,对于每个数我们用一个 01 向量来表示,对于这个数相应的素因子,如果出现奇数就是 1,否则就是 0,这样就可以得到一些方程,比如举个例子。 4 个整数, 4 6 10 15 ,素因子只有 2 3 5,4 = 2 ^ 2 * 3^0 * 5^0,对于每个素数可以列出一个方程 x2 + x3 = 0 mod 2 x2 + x4 = 0 mod 2 x3 + x4 = 0 mod2,很明显答案就是这些方程的解的个数,而且这个些解都是 0 或者是 1 的组合,换句话说,答案就是这些方程 2^自 由元变量 - 1,减 1 意思是去掉全不选的情况。自由变元怎么求呢,使用高斯消元,不过这次是使用异或,比高斯消元还好写。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 5;
const int maxm = 1e6 + 2;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool vis[maxn];
int prime[maxn], cnt; void init(){
for(int i = 2; i < maxn; ++i) if(!vis[i]){
prime[cnt++] = i;
for(int j = i*i; j < maxn; j += i) vis[j] = 1;
}
} int A[maxn][maxn]; int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d", &n);
ms(A, 0); int mmax = 0;
for(int i = 0; i < n; ++i){
LL x; scanf("%lld", &x);
for(int j = 0; j < cnt; ++j) while(x % prime[j] == 0){
A[j][i] ^= 1; x /= prime[j]; mmax = max(mmax, j);
}
}
int i = 0, j = 0;
while(i <= mmax && j < n){
int r = i;
for(int k = i; k <= mmax; ++k)
if(A[k][j]){ r = k; break; }
if(A[r][j]){
if(r != i) for(int k = i; k <= n; ++k) swap(A[r][k], A[i][k]);
for(int k = i+1; k <= mmax; ++k) if(A[k][j])
for(int l = i; l <= n; ++l) A[k][l] ^= A[i][l];
++i;
}
++j;
}
printf("%lld\n", (1LL<<n-i) - 1);
}
return 0;
}
UVa 11542 Square (高斯消元)的更多相关文章
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- UVA 11542 Square 高斯消元 异或方程组求解
题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...
- UVA11542 Square(高斯消元 异或方程组)
建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...
- UVA 1563 - SETI (高斯消元+逆元)
UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...
- UVA 11542 高斯消元
从数组中选择几个数,要求他们的乘积可以开平方,问有多少种方案. 先将单个数拆分成质因子,对于这个数而言,那些指数为奇数的质因子会使这个数无法被开平方. 所以我们需要选择一个对应质因子指数为奇数的元素, ...
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
- uva 1560 - Extended Lights Out(枚举 | 高斯消元)
题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...
- uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...
- UVA 1397 - The Teacher's Side of Math(高斯消元)
UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...
随机推荐
- git查看历史操作
在提交了若干更新,又或者克隆了某个项目之后,偶尔想回顾下过往提交历史.可以使用git log命令来实现. 最简单的查看提交历史命令如下: $ git log $ git log --oneline $ ...
- mysql怎么查看是否支持分区
mysql从5.1开始支持分区功能 查询命令如下: mysql> show plugins like '%partiotion%'; mysql> show variables like ...
- 因为要生成的折线数量是不定的 ,所以需要echarts 动态的为option中的Series添加数据
series:function(){ var serie=[]; var aa = zhonglei[0].split(","); for( var i=0;i < aa.l ...
- C#生成二维码(可保存二维码图片)
https://www.cnblogs.com/wlays/p/7994393.html 1.NuGet中搜索QRCoder,安装这个插件. 2.创建一个一般处理程序,代码: public void ...
- 序列化、模块 day21
一 序列化 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 字典示例 import json d={'a':1,'b':2} ret = json.dumps(d)# ...
- python 验证码 和进度条
import random def make_code(n): res='' for i in range(n): s1=chr(random.randint(65,90)) s2=str(rando ...
- 6.MySQL图形化工具的使用
6.图形化工具的使用6.1 Mysql Workbench Mysql Workbench是Mysql官方推出的集成图形化工具,替代了之前的图形化管理工具Mysql Administrator和图形化 ...
- git bug修复
在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支i ...
- C# mysql 插入数据,中文乱码
用C#操作mysql时, 插入数据中文都是乱码,只显示问号,数据库本身使用的是utf-8字符. 网上百度一下有两种解决办法: 一种是在执行语句前面设置,如:MySQLCommand mCommand ...
- (转)Oracle 使用 DBLINK详解
DBLINK详解 1.创建dblink语法: CREATE [PUBLIC] DATABASE LINK link CONNECT TO username IDENTIFIED BY password ...