问题 E: Massage

时间限制: 1 Sec  内存限制: 64 MB

题目描述

JSZKC  feels  so  bored  in  the  classroom  that  he  wants  to  send  massages  to  his  girl  friend. However, he can’t move to his girl friend by himself. So he has to ask his classmates for help. 
The classroom is a table of size N × M. We'll consider the table rows numbered from top to bottom 1 through N, and the columns numbered from left to right 1 through M. Then we'll denote the cell in row x and column y as (x, y). And every cell sits a student. 
Initially JSZKC sits on the cell (1, 1) . And his girl friend sits on cell (n, m). A message can go from  cell (x, y) to  one  of  two  cells (x + 1, y) and (x, y + 1).  JSZKC  doesn’t  want  to  trouble  his classmates too much. So his classmates(not including his girl friend) may not take massages more than once. It’s obvious that he can only send out two massages. Please help JSZKC find the number of ways in which the two massages can go from cell (1, 1) to cell (n, m). 
More  formally,  find  the  number  of  pairs  of  non-intersecting  ways  from  cell (1, 1) to cell (n, m) modulo 1000000007 .  Two  ways  are  called  non-intersecting  if  they  have  exactly  two common points — the starting point and the final point. 

输入

The input file contains several test cases, each of them as described below. 
The first line of the input contains one integers N (2 ≤ N,M≤ 1000), giving the number of rows and columns in the classroom. 
There are no more than 100 test cases. 

输出

One line per case, an integer indicates the answer mod 1000000007. 

样例输入

2 2
2 3
3 3

样例输出

1
1
3

meaning

从(1,1)到(n,m)只能向(x+1,y)和(x,y+1)走,问有多少种方案走两条不相交的路。

solution

又是一道想到了就很水的题

要求走两条路且不相交,可以将起点看作A1(1,2)和A2(2,1),终点看作B1(n-1,m)和B2(n,m-1)。

设S(A,B)表示从A到B的方案数。(C(n+m-2,n-1),n,m为矩形长宽)

那么由容斥,ans = S(A1,B1)*S(A2,B2)-S(A1,B2)*S(A2,B1) (因为只要路径相交,从A1就可以到B2,从A2就可以到B1)

code

#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 200005;
ll fac[maxn],MOD = 1e9+7; void init() {
int i;
fac[0] =1;
for(i =1; i <= (int)2e5+10; i++) //modify
fac[i] = fac[i-1]*i % MOD;
} ll fastpow(ll a, ll b) {
ll tmp = a % MOD, ans =1;
while(b) {
if(b &1)
ans = ans * tmp % MOD;
tmp = tmp*tmp % MOD;
b >>=1;
}
return ans;
} ll comb(ll n, ll m) {
return m>n ? 0 : fac[n]*fastpow(fac[m]*fac[n-m], MOD-2) % MOD;
} ll Lucas(ll n, ll m) {
return m ? (comb(n%MOD, m%MOD)*Lucas(n/MOD, m/MOD))%MOD : 1;
} int main() {
// IN_LB();
init();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
cout<<(Lucas(n+m-4,n-2)*Lucas(n+m-4,n-2)%MOD-Lucas(n+m-4,n-1)*Lucas(n+m-4,m-1)%MOD+MOD)%MOD<<endl;
}
return 0;
}

【容斥+组合数】Massage @2018acm徐州邀请赛 E的更多相关文章

  1. [AHOI2015 Junior] [Vijos P1943] 上学路上 【容斥+组合数】

    题目链接:Vijos - P1943 题目分析 这是 AHOI 普及组的题目,然而我并不会做= =弱到不行= = 首先,从 (x, 0) 到 (0, y) 的最短路,一定是只能向左走和向上走,那么用组 ...

  2. [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】

    题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...

  3. 【BZOJ4665】小w的喜糖 容斥+组合数

    [BZOJ4665]小w的喜糖 Description 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那 ...

  4. [BZOJ3027][Ceoi2004]Sweet 容斥+组合数

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 135  Solved: 66[Submit][Status] ...

  5. 【倍增】T-shirt @2018acm徐州邀请赛 I

    问题 I: T-shirt 时间限制: 1 Sec  内存限制: 64 MB 题目描述 JSZKC is going to spend his vacation! His vacation has N ...

  6. 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G

    问题 G: JS Window 时间限制: 2 Sec  内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...

  7. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  8. BZOJ5306 [HAOI2018]染色 【组合数 + 容斥 + NTT】

    题目 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 \(M\) 种颜色中的某一种. 然而小 C 只 ...

  9. 【BZOJ4710】[Jsoi2011]分特产 组合数+容斥

    [BZOJ4710][Jsoi2011]分特产 Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同 ...

随机推荐

  1. Java基础知识➣网络Socket(六)

    概述 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java.net 包中提供了两种常见的网络协议的支持: TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之 ...

  2. WPF中datagrid不显示滚动条问题

    WPF中DataGrid是自带滚动条的,当内容显示不下时可以使用滚动条显示.但是,开发中遇到了DataGrid一直不显示滚动条.之前也曾遇到过同类问题,再次经过查找,发现是StackPanel的原因, ...

  3. ionic2中使用自定义图标

    在ionic2中使用自定义图标,如iconfont(阿里巴巴矢量图标). 先在http://www.iconfont.cn/ 中找到自己需要的图标,然后将图标加入购物车,然后下载该图标. 下载完成后解 ...

  4. JMeter实现Oracle参数化(1)

    http://www.ithao123.cn/content-10469577.html

  5. day6.python字典练习题

    1.有如下变量(tu是个元组),请实现要求的功能 tu = (“alex”, [11, 22, {“k1”: ‘v1’, “k2”: [“age”, “name”], “k3”: (11,22,33) ...

  6. 2018牛客网暑假ACM多校训练赛(第五场)F take 树状数组,期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-F.html 题目传送门 - https://www.no ...

  7. BZOJ3862 Little Devil I 树链剖分

    原文链接http://www.cnblogs.com/zhouzhendong/p/8081514.html 题目传送门 - BZOJ3862 题意概括 一棵树,n个点,边权为黑或者白,支持3重操作: ...

  8. BZOJ3110 [Zjoi2013]K大数查询 树套树 线段树 整体二分 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3110 题意概括 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位 ...

  9. IDEA添加源码包

    1.在项目中选中左上角的File--->Project Structure 2.选择需要添加的源码包 3.源码已经加入

  10. Windows 下安装 Ubuntu 双系统(更新)

    Windows + Ubuntu 16.04 ---> 双系统 前言:本篇文章是对之前文章的更新,更新的主内容是把原来用手机拍摄的图片换成了虚拟机的截图,以及对磁盘划分的新的见解和一些使用感受, ...