列表

详情


NC17440. Generation I

描述

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 ≤ 1018, 1 ≤ M ≤ 1018, )

输出描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 358ms, 内存消耗: 4288K, 提交时间: 2018-08-04 12:50:51

#include<bits/stdc++.h>
using namespace std;
#define N 1000010
#define mo 998244353
int Inv[N];
int main(){
	int i;
	Inv[1]=1;
	for (i=2;i<N;i++){
		Inv[i]=1LL*(mo-mo/i)*Inv[mo%i]%mo;
	}
	int Case,Tt;
	scanf("%d",&Case);
	for (Tt=1;Tt<=Case;Tt++){
		long long n,m;
		scanf("%lld%lld",&n,&m);
		int Ans=0,Sum=1;
		for (i=1;i<=min(n,m);i++){
			Sum=1LL*Sum*((m-i+1)%mo)%mo;
			if (i>1){
				Sum=1LL*Sum*((n-i+1)%mo)%mo*Inv[i-1]%mo;
			}
			Ans=(Ans+Sum)%mo;
		}
		printf("Case #%d: %d\n",Tt,Ans);
	}	
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 220ms, 内存消耗: 8288K, 提交时间: 2018-08-04 12:57:12

#include<bits/stdc++.h>
using namespace std;
#define maxn 1001000
#define ll long long
const int p=998244353;
ll n,m;
ll inv[maxn];
int main()
{
	int T;
	scanf("%d",&T);
	inv[1]=inv[0]=1;
	for(int i=2;i<maxn;++i) inv[i]=(p-p/i)*inv[p%i]%p;
	for(int cas=1;cas<=T;++cas)
	{
		scanf("%lld%lld",&n,&m);
		ll fi=m%p,se=1;
		ll ans=0;
		for(int i=1;i<=n&&i<=m;++i)
		{
			ans=(ans+fi*se)%p;
			fi=fi*((m-i)%p)%p;
			se=se*((n-i)%p)%p*inv[i]%p;
		}
		printf("Case #%d: %lld\n",cas,ans);
	}
	return 0;
}

上一题