【JZOJ 省選模擬】6675.最大公約數

題目

Description

在這裏插入圖片描述
在這裏插入圖片描述
Input
從文件 traffic.in 中讀入數據。
總共一行,一個正整數n,表示城市個數。

Output
輸出到文件 traffic.out 中。
輸出一行,一個整數,表示價值總和對998244353 取模後的結果。

Sample Input
Sample Input1
4

Sample Input2
56

Sample Output
Sample Output1
94

Sample Output2
372169800

Data Constraint
在這裏插入圖片描述

思路

這裏我直接推薦一篇博文,講的很好
https://www.cnblogs.com/jz-597/p/13027755.html

代碼

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+77,mod=998244353;
ll n,ans;
ll fac[N],unfac[N];
ll power(ll x,ll t)
{
	ll b=1;
	while(t)
	{
		if(t&1) b=b*x%mod;
		x=x*x%mod; t>>=1;
	}
	return b;
}
ll C(ll n,ll m)
{
	return unfac[m]*unfac[n-m]%mod*fac[n]%mod;
}
ll f(ll n,ll m)
{
	return C(n+m-1,m);
}
int main()
{
	freopen("traffic.in","r",stdin);
	freopen("traffic.out","w",stdout);
	cin>>n;
	fac[0]=1;
	for(int i=1; i<=2*n; i++)
		fac[i]=fac[i-1]*i%mod;
	unfac[2*n]=power(fac[2*n],mod-2);
	for(int i=2*n; i; i--)
		unfac[i-1]=unfac[i]*i%mod;
	n--;
	ll q=power(n+1,mod-2);
	for(int i=1; i<=n; i++)
	{
		ans=(ans+f(2*i+1,n-i)*q%mod*2*i)%mod;
		q=q*(n+1)%mod;
	}
	cout<<ans*2%mod<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章