QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417048 | #8715. 放苹果 | ucup_team_qiuly# | AC ✓ | 224ms | 37572kb | C++17 | 3.1kb | 2024-05-22 13:42:30 | 2024-05-22 13:42:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5,MOD=998244353;
int n,m,ans,fc[N],invFc[N],a[N],b[N];
int l,lim,lim1,invL,r[N],inv[N],tmp1[N],tmp2[N],tmp3[N],g[2][N];
void W(int &x,int y) {x+=y;if(x>=MOD) x-=MOD;}
int add(int x,int y) {x+=y;return x<MOD?x:x-MOD;}
int qPow(int x,int y)
{int res=1;for(;y;y/=2,x=1ll*x*x%MOD) if(y&1) res=1ll*res*x%MOD;return res;}
void init1(int n)
{
fc[0]=invFc[0]=1;
for(int i=1;i<=n;++i) fc[i]=1ll*fc[i-1]*i%MOD;
invFc[n]=qPow(fc[n],MOD-2);
for(int i=n-1;i;--i) invFc[i]=1ll*invFc[i+1]*(i+1)%MOD;
}
void init(int n)
{
l=0;lim=1;while(lim<n) ++l,lim*=2;invL=qPow(lim,MOD-2);
for(int i=0;i<lim;++i) r[i]=(r[i>>1]>>1)|((i&1)<<l-1);
if(lim>lim1)
{
for(int i=1;i<lim;++i) inv[i]=i>1?1ll*inv[MOD%i]*(MOD-MOD/i)%MOD:1;
for(int i=1,t1,t2,t3,t4;i<lim;i*=2)
{
t1=qPow(3,(MOD-1)/(i*2));t2=qPow(t1,MOD-2);t3=t4=1;
for(int j=0;j<i;++j,t3=1ll*t3*t1%MOD,t4=1ll*t4*t2%MOD)
g[0][i+j]=t3,g[1][i+j]=t4;
}lim1=lim;
}
}
void deriv(int n,int a[]) {for(int i=1;i<n;++i) a[i-1]=1ll*a[i]*i%MOD;a[n-1]=0;}
void integ(int n,int a[]) {for(int i=n-1;i;--i) a[i]=1ll*a[i-1]*inv[i]%MOD;a[0]=0;}
void NTT(bool fl,int a[])
{
for(int i=0;i<lim;++i) if(i<r[i]) swap(a[i],a[r[i]]);
for(int i=1,t1,t2;i<lim;i*=2) for(int j=0;j<lim;j+=i*2) for(int k=0;k<i;++k)
{
t1=a[j+k];t2=1ll*g[fl][i+k]*a[i+j+k]%MOD;
a[j+k]=add(t1,t2);a[i+j+k]=add(t1,MOD-t2);
}if(fl) for(int i=0;i<lim;++i) a[i]=1ll*a[i]*invL%MOD;
}
void polyInv(int n,int a[],int res[])
{
if(n==1) {res[0]=qPow(a[0],MOD-2);return;}polyInv((n+1)/2,a,res);
for(int i=0;i<n;++i) tmp1[i]=a[i];for(int i=n;i<lim;++i) tmp1[i]=0;
init(n*2-1);NTT(0,tmp1);NTT(0,res);
for(int i=0;i<lim;++i) res[i]=(2-1ll*tmp1[i]*res[i]%MOD+MOD)*res[i]%MOD;
NTT(1,res);for(int i=n;i<lim;++i) res[i]=0;
}
void polyLn(int n,int a[])
{
init(n*2-1);for(int i=0;i<lim;++i) tmp2[i]=0;
polyInv(n,a,tmp2);deriv(n,a);NTT(0,a);NTT(0,tmp2);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*tmp2[i]%MOD;
NTT(1,a);integ(n,a);for(int i=n;i<lim;++i) a[i]=0;
}
void polyExp(int n,int a[],int res[])
{
if(n==1) {res[0]=1;return;}polyExp((n+1)/2,a,res);
for(int i=0;i<n;++i) tmp3[i]=res[i];for(int i=n;i<lim;++i) tmp3[i]=0;
polyLn(n,tmp3);for(int i=0;i<n;++i) tmp3[i]=add(a[i],MOD-tmp3[i]);++tmp3[0];
NTT(0,tmp3);NTT(0,res);for(int i=0;i<lim;++i) res[i]=1ll*res[i]*tmp3[i]%MOD;
NTT(1,res);for(int i=n;i<lim;++i) res[i]=0;
}
int main()
{
scanf("%d %d",&n,&m);init1(1e6);
for(int i=0;i<=n;++i) a[i]=invFc[i+1];polyInv(n+1,a,b);
for(int i=0;i<=n;++i) a[i]=1ll*qPow(m,i+1)*invFc[i+1]%MOD;
init(n*2+1);NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=0;i<=n;++i) a[i]=1ll*a[i]*fc[i]%MOD;W(a[0],MOD-1);
reverse(a,a+n+1);fill(a+n+1,a+lim,0);fill(b,b+lim,0);
for(int i=0;i<=n;++i) a[i]=1ll*a[i]*qPow(m,i)%MOD*invFc[i]%MOD;
for(int i=0;i<=n;++i) b[i]=i&1?MOD-invFc[i]:invFc[i];
NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=1;i<n;++i) ans=(ans+1ll*a[i]*fc[n]%MOD*invFc[n-i]%MOD*min(i,n-i))%MOD;
printf("%d\n",ans);return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 8ms
memory: 23228kb
input:
2 3
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 12ms
memory: 23476kb
input:
3 3
output:
36
result:
ok 1 number(s): "36"
Test #3:
score: 0
Accepted
time: 12ms
memory: 23040kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 8ms
memory: 23424kb
input:
1 2
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 4ms
memory: 22632kb
input:
1 3
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 12ms
memory: 22908kb
input:
2 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 4ms
memory: 22216kb
input:
3 1
output:
0
result:
ok 1 number(s): "0"
Test #8:
score: 0
Accepted
time: 11ms
memory: 23816kb
input:
3719 101
output:
78994090
result:
ok 1 number(s): "78994090"
Test #9:
score: 0
Accepted
time: 11ms
memory: 23640kb
input:
2189 1022
output:
149789741
result:
ok 1 number(s): "149789741"
Test #10:
score: 0
Accepted
time: 14ms
memory: 23696kb
input:
2910 382012013
output:
926541722
result:
ok 1 number(s): "926541722"
Test #11:
score: 0
Accepted
time: 204ms
memory: 35776kb
input:
131072 3837829
output:
487765455
result:
ok 1 number(s): "487765455"
Test #12:
score: 0
Accepted
time: 210ms
memory: 37572kb
input:
183092 100000000
output:
231786691
result:
ok 1 number(s): "231786691"
Test #13:
score: 0
Accepted
time: 224ms
memory: 34496kb
input:
197291 937201572
output:
337054675
result:
ok 1 number(s): "337054675"
Test #14:
score: 0
Accepted
time: 208ms
memory: 36616kb
input:
200000 328194672
output:
420979346
result:
ok 1 number(s): "420979346"
Test #15:
score: 0
Accepted
time: 220ms
memory: 33580kb
input:
200000 1000000000
output:
961552572
result:
ok 1 number(s): "961552572"
Extra Test:
score: 0
Extra Test Passed