百度之星 初賽2 瞬間轉移 HDU 5698 (組合數+逆元)
阿新 • • 發佈:2019-01-26
大意:有一個無限大的矩形,初始時你在左上角(即第一行第一列),每次你都可以選擇一個右下方格子,並瞬移過去(如從下圖中的紅色格子能直接瞬移到藍色格子),求到第n行第m列的格子有幾種方案,答案對1000000007取模
思路:每次都是向右下角走,然後就是
S
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
E
這麼看來類似於楊輝三角,
1 1 1 1 1 1 1
1 2 3 4 5 6
1 3 6 10 15
1 4 10 20
1 5 15
1 6
1
但是楊輝三角有組合公式(第n行第m個數為C(n-1,m-1)),因為第一、第N行第一、N列,不用故減去所以類似的可以退出公式C(n+m-4,n-2)
#include<map> #include<queue> #include<cmath> #include<cstdio> #include<stack> #include<iostream> #include<cstring> #include<algorithm> #define LL int #define inf 0x3f3f3f3f #define eps 1e-8 #include<vector> #define ls l,mid,rt<<1 #define rs mid+1,r,rt<<1|1 #define LL __int64 using namespace std; const int mod = 1000000007; LL mul[100010]; LL qpow(LL x, LL y, LL mod) { x %= mod; LL ans = 1; for(;y > 0; y>>= 1) { if(y & 1) { ans *= x; ans %= mod; } x *= x; x %= mod; } return ans; } // (x / y)%mod LL div(LL x, LL y, LL mod) { x %= mod; LL ans = x * qpow(y, mod - 2, mod); ans %= mod; return ans; } LL com(int a,int b){ if(a > b/2) a = b-a; int i,j; LL t1 = 1,t2 = 1; for(i = b,j = 1;j <= a;--i,++j){ t1 = (t1*i)%mod; t2 = (t2*j)%mod; if(t1%t2 == 0){ t1 = div(t1,t2,mod); t2 = 1; } } return div(t1,t2,mod); } int main(){ int n,m,i,j,k;LL ans,jie = 1; while(~scanf("%d%d",&n,&m)){ ans = 0; ans = com(m-2,n+m-4)%mod; printf("%I64d\n",ans); } return 0; }