牛客多校第六场 C Generation I 组合数学 阶乘逆元模板

链接:https://www.nowcoder.com/acm/contest/144/C
来源:牛客网

Oak is given N empty and non-repeatable sets which are numbered from 1 to N.

Now Oak is going to do N operations. In the i-th operation, he will insert an integer x between 1 and M to every set indexed between i and N.

Oak wonders how many different results he can make after the N operations. Two results are different if and only if there exists a set in one result different from the set with the same index in another result.

Please help Oak calculate the answer. As the answer can be extremely large, output it modulo 998244353.

输入描述:

The input starts with one line containing exactly one integer T which is the number of test cases. (1 ≤ T ≤ 20)

Each test case contains one line with two integers N and M indicating the number of sets and the range of integers. (1 ≤ N ≤ 10

18

, 1 ≤ M ≤ 10

18

, 

)

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the number of different results modulo 998244353.

示例1

输入

复制
2
2 2
3 4

输出

复制
Case #1: 4
Case #2: 52

题意:有n个set(没有重复元素),有无限个1~m,第i次操作可以从中选一个元素往set i~n里面插入 
求有多少种可能结果(只要有一个set不是完全相同)
分析:
参考博客:
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6 + 10;
const double eps = 1e-8;
const ll mod = 998244353;
const ll inf = 1e9;
const double pi = acos(-1.0);
ll inv[maxn];
ll qow( ll a, ll b ) {
    ll ans = 1;
    while(b) {
        if(b&1) {
            ans = ans*a%mod;
        }
        a = a*a%mod;
        b /= 2;
    }
    return ans;
}
void init() { //求阶乘逆元
    inv[1] = 1;
    for( ll i = 2; i <= maxn-10; i ++ ) {
        inv[i] = (mod-mod/i)*inv[mod%i]%mod;
    }
}
int main() {
    ll T;
    scanf("%lld",&T);
    init();
    for( ll cas = 1, n, m; cas <= T; cas ++ ) {
        scanf("%lld%lld",&n,&m);
        ll A = m%mod, C = 1, ans = 0, M = min(n,m);
        n = n%mod, m = m%mod;
        for( ll i = 1; i <= M; i ++ ) {
            ans += A*C%mod;
            ans %= mod;
            A = (m-i)%mod*A%mod, C = (n-i)%mod*C%mod*inv[i]%mod;
        }
        printf("Case #%lld: %lld\n",cas,ans);
    }
    return 0;
}

  

posted on 2018-08-30 15:02  九月旧约  阅读(262)  评论(0编辑  收藏  举报

导航