zoukankan      html  css  js  c++  java
  • BZOJ3503: [Cqoi2014]和谐矩阵

    题解:

    如果第一行的数知道了,我们就可以推出其他行的数。

    那么如何判断第一行的数的一种填法是否合法呢?很简单,我们递推出m+1行的数,当且仅当这一行都是0时满足题意。

    那么,我们就有了一种想法。

    直接把m+1行的每个数用x[1..n]表示出来,这一定是个系数只为0/1的式子。然后让这个异或值=0,就可以解异或方程组了。

    系数怎么推呢?

    for1(i,n)b[1][i]=(ll)1<<i-1;
        for2(i,2,m+1)
         for1(j,n)
          b[i][j]=b[i-1][j]^b[i-1][j-1]^b[i-1][j+1]^b[i-2][j];

    然后解方程就可以了。

    代码:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 50+5
    14 #define maxm 100000+5
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
    23 #define mod 1000000007
    24 using namespace std;
    25 inline int read()
    26 {
    27     int x=0,f=1;char ch=getchar();
    28     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    29     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    30     return x*f;
    31 }
    32 int n,m;
    33 ll a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
    34 inline void gauss()
    35 {
    36     for1(i,n)
    37     {
    38         int k=i;
    39         while(k<=n&&!a[k][i])k++;
    40         if(k>n)continue;
    41         for2(j,i,n+1)swap(a[i][j],a[k][j]);
    42         for2(j,i+1,n)if(a[j][i])
    43          for2(k,i,n+1) 
    44           a[j][k]^=a[i][k];
    45     }
    46     for3(i,n,1)
    47     {
    48         c[1][i]=a[i][n+1];
    49         if(!a[i][i]){c[1][i]=1;continue;}
    50         for2(j,i+1,n)if(a[i][j])c[1][i]^=c[1][j];
    51     }
    52 }
    53 int main()
    54 {
    55     freopen("input.txt","r",stdin);
    56     freopen("output.txt","w",stdout);
    57     m=read();n=read();
    58     for1(i,n)b[1][i]=(ll)1<<i-1;
    59     for2(i,2,m+1)
    60      for1(j,n)
    61       b[i][j]=b[i-1][j]^b[i-1][j-1]^b[i-1][j+1]^b[i-2][j];
    62     for1(i,n)
    63      for1(j,n)
    64       a[i][j]=b[m+1][i]>>(j-1)&1; 
    65     gauss();
    66     for2(i,2,m)
    67      for1(j,n)
    68       c[i][j]=c[i-1][j]^c[i-1][j-1]^c[i-1][j+1]^c[i-2][j];
    69     for1(i,m){for1(j,n-1)printf("%d ",c[i][j]);printf("%d
    ",c[i][n]);}
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    MySQL-基本sql命令
    Java for LeetCode 203 Remove Linked List Elements
    Java for LeetCode 202 Happy Number
    Java for LeetCode 201 Bitwise AND of Numbers Range
    Java for LeetCode 200 Number of Islands
    Java for LeetCode 199 Binary Tree Right Side View
    Java for LeetCode 198 House Robber
    Java for LeetCode 191 Number of 1 Bits
    Java for LeetCode 190 Reverse Bits
    Java for LeetCode 189 Rotate Array
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4231473.html
Copyright © 2011-2022 走看看