zoukankan      html  css  js  c++  java
  • Codeforces 40 E. Number Table

    题目链接:http://codeforces.com/problemset/problem/40/E


    妙啊...

      因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的,那么除了这一行或者这一列的格子,其余的格子随意填,只要满足了当且对应的行(列)的积是$-1$就好了,用组合数算一算就好了,剩下的空着的一行或者一列用于收尾,可以发现它当且仅有一种放法。

      考虑无解:如果$n+m$为奇数,同时还要注意一下如果$n=1$,或者$m=1$的情况

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<cstring>
     8 using namespace std;
     9 #define maxn 2010
    10 #define llg long long 
    11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    12 llg n,m,k,ans,md;
    13 llg c[maxn][maxn],a[maxn][maxn],d[maxn][maxn];
    14 bool pd=true;
    15 inline int getint()
    16 {
    17        int w=0,q=0; char c=getchar();
    18        while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    19        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    20 }
    21 
    22 llg check(llg x)
    23 {
    24     llg sum=0,V=1;
    25     for (llg i=1;i<=m;i++) sum+=(a[x][i]!=0),V*=a[x][i];
    26     if (sum==m && V==1) pd=false;
    27     return sum;
    28 }
    29 
    30 int main()
    31 {
    32     yyj("Number");
    33     cin>>n>>m;
    34     if ((n+m)&1) {cout<<0; return 0;}
    35     cin>>k;
    36     for (llg i=1;i<=k;i++)
    37     {
    38         llg x=getint(),y=getint();
    39         a[x][y]=getint();
    40     }
    41     if (n<m)
    42     {
    43         for (llg i=1;i<=n;i++)
    44             for (llg j=1;j<=m;j++)
    45                 d[j][i]=a[i][j];
    46         swap(n,m);
    47         for (llg i=1;i<=n;i++)
    48             for (llg j=1;j<=m;j++)
    49                 a[i][j]=d[i][j];
    50     }
    51     cin>>md;
    52     c[1][0]=c[1][1]=1;
    53     for (llg i=2;i<maxn;i++)
    54     {
    55         c[i][0]=1;
    56         for (llg j=1;j<maxn;j++)
    57             c[i][j]+=c[i-1][j]+c[i-1][j-1],c[i][j]%=md;
    58     }
    59     bool xian=0;
    60     ans=1;
    61     for (llg i=1;i<=n;i++)    
    62     {
    63         llg s=check(i);
    64         if (s==0 && !xian) xian=1;
    65         else
    66         {
    67             llg kong=0,vz=0,vf=0,tot=0;
    68             for (llg k=1;k<=m;k++) kong+=(a[i][k]==0),vz+=(a[i][k]==1),vf+=(a[i][k]==-1);
    69             if (!kong) continue;
    70             for (llg k=0;k<=kong;k++) 
    71             {
    72                 if ((vf+k)%2) 
    73                     tot+=c[kong][k],tot%=md;
    74             }
    75             ans*=tot;ans%=md;
    76         }
    77     }
    78     for (llg i=1;i<=m;i++)
    79     {
    80         llg V=1;
    81         llg sum=0;
    82         for (llg j=1;j<=n;j++) sum+=(a[j][i]==0),V*=a[j][i];
    83         if (V==1 && sum==0) pd=false;
    84     }
    85     if (!pd) {cout<<0; return 0;}
    86     cout<<ans;
    87     return 0;
    88 }
    89 //各种细节令人发指!
  • 相关阅读:
    Groovy 设计模式 -- null对象模式
    Groovy 设计模式 -- 借贷
    Groovy 设计模式 -- 抽象工厂 模式
    Groovy 设计模式 -- Strategy 模式
    Groovy 设计模式 -- proxy & delegate
    Groovy 类名称赋值为变量使用(newInstance & new)
    yaml
    REST POST PUT差别
    Continuous Design
    通配符 Globbing赏析
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6675564.html
Copyright © 2011-2022 走看看