wdl_ @ 2022-06-15 21:50:04
RT
#include<bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
ll ans1,ans2,n,f[1000005]={1};
ll ksm(ll a,ll b)
{
ll t=1;
while(b)
{
if(b&1)t=t*a%mod;
a=a*a%mod;
b>>=1;
}
return t;
}
void jiecheng(ll x)
{
for(int i=1;i<=x;i++)
f[i]=f[i-1]*i%mod;
}
int main()
{
freopen("draw.in","r",stdin);
freopen("draw.out","w",stdout);
cin>>n;
jiecheng(n);
for(int i=1;i<=n;i++)
{
ans1+=f[n]/(f[i]*f[n-i])*((i-1)%2==0?1:-1)*ksm(3,i)*ksm(3,n*(n-i))%mod;
ll mi = ksm(3,n-i);
ans2+=f[n]/(f[i]*f[n-i])*((i)%2==0?1:-1)*(ksm(mi-1,n)-ksm(mi,n));
//cout<<f[n]/(f[i]*f[n-i])*((i)%2==0?1:-1)*(ksm(-1*(ksm(3,i)-ksm(3,n))+1,n)-1)/ksm(3,i*n)<<endl;
//cout<<ans1<<' '<<ans2<<endl;
}
ans1=ans1*2%mod;
ans2=ans2*-3%mod;
cout<<(ans1+ans2)%mod;
}
by whdywjd @ 2023-06-11 09:31:53
f[n]/(f[i]*f[n-i])
是错误的,比如:
正确的取模方式请参见 https://www.luogu.com.cn/problem/P3811 。
by whdywjd @ 2023-06-11 09:35:28
ans1+=f[n]/(f[i]*f[n-i])*((i-1)%2==0?1:-1)*ksm(3,i)*ksm(3,n*(n-i))%mod;
不建议连乘几个数不取模,因为 long long
了。
by whdywjd @ 2023-06-11 09:38:40
cout<<(ans1+ans2)%mod;
C++ 中,负数取模后还是负的,如 ans1,ans2
符号不确定的情况下,为了避免输出负数,可改为 cout<<(2*mod+ans1+ans2)%mod;
by whdywjd @ 2023-06-11 09:38:58
@wdl_
by wdl_ @ 2024-07-16 20:14:29
@whdywjd 很早以前就AC了,但仍然谢谢您