51nod 1627 瞬間移動

來源:互聯網
上載者:User

標籤:scanf   space   strong   移動   const   枚舉   51nod   amp   ns2   

題意:

有一個n * m的矩形,一開始從(1, 1)開始,每次能夠到達右下方格子的一個格子,求到達(n, m)的方案數。

 

題解:

很顯然最多走min (n - 2, m - 2) + 1步,枚舉步數K,答案為 求和 C(n - 2, K - 1) * C(m - 2, K - 1)

 

代碼:

#include <bits/stdc++.h>using namespace std;const int mod = 1e9 + 7;const int N = 1e5 + 7;#define LL long longint n, m;LL F[N], ans;map <int, LL> inv;int Pow (int x, int cnt) {int ret = 1;while (cnt) {if (cnt & 1) ret = (LL) ret * x % mod;x = (LL) x * x % mod;cnt >>= 1;}return ret;}int main () {scanf ("%d%d", &n, &m);F[0] = 1, inv[0] = 1;for (int i = 1; i < N; ++i) {F[i] = F[i-1] * i % mod;inv[i] = Pow (F[i], mod - 2);} int lim = min (n - 2, m - 2);for (int i = 1; i <= lim + 1; ++i) {LL ans1 = F[n-2] * inv[i-1] % mod * inv[n-i-1] % mod;LL ans2 = F[m-2] * inv[i-1] % mod * inv[m-i-1] % mod;ans = (ans + ans1 * ans2 % mod) % mod;}cout << ans << endl;return 0;}

 

51nod 1627 瞬間移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.