TJU 4087组合数打表快速幂容斥原理

来源:互联网 发布:广电手游新规 知乎 编辑:程序博客网 时间:2024/05/22 03:35
4087.   box
Time Limit: 1.0 Seconds   Memory Limit:65536K
Total Runs: 119   Accepted Runs:32



Tuhao and his two small partners participated in the tournament.But in the end, they lost the chance to finish a mathematical problem.After the competition,Tuhao recalled that year in high school math class on a variety of tragedy.
One day, the math teacher give Tuhao a question:if you have 5 boxes in a line(every boxes are different) and you have 4 kinds of balls.you must put an ball in every box, and all balls can take unlimited. How many kinds of way of placing? Tuhao solve this problem easily.But he has another question to you.if we have N boxes, and we have m kind of balls, and you can't use all kinds of balls because Tuhao must have one kind ball at least to play with his partners...

Input

There will be multiple cases to consider. The first input will be a number T(0 < T ≤ 50) indicating how many cases with which you will deal. Following this number will be pairs of integers giving values for N and M, in that order. You are guaranteed that 1 ≤ N < 500, 1 ≤ M < 10, Each N M pair will occur on a line of its own. N and M will be separated by a single space.

Output

For each case display a line containing the case number (starting with 1 and increasing sequentially), and the kinds of way of placing. The desired format is illustrated in the sample shown below.(p.s. the answer should be module 200000007)

Sample Input

21 18 3

Sample Output

Case 1: 0Case 2: 765
代码来自向神,表示感谢~
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;typedef long long ll;const long long md = 200000007;const int N = 505, M = 20;ll c[M + 5][M + 5];void init()//C组合数打表{    c[0][0] = 1;    for (int i = 1; i < M; i++)    {        c[i][0] = 1;        for (int j = 1; j <= i; j++)        {            c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % md;        }    }}ll powmod(ll a, ll n){    ll y = 1;    while (n)    {        if (n & 1) y = y * a % md;        if (n >>= 1) a = a * a % md;    }    return y;}int n, m;ll solve(){    scanf("%d%d", &n, &m);    ll ans = 0;    ll op = 1;    for (int i = m - 1; i > 0; i--)    {        ans = (ans + c[m][i] * powmod(i, n) * op) % md;        op *= -1;    }    ans = (ans % md + md) % md;    return ans;}int main(){    // freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);    int T, C = 0;    init();    scanf("%d", &T);    while (T--)    {        printf("Case %d: %lld\n", ++C, solve());    }    return 0;}


0 0
原创粉丝点击