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 ...
随机推荐
- cocos2d-x JS 富文本(为一段文本中的个别字体上颜色)
setWinText : function (levelStr1,levelStr2,levelStr3,color1,color2) { var imgRankingBG = this.contai ...
- 用log4net记录日志信息
在.net中用log4net记录日志信息,已经是很平常的事情了. log4net下载:http://logging.apache.org/log4net/download_log4net.cgi 百度 ...
- 递归n!
package sushudigui; import java.util.Scanner; public class digui { public static void main(String[] ...
- HDU 2256 Problem of Precision(矩阵)
Problem of Precision [题目链接]Problem of Precision [题目类型]矩阵 &题解: 参考:点这里 这题做的好玄啊,最后要添加一项,之后约等于,但是有do ...
- JavaScript 创建和浅析自定义对象
在Js中,除了Array.Date.Number等内置对象外,开发者可以通过Js代码创建自己的对象. 目录 1. 对象特性:描述对象的特性 2. 创建对象方式:对象直接量.new 构造函数.Objec ...
- 学习笔记<2>Android基本四大组件
<1>Activity(活动) ------与用户交互的界面 (1)Activity启动基本流程 <2>service(服务) <3>ContentProvider ...
- ubuntu安装启动redis
1.下载安装 sudo apt-get install build-essential wget http://redis.googlecode.com/files/redis-2.2.13.ta ...
- Java基础(basis)-----抽象类和接口详解
1.抽象类 1.1 abstract修饰类:抽象类 不可被实例化 抽象类有构造器 (凡是类都有构造器) 抽象方法所在的类,一定是抽象类 抽象类中可以没有抽象方法 1.2 abstract修饰方法:抽象 ...
- c++学习笔记(二)-指针
1. 指向数组的指针 int balance[5] = { 1000, 2, 3, 17, 50 }; int *ptr; ptr = balance; //ptr是指向数组balance的指针 // ...
- 抓取biqukan
#python3.7 ''' 功能:实现www.biqukan.com/1_1094/5403177.html小说下载为txtv1.0 ''' import requests,sys,time fro ...