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 ...
随机推荐
- vue+element-ui+slot-scope或原生实现可编辑表格(日历)
你们公司的产品是不是还在做一个可编辑表格功能? 1.前言 咱开发拿到需求大多数是去网上找成型的组件,找不到再看原生的方法能否实现,大牛除外哈,大牛一般喜欢封装组件框架. 2.思路 可编辑表格在后台管理 ...
- svn文件管理器的使用
服务器端: 客户端 使用SVN的注意事项 做任何操作之前,先update一下 不要修改其他人的文件 不要在SVN里直接打开.编辑文件 不要在打开.编辑文件的时候,进行操作 SVN客户端的安装,非常简单 ...
- Cannot set web app root system property when WAR file is not expanded
Cannot set web app root system property when WAR file is not expanded 在tomcat下面可以,在weblogic下面不行的处理方法 ...
- Ubuntu设置显示桌面快捷键
Ubuntu设置显示桌面快捷键 直接在系统设置中没有效果, 学习了:http://www.cnblogs.com/pluse/p/5286585.html 需要进行安装compizconfig,然后在 ...
- leetCode(38):Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- OpenCASCADE直线与平面求交
OpenCASCADE直线与平面求交 在<解析几何>相关的书中都给出了直线和平面的一般方程和参数方程.其中直线的一般方程有点向式形式的. 由于过空间一点可作且只能作一条直线平行于已知直线, ...
- iOS CST NSDate
好像是从ios4.1開始[NSDate date];获取的是GMT时间,这个时间和北京时间相差8个小时.下面代码能够解决问题 - (void)tDate { NSDate *date = [NSDat ...
- DNS解析污染原理——要么修改包,要么直接丢弃你的网络包
DNS/域名解析 可以看到dns解析是最初的一步,也是最重要的一步.比如访问亲友,要知道他的正确的住址,才能正确地上门拜访. dns有两种协议,一种是UDP(默认),一种是TCP. udp 方式,先回 ...
- POJ 2110 二分+暴搜
题意: 给你一个矩阵 ,你能往各个方向走(不走出去就行),每次只能上下左右走一格,问路径上的点权最大值和最小值的差最小是多少. 思路: 首先 二分最后的答案, 暴力枚举当前的区间是啥. DFS 就OK ...
- ListView和GridView的setOnScrollListener的简介
---恢复内容开始--- 设置ListView和GridView的滑动监听 circle_lv.setOnScrollListener(new AbsListView.OnScrollListener ...