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 ...
随机推荐
- 通过nodeSelector配置项实现pod部署至指定node
Node节点添加标签 [root@node1 work]# kubectl label nodes node1 node=master --overwrite node/node1 labeled [ ...
- Mean, Median, Mode, Range, and Standard Deviation
Descriptive statistics tell you about the distribution of data points in data set. The most common m ...
- 一线 | 中国联通宣布首批5G手机到位
腾讯<一线> 作者郭晓峰 据中国联通相关人士今日透露,中国联通用于 5G 友好体验的首批合作 5G 手机全部到位.有 12 个品牌共 15 款 5G 手机及 5G CPE,包括.华为. O ...
- sort排序到底怎么排序
sort()方法 sort() 方法在适当的位置对数组的元素进行排序,并返回数组. <!DOCTYPE html> <html> <head> <meta c ...
- [置顶]
Docker学习总结(7)——云端基于Docker的微服务与持续交付实践
本文根据[2016 全球运维大会•深圳站]现场演讲嘉宾分享内容整理而成 讲师简介 易立 毕业于北京大学,获得学士学位和硕士学位:目前负责阿里云容器技术相关的产品的研发工作. 加入阿里之前,曾在IBM中 ...
- 在Windows Server 2008 R2中删除网桥
How to remove a network bridge in Windows Server 2008 R2 症状: 删除网桥的时候,按理说应该在“网络连接”中选择要被删除的网桥,右键点击,然后选 ...
- Qt之OpenSSL
简述 OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. 简述 下载安装 使用 更多参考 下载安装 ...
- Delphi的时间 x87 fpu control word 精度设置的不够
在win7 64位系统下, 一个DELPHI写的DLL注入一个C语言程序后. 出现非常奇怪的浮点数相加出错的情况. (注: 在XP系统下是正常的).比如: 40725.0001597563 + 0.7 ...
- vs输出窗口,显示build的时间
https://stackoverflow.com/questions/82128/displaying-build-times-in-visual-studio Tools... Options.. ...
- Getting Started with MongoDB (C# Edition)
https://docs.mongodb.com/getting-started/csharp/ 概览 Welcome to the Getting Started with MongoDB guid ...