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 ...
随机推荐
- 数据表格控件 DataGridControl
数据表格控件 书154页 <?xml version="1.0" encoding="utf-8"?> <s:Application xmln ...
- spring注解式开发之视图解析器
http://localhost:8089/springmvc-04-viewResovler/springmvc/hello
- Hibernate框架第二天
### Hibernate的持久化类 ### ---------- **什么是持久化类** 1. 持久化类:就是一个Java类(咱们编写的JavaBean),这个Java类与表建立了映射关系就可以成为 ...
- 注解(Annotation)
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是 ...
- /*使用PHP创建一个数组,保存5個员工的信息(ename/sex/salary/birthday/pic)*/
<?php/*使用PHP创建一个数组,保存5個员工的信息(ename/sex/salary/birthday/pic)*/$empList=[ ['ename'=>'张学友','se ...
- Oracle表空间管理相关
以下以我自己的测试环境举例: 1.表空间的 block_size 为 8192字节,即8KBytes.从数据字典中查到 max_size 为 2147483645,即约为15.9TBytes. 2.在 ...
- 去掉idea中竖线
1.现象如下: 2.解决办法. 3.解决后如下:
- Eclipse + Pydev问题 : pydev unresolved import
http://blog.csdn.net/qq_22765745/article/details/71054030http://blog.csdn.net/amghost/article/detail ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- 集合List
//数组 存值长度固定,类型固定 //集合 长度不固定,类型也可以不固定 List<int> list = new List<int>(); //list.Add(78); ; ...