Codeforces 40E Number Table - 组合数学
题目传送门
题目大意
给定一个$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 - 组合数学的更多相关文章
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
- Codeforces #144 (Div. 1) B. Table (组合数学+dp)
题目链接: B.Table 题意: \(n*m\)的矩阵使每个\(n*n\)矩阵里面准确包含\(k\)个点,问你有多少种放法. \((1 ≤ n ≤ 100; n ≤ m ≤ 10^{18}; 0 ≤ ...
- Codeforces - 466C - Number of Ways - 组合数学
https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...
- CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- Codeforces 711D Directed Roads - 组合数学
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
- 【CODEFORCES】 C. Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 651E E. Table Compression(贪心+并查集)
题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...
随机推荐
- vue中检测敏感词,锚点
当发布文章的时候,标题有敏感词 则检测有敏感词的接口成功的时候,写锚点 eg: _this .$alert("检测到标题有敏感词,请修改后再发布", "提示", ...
- java基础(三) -基本数据类型
变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过定义不同类型的变量,可以在内存 ...
- BCB Access violateion at Address 0000 0003. Read of address 0000 0003
来自网页:(我的电脑做不到) 运行一个程序,莫名出现一个对话框:access violation at address 0000.. read of address000试了几次问题依旧,网上搜了下解 ...
- Ecust DIV3 k进制 【暴力不断优化】
K进制 Description 给定一个正整数n,请你判断在哪些进制下n的表示恰好有2位是1,其余位都是0. Input 输入第一行为整数TT,表示有TT组数据(1 \le T \le 50)(1≤T ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- Python -- Pandas介绍及简单实用【转】
转http://www.datadependence.com/2016/05/scientific-python-pandas/ 一. Pandas简介 1.Python Data Analysis ...
- C# 设置按钮快捷键
参考自:http://www.csharpwin.com/csharpspace/3932r8132.shtml 一.C# button快捷键之第一种:Alt + *(按钮快捷键) 在Button按钮 ...
- 360浏览器有个 谷歌访问助手(插件管理里搜谷歌即可) 可以免费访问:谷歌搜索,Google+ gmail
360浏览器有个 谷歌访问助手(插件管理里搜谷歌即可) 可以免费访问:谷歌搜索,Google+ gmail
- python内置函数的简单使用和介绍
"""内置函数的简单使用和介绍参考链接:https://docs.python.org/3/library/functions.html ""&quo ...
- 安装mysql警告 warning: mysql-community-server-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
摘自:https://www.cnblogs.com/royfans/p/7243641.html 红帽安装rpm安装MySQL时爆出警告: 警告:MySQL-server-5.5.46-1.linu ...