QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#287488 | #7923. Ferris Wheel | Lynkcat | AC ✓ | 4811ms | 226796kb | C++20 | 3.0kb | 2023-12-20 17:28:08 | 2023-12-20 17:28:08 |
Judging History
answer
// #pragma GCC optimize(2,3)
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define poly vector<int>
#define IOS ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define mt make_tuple
#define pa pair < int,int >
#define fi first
#define se second
#define inf 1e18
#define mod 998244353
#define sz(x) (int)((x).size())
// #define int ll
//#define N
using namespace std;
const int N=10000005;
int fac[N],mul[N],inv[N],f[N];
int n,m;
inline ll quickPower(ll x,ll y)
{
ll res=1;
while (y)
{
if (y&1) res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
inline int C(int x,int y)
{
if (x<y||y<0) return 0;
return 1ll*fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int way(int x,int y)
{
return C(x+y,y);
}
int way_(int x,int y)
{
return (way(x,y)-way(y-1,x+1)+mod)%mod;
}
int h[N],hh[N];
int re[N];
void build_re(int nn)
{
int x,now=nn>>1,len=0;
while(now)
len++,now>>=1;
for(int i=0;i<nn;i++)
{
x=i;now=0;
for(int j=0;j<len;j++)
now=(now<<1)|(x&1),x>>=1;
re[i]=now;
}
}
void NTT(int a[],int len,int opt)
{
for(int i=0;i<len;i++)
if(i<re[i])
swap(a[i],a[re[i]]);
int wn,w,x;
for(int i=2;i<=len;i<<=1)
for(int j=(wn=quickPower(3,(mod-1)/i),(opt==-1?wn=quickPower(wn,mod-2):0),0);j<len;j+=i)
for(int k=(w=1,0);k<i>>1;k++,w=1ll*w*wn%mod)
x=1ll*a[j+k+(i>>1)]*w%mod,a[j+k+(i>>1)]=(a[j+k]-x+mod)%mod,a[j+k]=(a[j+k]+x)%mod;
if(opt==-1)
{
long long inv=quickPower(len,mod-2);
for(int i=0;i<len;i++)
a[i]=1ll*a[i]*inv%mod;
}
}
void poly_inv(int *a,int *tar,int len)
{
static int now[N];
memset(now,0,sizeof(int)*len);
tar[0]=quickPower(a[0],mod-2);
for(int i=2;i<=len;i<<=1)
{
build_re(i<<1);
memcpy(now,a,sizeof(int)*i);
NTT(tar,i<<1,1);NTT(now,i<<1,1);
for(int j=0;j<i<<1;j++)
tar[j]=1ll*tar[j]*(2LL-1ll*now[j]*tar[j]%mod+mod)%mod;
NTT(tar,i<<1,-1);
memset(tar+i,0,sizeof(int)*i);
}
}
void BellaKira()
{
cin>>n>>m;
n*=2;
poly g;
for (int i=1;i<=n;i++)
mul[__gcd(i,n)]++;
if (m==1)
{
cout<<1<<'\n';
return;
}
hh[0]=1;
for (int i=1;i<=n/2;i++)
hh[i]=(mod-1ll*way_(i-1,i-1)*quickPower(m-1,i-1)%mod*m%mod)%mod;
int Len=1;
while(Len<=n/2+1)
Len<<=1;
poly_inv(hh,h,Len);
// for (int i=0;i<=n;i++) cout<<h[i]<<" ";
// cout<<endl;
int ans=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
{
if (i%2==0)
{
if (m==1) f[i]=1;
else
f[i]=h[i/2];
} else
{
if (m==1) f[i]=1;
else
for (int j=0;j<=i/2;j++)
{
f[i]=(f[i]+1ll*way(j,i-j)*m%mod
*quickPower(m-1,j)%mod)%mod;
}
}
ans=(ans+1ll*f[i]*mul[i]%mod)%mod;
}
}
cout<<1ll*ans*quickPower(n,mod-2)%mod<<'\n';
}
signed main()
{
fac[0]=1;
for (int i=1;i<N;i++)
fac[i]=1ll*fac[i-1]*i%mod;
inv[N-1]=quickPower(fac[N-1],mod-2);
for (int i=N-1;i>=1;i--)
inv[i-1]=1ll*inv[i]*i%mod;
IOS;
cin.tie(0);
int T=1;
while (T--)
{
BellaKira();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 82ms
memory: 91676kb
input:
3 2
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 85ms
memory: 91772kb
input:
5 3
output:
372
result:
ok single line: '372'
Test #3:
score: 0
Accepted
time: 79ms
memory: 91700kb
input:
2023 1126
output:
900119621
result:
ok single line: '900119621'
Test #4:
score: 0
Accepted
time: 4368ms
memory: 224992kb
input:
2882880 2892778
output:
421329574
result:
ok single line: '421329574'
Test #5:
score: 0
Accepted
time: 4432ms
memory: 224416kb
input:
2993760 2738336
output:
436950672
result:
ok single line: '436950672'
Test #6:
score: 0
Accepted
time: 4387ms
memory: 226796kb
input:
2948400 2885387
output:
4079921
result:
ok single line: '4079921'
Test #7:
score: 0
Accepted
time: 4380ms
memory: 219804kb
input:
2827440 2802363
output:
243025786
result:
ok single line: '243025786'
Test #8:
score: 0
Accepted
time: 4677ms
memory: 216452kb
input:
2962575 2720060
output:
651075152
result:
ok single line: '651075152'
Test #9:
score: 0
Accepted
time: 4629ms
memory: 214304kb
input:
2927925 2880541
output:
547809301
result:
ok single line: '547809301'
Test #10:
score: 0
Accepted
time: 4601ms
memory: 221440kb
input:
2837835 2955737
output:
66014301
result:
ok single line: '66014301'
Test #11:
score: 0
Accepted
time: 4811ms
memory: 213920kb
input:
2993445 2802568
output:
852155961
result:
ok single line: '852155961'
Test #12:
score: 0
Accepted
time: 4549ms
memory: 225424kb
input:
3000000 2721531
output:
330139745
result:
ok single line: '330139745'
Test #13:
score: 0
Accepted
time: 4649ms
memory: 206076kb
input:
2999999 2874068
output:
11487172
result:
ok single line: '11487172'
Test #14:
score: 0
Accepted
time: 83ms
memory: 91700kb
input:
1 2879803
output:
2879803
result:
ok single line: '2879803'
Test #15:
score: 0
Accepted
time: 88ms
memory: 93784kb
input:
2 1634023
output:
363699315
result:
ok single line: '363699315'
Test #16:
score: 0
Accepted
time: 79ms
memory: 91580kb
input:
3 1133070
output:
941531760
result:
ok single line: '941531760'
Test #17:
score: 0
Accepted
time: 79ms
memory: 91788kb
input:
4 2975012
output:
600544573
result:
ok single line: '600544573'
Test #18:
score: 0
Accepted
time: 80ms
memory: 91700kb
input:
5 478043
output:
284058138
result:
ok single line: '284058138'
Test #19:
score: 0
Accepted
time: 71ms
memory: 91696kb
input:
6 377374
output:
21417458
result:
ok single line: '21417458'
Test #20:
score: 0
Accepted
time: 1050ms
memory: 123584kb
input:
575199 2998538
output:
985915533
result:
ok single line: '985915533'
Test #21:
score: 0
Accepted
time: 2202ms
memory: 159876kb
input:
1566414 2864371
output:
555268438
result:
ok single line: '555268438'
Test #22:
score: 0
Accepted
time: 1104ms
memory: 125528kb
input:
759957 2868981
output:
485958800
result:
ok single line: '485958800'
Test #23:
score: 0
Accepted
time: 2348ms
memory: 153888kb
input:
1906997 2799171
output:
340977363
result:
ok single line: '340977363'
Test #24:
score: 0
Accepted
time: 4489ms
memory: 210404kb
input:
2420926 497282
output:
927874930
result:
ok single line: '927874930'
Test #25:
score: 0
Accepted
time: 4609ms
memory: 215376kb
input:
2660337 2477085
output:
817341716
result:
ok single line: '817341716'
Test #26:
score: 0
Accepted
time: 4561ms
memory: 209844kb
input:
2661314 2721730
output:
576896798
result:
ok single line: '576896798'
Test #27:
score: 0
Accepted
time: 532ms
memory: 107488kb
input:
353896 2925858
output:
274269675
result:
ok single line: '274269675'
Test #28:
score: 0
Accepted
time: 4613ms
memory: 205816kb
input:
2726809 819583
output:
864442996
result:
ok single line: '864442996'
Test #29:
score: 0
Accepted
time: 4680ms
memory: 206964kb
input:
2893369 2895789
output:
521969424
result:
ok single line: '521969424'
Test #30:
score: 0
Accepted
time: 4625ms
memory: 205892kb
input:
2858441 2736227
output:
357358178
result:
ok single line: '357358178'
Test #31:
score: 0
Accepted
time: 4645ms
memory: 207432kb
input:
2954587 744730
output:
540167871
result:
ok single line: '540167871'
Test #32:
score: 0
Accepted
time: 2307ms
memory: 152984kb
input:
1902149 2964684
output:
967678819
result:
ok single line: '967678819'
Test #33:
score: 0
Accepted
time: 4665ms
memory: 205696kb
input:
2906083 2818150
output:
800415339
result:
ok single line: '800415339'
Test #34:
score: 0
Accepted
time: 1137ms
memory: 127052kb
input:
947213 2843556
output:
704887450
result:
ok single line: '704887450'
Test #35:
score: 0
Accepted
time: 2099ms
memory: 150068kb
input:
1068401 2866144
output:
643696823
result:
ok single line: '643696823'
Test #36:
score: 0
Accepted
time: 4361ms
memory: 214772kb
input:
2097152 2706726
output:
879969833
result:
ok single line: '879969833'
Test #37:
score: 0
Accepted
time: 2245ms
memory: 160892kb
input:
1594323 1385645
output:
166289728
result:
ok single line: '166289728'
Test #38:
score: 0
Accepted
time: 2356ms
memory: 154616kb
input:
1953125 2702955
output:
356924892
result:
ok single line: '356924892'
Test #39:
score: 0
Accepted
time: 1119ms
memory: 127260kb
input:
823543 469787
output:
217410289
result:
ok single line: '217410289'
Test #40:
score: 0
Accepted
time: 2280ms
memory: 157392kb
input:
1771561 2338083
output:
17307265
result:
ok single line: '17307265'
Test #41:
score: 0
Accepted
time: 540ms
memory: 112320kb
input:
371293 2739508
output:
768654866
result:
ok single line: '768654866'
Test #42:
score: 0
Accepted
time: 227ms
memory: 95868kb
input:
1278612 1
output:
1
result:
ok single line: '1'
Test #43:
score: 0
Accepted
time: 410ms
memory: 101916kb
input:
2949300 1
output:
1
result:
ok single line: '1'