码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5201 The Monkey King(容斥)

时间:2015-04-09 21:58:18      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

看了斌神的题解恍然大悟http://www.kuangbin.net/archives/bc36

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
const int MOD = 1000000007;
const int N = 200005;

int t, n, m, f[N], rf[N];

int pow_mod(int x, int k) {
    int ans = 1;
    while (k) {
        if (k&1) ans = (ll)ans * x % MOD;
        x = (ll)x * x % MOD;
        k >>= 1;
    }
    return ans;
}

int C(int n, int m) {
    return (ll)f[n] * rf[n - m] % MOD * rf[m] % MOD;
}

int cal(int x, int n, int sum) {
    if (n == 0) return sum == 0;
    int ans = 0;
    for (int i = 0; i * x <= sum && i <= n; i++) {
        int tmp = (ll)C(n, i) * C(sum - i * x + n - 1, n - 1) % MOD;
        if (i%2) ans = ((ans - tmp) % MOD + MOD) % MOD;
        else ans = (ans + tmp) % MOD;
    }
    return ans;
}

int main() {
    f[0] = 1;
    for (int i = 1; i < N; i++)
        f[i] = (ll)f[i - 1] * i % MOD;
    rf[N - 1] = pow_mod(f[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--)
        rf[i] = (ll)rf[i + 1] * (i + 1) % MOD;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans = (ans + cal(i, m - 1, n - i)) % MOD;
        printf("%d\n", ans);
    }
    return 0;
}


HDU 5201 The Monkey King(容斥)

标签:

原文地址:http://blog.csdn.net/accelerator_/article/details/44965497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!