Gym - 100625F Count Ways 快速幂+容斥原理
题意:n*m的格子,中间有若干点不能走,问从左上角到右下角有多少种走法。
思路:CountWay(i,j) 表示从 i 点到 j 点的种数。然后用容斥原理加加减减解决
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define MAXN 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
struct Node
{
LL x, y;
};
Node p[MAXN];
LL factor[ * MAXN], w[ * MAXN];
LL res[MAXN];
bool compare(Node a, Node b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
LL CountWay(LL x, LL y)
{
LL res = factor[x + y];
res = res * w[x] % MOD;
res = res * w[y] % MOD;
return res;
}
LL quick_power(LL x, LL y)
{
if (y == ){
return (LL);
}
if (y == ){
return x % MOD;
}
LL res = quick_power(x, y >> );
res = (res * res) % MOD;
if (y & ){
res = (res * x) % MOD;
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
factor[] = ;
w[] = ;
for (LL i = ; i <= ; i++){
factor[i] = (factor[i - ] * i) % MOD;
w[i] = quick_power(factor[i], MOD - );
}
scanf("%d", &T);
LL n, m, k;
while (T--){
scanf("%I64d%I64d%I64d", &n, &m, &k);
for (int i = ; i <= k; i++){
scanf("%I64d%I64d", &p[i].x, &p[i].y);
}
p[k + ].x = n;
p[k + ].y = m;
sort(p + , p + k + , compare);
for (int i = ; i <= k + ; i++){
res[i] = CountWay(p[i].x - , p[i].y - );
}
for (int i = ; i <= k + ; i++){
for (int j = i + ; j <= k + ; j++){
if (p[j].x < p[i].x || p[j].y < p[i].y) continue;
//if(p[j].x == p[i].x && p[j].y == p[i].y) continue;
res[j] = (res[j] - (res[i] * CountWay(p[j].x - p[i].x, p[j].y - p[i].y)) % MOD) % MOD;
}
}
printf("%I64d\n", (res[k + ] + MOD) % MOD);
}
}
Gym - 100625F Count Ways 快速幂+容斥原理的更多相关文章
- 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)
Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...
- 广工十四届校赛 count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...
- hdu6470 Count 矩阵快速幂
hdu6470 Count #include <bits/stdc++.h> using namespace std; typedef long long ll; , mod = ; st ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...
- UVALive 7040 Color (容斥原理+逆元+组合数+费马小定理+快速幂)
题目:传送门. 题意:t组数据,每组给定n,m,k.有n个格子,m种颜色,要求把每个格子涂上颜色且正好适用k种颜色且相邻的格子颜色不同,求一共有多少种方案,结果对1e9+7取余. 题解: 首先可以将m ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others) ...
- HDU2157 How many ways??---(邻接矩阵,图论,矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=2157 How many ways?? Time Limit: 2000/1000 MS (Java/Others ...
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
随机推荐
- Python学习笔记(二):字符串类型
在上一篇随笔(https://www.cnblogs.com/g-qiang/p/10448813.html)中,说到 Python 有六种标准数据类型,而数字类型和字符串类型又是其中基本的数据类型. ...
- 题解 P3372 【【模板】线段树1 】(zkw)
看了一下题解里的zkw线段树,感觉讲的不是很清楚啊(可能有清楚的但是我没翻到,望大佬勿怪). 决定自己写一篇...希望大家能看明白... zkw线段树是一种优秀的非递归线段树,速度比普通线段树快两道三 ...
- C#-反射知识点
1.反射是基于Sytem.Type的,里面的很多方法是system.reflection里面的 MethodInfo[] tt= t.GetType().GetMethods(); //t是strin ...
- poj 2079 Triangle,旋转卡壳求点集的最大三角形
给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,由于题目n比較大,枚举的话要O(n^3)的数量级,所以採用旋转卡壳的做法: 首先枚举三 ...
- 【linux驱动分析】misc设备驱动
misc设备驱动.又称混杂设备驱动. misc设备驱动共享一个设备驱动号MISC_MAJOR.它在include\linux\major.h中定义: #define MISC_MAJO ...
- Unity 3D本地公布WebPlayer版时"Failed to download data file"解决方式
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzZW55YW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- Hdu4786
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 基于深度学习的病毒检测技术无需沙箱环境,直接将样本文件转换为二维图片,进而应用改造后的卷积神经网络 Inception V4 进行训练和检测
话题 3: 基于深度学习的二进制恶意样本检测 分享主题:全球正在经历一场由科技驱动的数字化转型,传统技术已经不能适应病毒数量飞速增长的发展态势.而基于沙箱的检测方案无法满足 APT 攻击的检测需求,也 ...
- nyoj--218--Dinner(语法)
Dinner 时间限制:100 ms | 内存限制:65535 KB 难度:1 描述 Little A is one member of ACM team. He had just won the ...
- Filenames and paths
Files are organized into directories (also called ‘folders’). Every running program has a ‘current d ...