zoj——3624 Count Path Pair
Count Path Pair
Time Limit: 3 Seconds Memory Limit: 65536 KB
You are given four positive integers m,n,p,q(p < m and q < n). There are four points A(0,0),B(p,0),C(m,q),D(m,n). Consider the path f from A to D and path g from B to C. f and g are always towards and parallel to the positive direction of one axis, and they can only change their direction on integer points(whose coordinates are both integers).
You are asked to count the number(mod 100000007) of pair (f,g) that f and g have no intersection.
Input
There are multiple cases(less than 100). Each case is a line containing four integers m,n,p,q(m ≤ 100000 and n ≤ 100000).
Output
For each case, output a single line containing the right answer.
Sample Input
2 2 1 1 3 2 1 1
Sample Output
3 6
题目大意:从A到D与从B到C的路径中不相交的有多少条。
思路:
这道题比较恶心,读题目大约找了2位大佬读,看了大约有1个多小时才搞懂的题意。(ORZ)
唉。这道题我们要求不相交的条数,不过不想交直接求太复杂,所以转化为补集思想:不想交=所有-相交,我们求出相交的,然后用总条数减去相交的条数就好了!
但是问题来了,我们要怎样求总条数与相交的条数呢?!
总路径数是C(M+N,M)*C(Q+M-P,Q)。
然后相交的地方肯定是B与C构成的矩形当中。
我们可以考虑A到C以及B到D,他们的路径必定有相交的,而且相交的位置必定也在矩形当中。
而且相交之后你可以考虑成从A到C的路径改为从交点到D,就样就完成了转化。
结果便是C(M+N,M)*C(Q+M-P,Q)-C(N+M-P,N)*C(M+Q,M);(为什么,背公式!)
然而,这还不够,因为这道题让着取模啊!!!(靠,变态的题目、、、、、、)
没办法,还是要搞一搞的,据学长说在遇到排列组合取模的问题的时候可以求乘法逆元(模数为质数)(还记不记得我们之前在做乘法逆元的题的时候遇到过这样的题,这里粘一下那道题:http://www.cnblogs.com/z360/p/7327322.html)我们这里上公式:
(a/b)mod p=a*c mod p = (a mod p * c mod p)mod p(定义 c为b在mod p意义下的逆元)
这个地方好像可以用卢卡斯定理做、、、、、(然而蒟蒻并不会,做完这道题再学吧、、、、、、、(⊙o⊙)…)
好了,好像这样就差不多了。(然而蒟蒻表示做了一天啊!~~~~(>_<)~~~~)
mdzz 取模的地方写错了,硬是找了一天、、、、、
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000005
#define mod 100000007
#define ll long long
using namespace std;
ll ans,m,n,q,p,jie[N];
ll read()
{
ll x=,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
ll ins(ll n)
{
jie[]=;
;i<n;i++)
jie[i]=(jie[i-]*i)%mod;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
)
{
x=,y=;
return a;
}
ll r=exgcd(b,a%b,x,y),tmp;
tmp=x,x=y,y=tmp-a/b*y;
return r;
}
ll c(ll a,ll b)
{
ll sum;
ll x,y;b=jie[b]%mod*jie[a-b]%mod;
sum=jie[a];
exgcd(b,mod,x,y);
x=(mod+x%mod)%mod;
sum=(sum%mod*x%mod)%mod;
return sum;
}
int main()
{
ins(N);
while(~scanf("%lld",&m))
{
n=read(),p=read(),q=read();
ans=((c(m+n,m)%mod*c((q+m-p),q)%mod)%mod-(c((n+m-p),n)%mod*c((m+q),m)%mod)+mod)%mod;
printf("%lld\n",ans);
}
;
}
zoj——3624 Count Path Pair的更多相关文章
- ZOJ 3624 Count Path Pair 排列组合
思路:在没有限制条件时,很容易知道结果为C(m+n,n)*C(m+q-p,q). 然后再把相交的情况去除就可以了.而如果想到了就是水题了…… 求A->D,B->C相交的情况可以转化为求A- ...
- ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)
ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting s ...
- zoj 1610 Count the Colors
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 Count the Colors Time Limit:2000MS ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- zoj 1610 Count the Colors 【区间覆盖 求染色段】
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
随机推荐
- [Qt Creator 快速入门] 第3章 窗口部件
从这一章开始正式接触Qt的窗口部件.在第2章曾看到 Qt Creator 提供的默认基类只有 QMainWindow.QWidget 和 QDialog 这3种.QMainWindow 是带有菜单栏和 ...
- WIN2012的桌面和开始菜单跑到什么地方去了
传统开始菜单取消了,你把鼠标指针移动到左下角的边缘,会有一个小浮窗,就是开始,再点开始就能进到一个全屏的层里面,跟手机界面,那个玩意就是新一代的开始菜单,程序菜单,你在开始菜单上,鼠标右键点一下,下面 ...
- jquery 菜单展开与收缩参考脚本
/* * metismenu - v1.1.3 * Easy menu jQuery plugin for Twitter Bootstrap 3 * https://github.com/onoku ...
- 【转】mysql INSERT的用法
转自:http://www.cnblogs.com/ggjucheng/archive/2012/11/05/2754938.html insert的语法 INSERT [LOW_PRIORITY | ...
- LN : leetcode 263 Ugly Number
lc 263 Ugly Number lc 263 Ugly Number Write a program to check whether a given number is an ugly num ...
- LR接口测试---socket
前提条件: 编译:javac TcpServer.java 启动:java TcpServer ============================================ 代码示例: # ...
- Microsoft SQL Server学习(六)--查询语句
联合查询 use student --建表 create table class_A( id int primary key, name varchar(50), sex char(50), cour ...
- POJ_3278_Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 54911 Accepted: 17176 ...
- PHP封装数据库
(1)按照步骤封装数据库 ①引入抽象类和抽象方法,即引入模板: ②继承抽象类,注意参数(规定几个就传入几个): ③逐个写入抽象方法,必须一一对应:(抽象方法必须一一引入,否则会报错-->有个抽象 ...
- PHP 加密:Password Hashing API
PHP 5.5 之后引入 Password hashing API 用于创建和校验哈希密码,它属于内核自带,无需进行任何扩展安装和配置.它主要提供了四个函数以供使用: password_hash(): ...