首页 > 其他 > 详细

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

时间:2018-08-30 15:19:03      阅读:194      评论:0      收藏:0      [点我收藏+]

链接: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;
}

  

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

原文:https://www.cnblogs.com/l609929321/p/9560261.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!