zoukankan      html  css  js  c++  java
  • 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
    ", (1LL<<n-i) - 1);
      }
      return 0;
    }
    

      

  • 相关阅读:
    2017.8.07
    2017.8.05
    2017.8.04
    2017.8.03
    2017.8.02
    2017.8.01
    2017.7.31
    2017.7.29
    2017.7.28
    简易日历
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8608705.html
Copyright © 2011-2022 走看看