1866H - Happy Sets - CodeForces Solution


combinatorics

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int mod=998244353;
inline int ksm(int x,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)	res=res*x%mod;
		x=x*x%mod;
		b>>=1;
	}
	return res;
}

int fac[300005],invFac[300005],n,k;

inline int C(int x,int y)
{
	if (y>x) return 0;
	if (x==y||y==0) return 1;
	return fac[x]*invFac[x-y]%mod*invFac[y]%mod;
}

inline void init(int x)
{
	fac[0]=fac[1]=1;
	for (int i=2;i<=x;i++) fac[i]=fac[i-1]*i%mod;
	invFac[x]=ksm(fac[x],mod-2);
	for (int i=x-1;i>=0;i--) invFac[i]=invFac[i+1]*(i+1)%mod;
}

signed main()
{
    init(300000);
	cin>>n>>k;
	int ans=0;
	for (int i=1,lst=1;i<=min(n,k);i++) 
	{
		int tmp=fac[i+1];
		tmp=tmp*ksm(i+1,k-i)%mod;
		tmp=(tmp-lst+mod)%mod;
		lst=(lst+tmp)%mod;
		tmp=tmp*C(n,i)%mod;
		tmp=tmp*fac[i]%mod;
		ans=(ans+tmp)%mod;
	}
	ans=(ans+1)%mod; 
	cout<<ans;
}


Comments

Submit
0 Comments
More Questions

1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold