x和y分开考虑,在(1,1)到(n,m)之间可以选择走i步。就需要选i步对应的行C(n-2,i)及i步对应的列C(m-2,i)。相乘起来。 假设$m\leq n$
$$\sum_{i=1}^{m-2} C_{n-2}^i\cdot C_{m-2}^i=\sum_{i=1}^{m-2} C_{n-2}^i\cdot C_{m-2}^{m-2-i}=C_{n+m-4}^{m-2}$$
然后标程里求i的阶乘的逆是预处理的,主要这句:
$$f[i]=(M-M/i)\cdot f[M\%i]\%M$$
这里f即i的逆元,为什么可以这么求呢?

首先这里的M必须是质数。
$$M=k\cdot i+r \equiv 0 \pmod M$$
两边乘上$i^{-1}\cdot r^{-1}$(如果M不是质数,r就可能为0)
$$\begin{eqnarray} k\cdot r^{-1}+i^{-1} &\equiv& 0 &\pmod M\\
i^{-1} &\equiv& -k\cdot r^{-1} &\pmod M\\
i^{-1} &\equiv& M-\left\lfloor\frac{M}{i}\right\rfloor\cdot \left(M\bmod i\right)^{-1} &\pmod M \end{eqnarray}$$
代码

#include<cstdio>
#define M 1000000007
#define N 200001
#define ll long long
ll fac[N]={1,1},inv[N]={1,1},f[N]={1,1};
int n,m;
ll C(ll a,ll b){
return fac[a]*inv[b]%M*inv[a-b]%M;
}
int main(){
for(int i=2;i<N;i++){
fac[i]=fac[i-1]*i%M;
f[i]=(M-M/i)*f[M%i]%M;
inv[i]=inv[i-1]*f[i]%M;
}
while(~scanf("%d%d",&n,&m))
printf("%lld\n",C(m+n-4,m-2));
}

  

【HDU 5698】瞬间移动(组合数,逆元)的更多相关文章

  1. HDU 5698——瞬间移动——————【逆元求组合数】

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  2. HDU 5698 大组合数取模(逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. HDU 5698 瞬间移动 数学

    瞬间移动 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5698 Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次 ...

  4. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  5. HDU 5698 瞬间移动

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  6. hdu 5698 瞬间移动(排列组合)

    这题刚看完,想了想,没思路,就题解了 = = 但不得不说,找到这个题解真的很强大,链接:http://blog.csdn.net/qwb492859377/article/details/514781 ...

  7. hdu 5698(杨辉三角的性质+逆元)

    ---恢复内容开始--- 瞬间移动 Accepts: 1018 Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...

  8. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  9. HDU 6044--Limited Permutation(搜索+组合数+逆元)

    题目链接 Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplicated for each 1≤ ...

随机推荐

  1. ADB server didn't ACK 问题解决

    在命令行中运行adb shell 出现如下错误提示 C:\Documents and Settings\Administrator>adb shelladb server is out of d ...

  2. 用mysql触发器实现log记录

    首先建立两张测试用表 mysql> desc pay; +-------+---------------+------+-----+---------+----------------+ | F ...

  3. P1835 素数密度_NOI导刊2011提高(04)

    题目描述 给定区间[L,R](L≤R≤2147483647,R-L≤1000000),请计算区间中素数的个数. 输入输出格式 输入格式: 两个数L和R. 输出格式: 一行,区间中素数的个数. 输入输出 ...

  4. Could not load file or assembly 'System.Data.SQLite' or one of its dependencies

    试图加载格式不正确的程 异常类型 异常消息Could not load file or assembly 'System.Data.SQLite' or one of its dependencies ...

  5. How do I list the files in a directory?

    原文地址:How do I list the files in a directory? You want a list of all the files, or all the files matc ...

  6. 什么是API

    我们从API的功能.分类.设计.实现.用户来看什么是API. API是应用程序组件之间通信的接口 --wiki:Application Programming Interface In compute ...

  7. VMware-Transport(VMDB) error -44:Message.The VMware Authorization Service is not running解决方案

    出现的错误如下: 原因:本机中有一个VMware服务未开启导致的. 解决方案: 1.打开“运行”->输入services.msc !!!文章转自浩瀚先森博客,转载请注明,谢谢.http://ww ...

  8. 分布式中使用Redis实现Session共享(一)

    上一篇介绍了如何使用nginx+iis部署一个简单的分布式系统,文章结尾留下了几个问题,其中一个是"如何解决多站点下Session共享".这篇文章将会介绍如何使用Redis,下一篇 ...

  9. 提高Visual Studio开发性能的几款插件

    通过打开Visual Studio,单机TOOLS—Extensions and Updates-Online-Visual Studio Gallery(工具-扩展和更新-联网-Visual Stu ...

  10. 20160223 - Windows 10 的文件资源管理器下出现重复文件夹的解决办法

    现象: 安装 OneDrive 从 Windows 7.8.8.1 升级来的 Windows 10 的电脑,可能会出现文件资源管理器左侧面板中出现重复的文件夹. 通常有:视频.图片.文档.下载.音频. ...