HDU-5698-瞬间移动(杨辉三角)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5698


Problem Description

有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第nnn行第mmm列的格子有几种方案,答案对100000000710000000071000000007取模。

http://acm.hdu.edu.cn/data/images/C702-1003-1.jpg

Input

多组测试数据。

两个整数n,m(2≤n,m≤100000)n,m(2\leq n,m\leq 100000)n,m(2n,m100000)

Output

一个整数表示答案

Sample Input
Copy
4 5
Sample Output
Copy
10

题解:由题意可以发现ans[x][y]=ans[x-1][y]+ans[x][y-1]

显然,顺时针转90°就变成杨辉三角啦,那么就直接上组合数= =,

由于有mod,组合数除的时候算一下阶乘的逆元就好。




//#include 
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int Scan()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')flag=1;
    else if(ch>='0'&&ch<='9')res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
    return flag?-res:res;
}
void Out(int a)
{
    if(a>9)Out(a/10);
    putchar(a%10+'0');
}
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug"<>=1;
        a=(a+a)%mod;
    }
    return ans;
}
int main()
{
    int n,m;
    get_jie();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(m>n)swap(m,n);
        int len=n+m-3;
        if(n==2 || m==2){cout<<"1\n";continue;}
        long long ans=(q_mul(((1LL*q_mul(jie[len-1],inv(jie[m-2])))%mod),inv(jie[len-m+1])))%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}



你可能感兴趣的:(HDU,数论)