题目传送门

  传送门I

  传送门II

题目大意

  给定一个$n\times m$的网格,每个格子上要么填$1$,要么填$-1$,有$k$个位置上的数是已经填好的,其他位置都是空的。问有多少种填法使得任意一行或一列上的数的乘积为$-1$.

  $1 \leqslant n, m \leqslant 10^{3}$,$1 \leqslant k < \max (n, m)$。

  $k$的范围醒目。那么意味着至少存在一行或者一列为空。

  假设空的是一行。那么剩下的行只需要满足那一行的乘积为$-1$,而空的这一行对应一种唯一的填法。

  可以计算出,空行补数后的乘积为$(-1)^{m}\times (-1)^{n - 1}$,即$(-1)^{m + n - 1}$。

  所以特判$m. n$奇偶性不同的时候无解。然后就可以将每一行单独计算。

  每一行中,要么只填奇数个$-1$,要么只填偶数个$-1$。这样就可以$O(nm)$的时间内解决这道题目。

  但是这不能满足装逼爱好者的欲望。明明这东西可以做到O(n)。

定理1 当$n > 0$时,满足$\sum_{k = 0}^{n}[2 \mid k]C_{n}^{k} = \sum_{k = 0} ^{n}[2 \nmid k]C_{n}^{k} = 2^{n - 1}$。

  证明 当$n$为奇数时,根据式子$C_{n}^{k} = C_{n}^{n - k}$易证。

  当$n$为偶数时,根据杨辉恒等式$C_{n}^{k} = C_{n - 1}^{k - 1} + C_{n - 1}^{k}$可得偶数位的和等于第$n - 1$层的和。

  根据杨辉三角的性质,我们知道第$n - 1$层的和是$2^{n - 1}$,第$n$层的和是$2^{n}$。

  所以第$n$层奇数位的和是$2^{n} - 2^{n - 1} = 2^{n - 1}$。

  因此定理得证。

  然后预处理2的幂,就可以做到$O(n)$了。

  (另外提一句,即使没有 $k$ 那个限制,可以做到 $O(n + k)$)

Code

 /**
* Codeforces
* Problem#40E
* Accepted
* Time: 60ms
* Memory: 2160k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; const int N = ; int n, m, k, p;
boolean aflag;
int pow2[N];
int cnt[N], pro[N]; inline void init() {
scanf("%d%d", &n, &m);
scanf("%d", &k);
if (n < m) swap(n, m), aflag = true;
fill(pro + , pro + n + , );
for (int i = , u, v, x; i <= k; i++) {
scanf("%d%d%d", &u, &v, &x);
if (aflag) swap(u, v);
cnt[u]++, pro[u] *= x;
}
scanf("%d", &p);
} inline void solve() {
if ((n & ) != (m & )) {
puts("");
return;
} pow2[] = ;
for (int i = ; i <= n; i++)
pow2[i] = (pow2[i - ] << ) % p; for (int i = ; i < n; i++)
if (!cnt[i]) {
swap(cnt[i], cnt[n]);
swap(pro[i], pro[n]);
break;
} int ans = ;
for (int i = ; i < n && ans; i++) {
if (cnt[i] == m) {
if (pro[i] == )
ans = ;
} else
ans = ans * 1ll * pow2[m - cnt[i] - ] % p;
}
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}

Codeforces 40E Number Table - 组合数学的更多相关文章

  1. Codeforces 417E Square Table(随机算法)

    题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...

  2. Codeforces 40 E. Number Table

    题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...

  3. Codeforces #144 (Div. 1) B. Table (组合数学+dp)

    题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...

  4. Codeforces - 466C - Number of Ways - 组合数学

    https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...

  5. CodeForces 1099E - Nice table - [好题]

    题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...

  6. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  7. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  8. 【CODEFORCES】 C. Table Decorations

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. 延期版本webstorm(解决许可证过期,注册,激活,破解,码,支持正版,最新可用)

    很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 选择 License server (20 ...

  2. chrome\IE\Firefox驱动下载地址

    安装三大浏览器驱动driver 1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list 2.Firefox的 ...

  3. mysql基础常用命令

    数据库 1查询 Select * From table select host,user,password from mysql.user where user='ybb' and host='%'; ...

  4. jQuery-淡入淡出效果-fadeIn()淡进 fadeOut()淡出 fadeToggle();

    动画高级函数:基于底层函数又进行了封装两大块:简化版的动画函数和万能动画函数简化版动画函数显示/隐藏$().show;  $(...).hide();  强调:无参数的show()/hide()使用的 ...

  5. JavaScript-switch-case-电话系统

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. CentOS下Docker安装

    CentOS下Docker安装 1.安装: #sudo yum install docker 2.启动并加入开机启动: A.#sudo systemctl start docker B.#sudo s ...

  7. MongoDB Driver:使用正确的姿势连接复制集

    from:https://yq.aliyun.com/articles/8461?spm=5176.7937264.222114.10.s2oqcT   摘要: MongoDB复制集(Replica ...

  8. msyql 移动某一列数据到某列 & 字段加前缀

    #移动数据 UPDATE dcs_organize_user AS a, dcs_organize_user AS b SET a.SHORTTELNO=b.USERTELNO WHERE a.id= ...

  9. activiti_change_ok

    package com.xinwei.process.controller; import java.util.Calendar; import java.util.HashMap; import j ...

  10. Gambler Bo (高斯消元求特解)

    对于图中的每一个点假设点击Xi * m + j 然后每个点都有那么对于每一个点可以列举出一个方程式,n*m个点解n*m个未知数.利用高斯消元就可以解决. 问题就在这个题目可能不止有一个特,所以我们需要 ...