题目传送门

  传送门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. Django Form(表单)

    前台用 get 或 post 方法向后台提交一些数据. GET方法: html示例(保存在templates文件夹中): <!DOCTYPE html> <html> < ...

  2. hibernate中configuration和配置文件笔记

    hibernate的核心类和接口 Configuration类 作用:(1)读取hibernate.cfg.xml文件 (2)管理对象关系映射文件<mapping resource=" ...

  3. 合并dict、list的方法

    dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}合并两个字典得到类似 {1:[1,11,111],2:[2,22,2 ...

  4. 从caffemodel里面导出参数

    参见博文https://blog.csdn.net/u014510375/article/details/51704447

  5. QT5中QByteArray转QString中文乱码

    1.添加头文件 #include <QTextCodec> 2.用QTextCodec 设置格式转换 QByteArray barr; barr.insert(0,(char*)(pMsg ...

  6. MySql 应用语句

    [1]MySQL基础语句 -- 查询mysql版本号 SELECT VERSION(); -- 创建数据库 DROP DATABASE IF EXISTS study; -- 如果存在先删除 CREA ...

  7. 查看完整的 Unicode 字符集

    https://unicode-table.com/cn/ 这个链接是我想要查的 格式如下图 先放这里收藏,我也不知道怎么搜索

  8. Jersey入门——对Json的支持

    Jersey rest接口对POJO的支持如下: package com.coshaho.learn.jersey; import java.net.URI; import javax.ws.rs.C ...

  9. 设计模式之State(状态)(转)

    State的定义: 不同的状态,不同的行为;或者说,每个状态有着相应的行为. 何时使用? State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif ...

  10. Linux上查看大文件的开头几行内容以及结尾几行的内容

    head -n 50 filePath 查看开头50行的内容 tail -n 50 filePath 查看文件结尾50行的内容