QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282415#7923. Ferris WheelLynkcatAC ✓5389ms440056kbC++233.5kb2023-12-11 23:13:212023-12-11 23:13:21

Judging History

你现在查看的是最新测评结果

  • [2023-12-11 23:13:21]
  • 评测
  • 测评结果:AC
  • 用时:5389ms
  • 内存:440056kb
  • [2023-12-11 23:13:21]
  • 提交

answer

#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 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 FFT(complex a[],int len,int opt)
//{
//	for(int i=0;i<len;i++)
//	  if(i<re[i])
//	    swap(a[i],a[re[i]]);
//	complex wn,w,x;
//	for(int i=2;i<=len;i<<=1)
//	  for(int j=(wn=(complex){cos(pi*2/i),opt*sin(pi*2/i)},0);j<len;j+=i)
//	  	for(int k=(w=(complex){1,0},0);k<i>>1;k++,w=w*wn)
//	  	  x=w*a[j+k+(i>>1)],a[j+k+(i>>1)]=a[j+k]-x,a[j+k]=a[j+k]+x;
//	if(opt==-1)
//	  for(int i=0;i<len;i++)
//	    a[i]=a[i]/len;
//}
void NTT(long long a[],int len,int opt)
{
	for(int i=0;i<len;i++)
	  if(i<re[i])
	    swap(a[i],a[re[i]]);
	long long 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=w*wn%mod)
	      x=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]=a[i]*inv%mod;
	}
}
void poly_inv(long long a[],long long tar[],int len)
{
	static long long now[N],ret[N];
	memset(now,0,sizeof(long long)*len);memset(ret,0,sizeof(long long)*len);
	ret[0]=quickPower(a[0],mod-2);
	for(int i=2;i<=len;i<<=1)
	{
		build_re(i<<1);
		memcpy(now,a,sizeof(long long)*i);
		NTT(ret,i<<1,1);NTT(now,i<<1,1);
		for(int j=0;j<i<<1;j++)
		  ret[j]=ret[j]*(2LL-now[j]*ret[j]%mod+mod)%mod;
		NTT(ret,i<<1,-1);
		memset(ret+i,0,sizeof(long long)*i);
	}
	memcpy(tar,ret,sizeof(long long)*len);
}
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-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]+way(j,i-j)*m%mod
					*quickPower(m-1,j)%mod)%mod;
				}
			}
			// cout<<i<<" "<<f[i]<<" "<<mul[i]<<" "<<way_(4,4)<<endl;
			ans=(ans+f[i]*mul[i]%mod)%mod;
		}
	}
	// cout<<ans<<'\n';
	cout<<ans*quickPower(n,mod-2)%mod<<'\n';
}
signed main()
{
	fac[0]=1;
	for (int i=1;i<N;i++)
		fac[i]=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]=inv[i]*i%mod;
	IOS;
	cin.tie(0);
	int T=1;
	while (T--)
	{
		BellaKira();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 56ms
memory: 163040kb

input:

3 2

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 67ms
memory: 162764kb

input:

5 3

output:

372

result:

ok single line: '372'

Test #3:

score: 0
Accepted
time: 97ms
memory: 171620kb

input:

2023 1126

output:

900119621

result:

ok single line: '900119621'

Test #4:

score: 0
Accepted
time: 4150ms
memory: 416408kb

input:

2882880 2892778

output:

421329574

result:

ok single line: '421329574'

Test #5:

score: 0
Accepted
time: 4069ms
memory: 436416kb

input:

2993760 2738336

output:

436950672

result:

ok single line: '436950672'

Test #6:

score: 0
Accepted
time: 4074ms
memory: 433544kb

input:

2948400 2885387

output:

4079921

result:

ok single line: '4079921'

Test #7:

score: 0
Accepted
time: 4045ms
memory: 431568kb

input:

2827440 2802363

output:

243025786

result:

ok single line: '243025786'

Test #8:

score: 0
Accepted
time: 4361ms
memory: 434596kb

input:

2962575 2720060

output:

651075152

result:

ok single line: '651075152'

Test #9:

score: 0
Accepted
time: 4319ms
memory: 433228kb

input:

2927925 2880541

output:

547809301

result:

ok single line: '547809301'

Test #10:

score: 0
Accepted
time: 4280ms
memory: 432572kb

input:

2837835 2955737

output:

66014301

result:

ok single line: '66014301'

Test #11:

score: 0
Accepted
time: 4376ms
memory: 435380kb

input:

2993445 2802568

output:

852155961

result:

ok single line: '852155961'

Test #12:

score: 0
Accepted
time: 4991ms
memory: 440056kb

input:

3000000 2721531

output:

330139745

result:

ok single line: '330139745'

Test #13:

score: 0
Accepted
time: 5389ms
memory: 422748kb

input:

2999999 2874068

output:

11487172

result:

ok single line: '11487172'

Test #14:

score: 0
Accepted
time: 81ms
memory: 171612kb

input:

1 2879803

output:

2879803

result:

ok single line: '2879803'

Test #15:

score: 0
Accepted
time: 76ms
memory: 173644kb

input:

2 1634023

output:

363699315

result:

ok single line: '363699315'

Test #16:

score: 0
Accepted
time: 105ms
memory: 171644kb

input:

3 1133070

output:

941531760

result:

ok single line: '941531760'

Test #17:

score: 0
Accepted
time: 100ms
memory: 171588kb

input:

4 2975012

output:

600544573

result:

ok single line: '600544573'

Test #18:

score: 0
Accepted
time: 58ms
memory: 171592kb

input:

5 478043

output:

284058138

result:

ok single line: '284058138'

Test #19:

score: 0
Accepted
time: 93ms
memory: 171616kb

input:

6 377374

output:

21417458

result:

ok single line: '21417458'

Test #20:

score: 0
Accepted
time: 1100ms
memory: 239516kb

input:

575199 2998538

output:

985915533

result:

ok single line: '985915533'

Test #21:

score: 0
Accepted
time: 2450ms
memory: 316204kb

input:

1566414 2864371

output:

555268438

result:

ok single line: '555268438'

Test #22:

score: 0
Accepted
time: 1158ms
memory: 248080kb

input:

759957 2868981

output:

485958800

result:

ok single line: '485958800'

Test #23:

score: 0
Accepted
time: 2534ms
memory: 305024kb

input:

1906997 2799171

output:

340977363

result:

ok single line: '340977363'

Test #24:

score: 0
Accepted
time: 5058ms
memory: 423408kb

input:

2420926 497282

output:

927874930

result:

ok single line: '927874930'

Test #25:

score: 0
Accepted
time: 5313ms
memory: 430284kb

input:

2660337 2477085

output:

817341716

result:

ok single line: '817341716'

Test #26:

score: 0
Accepted
time: 5246ms
memory: 426384kb

input:

2661314 2721730

output:

576896798

result:

ok single line: '576896798'

Test #27:

score: 0
Accepted
time: 558ms
memory: 212680kb

input:

353896 2925858

output:

274269675

result:

ok single line: '274269675'

Test #28:

score: 0
Accepted
time: 5237ms
memory: 423100kb

input:

2726809 819583

output:

864442996

result:

ok single line: '864442996'

Test #29:

score: 0
Accepted
time: 5263ms
memory: 424228kb

input:

2893369 2895789

output:

521969424

result:

ok single line: '521969424'

Test #30:

score: 0
Accepted
time: 5208ms
memory: 421552kb

input:

2858441 2736227

output:

357358178

result:

ok single line: '357358178'

Test #31:

score: 0
Accepted
time: 5326ms
memory: 423900kb

input:

2954587 744730

output:

540167871

result:

ok single line: '540167871'

Test #32:

score: 0
Accepted
time: 2560ms
memory: 307248kb

input:

1902149 2964684

output:

967678819

result:

ok single line: '967678819'

Test #33:

score: 0
Accepted
time: 5360ms
memory: 422680kb

input:

2906083 2818150

output:

800415339

result:

ok single line: '800415339'

Test #34:

score: 0
Accepted
time: 1184ms
memory: 244444kb

input:

947213 2843556

output:

704887450

result:

ok single line: '704887450'

Test #35:

score: 0
Accepted
time: 2257ms
memory: 301264kb

input:

1068401 2866144

output:

643696823

result:

ok single line: '643696823'

Test #36:

score: 0
Accepted
time: 5000ms
memory: 424228kb

input:

2097152 2706726

output:

879969833

result:

ok single line: '879969833'

Test #37:

score: 0
Accepted
time: 2503ms
memory: 313844kb

input:

1594323 1385645

output:

166289728

result:

ok single line: '166289728'

Test #38:

score: 0
Accepted
time: 2592ms
memory: 315404kb

input:

1953125 2702955

output:

356924892

result:

ok single line: '356924892'

Test #39:

score: 0
Accepted
time: 1156ms
memory: 243496kb

input:

823543 469787

output:

217410289

result:

ok single line: '217410289'

Test #40:

score: 0
Accepted
time: 2538ms
memory: 312364kb

input:

1771561 2338083

output:

17307265

result:

ok single line: '17307265'

Test #41:

score: 0
Accepted
time: 570ms
memory: 206548kb

input:

371293 2739508

output:

768654866

result:

ok single line: '768654866'

Test #42:

score: 0
Accepted
time: 211ms
memory: 175756kb

input:

1278612 1

output:

1

result:

ok single line: '1'

Test #43:

score: 0
Accepted
time: 510ms
memory: 183944kb

input:

2949300 1

output:

1

result:

ok single line: '1'