Codeforces 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 //各种细节令人发指!
时间: 2024-08-28 09:31:08

Codeforces E. Number Table的相关文章

Codeforces 346C Number Transformation II 构造

题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<

Codeforces 338D GCD Table 中国剩余定理

题目链接:点击打开链接 给定n*m的矩阵,[i,j]的点值为gcd(i,j) 给定一个k长的序列,问是否能匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n && j<=m 此时输出 YES 对于j j % b[0] = 0 j+1 % b[1] = 0 ··· j+l % b[l] = 0 根据定理:若 a == b (mod n) => (a+c) == b+c (mod n) 所以将上式变换为 j % b[0] = 0 j % b

codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl

Codeforces 577A - Multiplication Table

Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i-th row and j-th column contains numberi?×?j. The rows and columns are numbered starting from 1. You are given a positive integer x. Your task is to c

【打CF,学算法——二星级】CodeForces 237B Young Table (构造)

[CF简介] 提交链接:CF 237B 题面: B. Young Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci

codeforces 582A. GCD Table 解题报告

题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com/blog/entry/20692 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm>

Codeforces 22B Bargaining Table

http://www.codeforces.com/problemset/problem/22/B 题意:求出n*m的方格图中全是0的矩阵的最大周长 思路:枚举 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 int n,m,a[50][50],h[50][50]; 7 char s[500]

Codeforces 251C Number Transformation

Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int,

CodeForces 346C Number Transformation II

Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x[i] -1 ]这段区间的的数都会走到 k * x[i]上. 所以对于每个位置都先计算出他到右边最远的覆盖位置. 然后在反着求出每个位置能往左走走到的最远的位置. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freo