1. 程式人生 > >【zzuli-2276】跳一跳

【zzuli-2276】跳一跳

main print led return mes 51nod 紅色 load com

題目描述

今天跳跳去公園遊玩,第一個遊戲就難倒了跳跳,遊戲規則是跳跳站在一個面積無限大的矩形土地上,開始時跳跳在左上角(即第一行第一列),每一次跳跳都可以選擇一個右下方格子,並瞬間跳過去(如從下圖中的紅色格子能直接跳到藍色格子),求跳到第n行第m列的格子有多少種方案,答案對1000000007取模。

技術分享圖片

輸入

單組測試數據。

兩個整數n,m(2<=n,m<=100000)

輸出

一個整數表示方案數。

樣例輸入

4 5

樣例輸出

10

組合數解決,可以發現規律當(2,2)是起點時,dp[x][y] = dp[x-1][y] + dp[x][y-1] ,跟51nod1118可以說是很像了

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL mod = 1000000007;
LL pow(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
LL C(LL n,LL m)
{
    LL i;
    
if(m==0) return 1; if(m>n-m) m=n-m; LL up=1,down=1; for(i=1; i<=m; i++) { up=(up*(n-i+1))%mod; down=(down*i)%mod; } return up*pow(down,mod-2)%mod; } LL lucas(LL n, LL m) { if(m==0) return 1; return lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod; }
int main() { LL m, n, t; scanf("%lld%lld", &n, &m, &mod); n--, m--; printf("%lld\n", lucas(n+m-2, min(n-1, m-1))); return 0; }

【zzuli-2276】跳一跳