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.然后能够把每一个 ...
随机推荐
- selenimu学习二
1.上传文件 from selenium import webdriver import time import os driver = webdriver.Chrome() src_file = & ...
- 再遇ibatisNet
11年在Mr刘的带领下第一次接触ibatisnet ,当时Mr刘很详细的很讲了xml里的写法还有配置文件之类的,但是随着时间越来越久远.很多东西都开始淡忘了. 如今,再次和它相遇,依然觉得很亲切,虽然 ...
- java 线程Thread 技术--线程状态与同步问题
线程技术第三篇: 线程的状态: 1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源 2.可运行状态:当线程调用start 方法将为线程分配必须的系统资 ...
- SSKeychain
SSKeyChains对苹果安全框架API进行了简单封装,支持对存储在钥匙串中密码.账户进行访问,包括读取.删除和设置.SSKeyChain的作者是大名鼎鼎的SSToolkit的作者samsoffes ...
- Linux系统下重启Tomcat
在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...
- 在Eclipse中安装python插件的方法
一个博士给了我一堆代码,原本以为是C++或者java写的,结果是python,我压根没学过呀,不过本着语言都是相通的原则,我硬着头皮开始学习Python,当然先学习安装IDE(以前学习一门新语言,我会 ...
- js中用变量作为$()内id的值、动态获取id,及获取其下面的class元素
在开发中写了一个公共方法对模板tpl进行渲染,然而他的id是通过变量传值过来的,在网上查阅后找到解决方法,写法如下: $("#"+tplVal).html(html); 用$(&q ...
- Django 前端Wbe框架
Web框架本质及第一个Django实例 Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...
- *jQuery选择器总结(原创:最全、最系统、实例展示)
jquery选择器包括四部分:一.基本选择器二.层次选择器三.过滤选择器四.表单元素选择器 一.基本选择器1.ID选择器:$('#myDiv');2.类选择器:$('.className');3.元素 ...
- ATM作业
关于ATM作业,最近做了很久,才明白,其实看了很久的作业视频讲解,到不如将作业的整个下载下来进行运行,去了解程序本身的结构和运行方式.首先说需求,就感觉是各种懵逼,这才学了函数,和模块之间的简单调用就 ...