QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#308243#8015. 鸡do_while_true100 ✓0ms3668kbC++143.7kb2024-01-19 19:36:522024-01-19 19:36:52

Judging History

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

  • [2024-01-19 19:36:52]
  • 评测
  • 测评结果:100
  • 用时:0ms
  • 内存:3668kb
  • [2024-01-19 19:36:52]
  • 提交

answer

//O(n)
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
int mod,inv6;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return s;
}
const int N=3010;
int n,m;
int f[N],g[N],d[10];
int ans;
void calc1(){//c[i]>0
	int s=add(f[n-2],f[n-2]);
	for(int i=2;i<n;i++)cadd(s,1ll*f[i-2]*f[n-i-1]%mod);
	cadd(ans,1ll*s*((1ll*m*(m-1)/2))%mod);
}
int i1(int m){
	return 1ll*m*(m+1)/2%mod;
}
int i2(int m){
	ll x=1ll*m*(m+1)/2;
	if(x%3==0)return x/3%mod*(2*m+1)%mod;
	return (2*m+1)/3*x%mod;
}
int i3(int m){return 1ll*i1(m)*i1(m)%mod;}
void calc2(){//减去c[i]>0多算的
	int sum=0;
	int sum0=(n&1),sum1=0,sum2=0;
	for(int i=1;i<=n;i+=2){
		//{[x0x0x]0a|0...}
		if(i+2<=n)cadd(sum1,f[max(0,n-i-3)]);
		//{..0|a0[x0x0x]0b|0..}
		if(i+4<=n)cadd(sum2,g[max(0,n-i-4)]);
	}
	cadd(sum,1ll*sum0*(1ll*m*(m-1)/2%mod)%mod);
	sum1=add(sum1,sum1);
//	for(int i=1;i<=m;i++)
//		cadd(sum,1ll*sum1*(i-1)%mod*(i-1)%mod);
	cadd(sum,1ll*m*sum1%mod);
	cdel(sum,2ll*sum1%mod*i1(m)%mod);
	cadd(sum,1ll*sum1%mod*i2(m)%mod);
//	for(int i=1;i<=m;i++)
//		cadd(sum,1ll*sum2*(i-1)%mod*(i-1)%mod*(i-1)%mod);
	cadd(sum,1ll*sum2*i3(m)%mod);
	cdel(sum,3ll*sum2%mod*i2(m)%mod);
	cadd(sum,3ll*sum2%mod*i1(m)%mod);
	cdel(sum,1ll*sum2*m%mod);
	cdel(ans,sum);
}
void calc3(){//c[i]=0
	int sum=0;
	int sum1=0,sum2=0;
	for(int i=2;i<=n;i++){
		//{[0000]a|0...}
		if(i+1<=n)cadd(sum1,2ll*((i-1)/2)*m%mod*f[max(0,n-i-2)]%mod);
		//{..0|a[00000]b|0..}
		if(i+2<=n)cadd(sum2,1ll*((i-2)/2)*m%mod*g[max(0,n-i-2)]%mod);
	}
	cadd(sum,1ll*n/2*m%mod);
	cadd(sum,1ll*sum1*m%mod);
	cadd(sum,1ll*sum2*m%mod*m%mod);
	cadd(ans,sum);
}
signed main(){
//	#ifdef do_while_true
//		assert(freopen("chicken.in","r",stdin));
//		assert(freopen("chicken.out","w",stdout));
//	#endif
	read(n,m,mod);
	f[0]=1;f[1]=m+1;
	for(int i=2;i<=n;i++)f[i]=add(f[i-1],1ll*m*f[i-2]%mod);
//	for(int i=0;i<=n;i++)
//		for(int j=0;i+j<=n;j++)
//			cadd(g[i+j],1ll*f[max(i-1,0)]*f[max(j-1,0)]%mod);
	g[0]=1;
	d[1]=2;
	d[2]=(2ll*m-1)%mod;
	d[3]=del(0,2ll*m%mod);
	d[4]=del(0,1ll*m*m%mod);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=4;j++)
			cadd(g[i],1ll*d[j]*g[i-j]%mod);
	}
	calc1();
	calc2();
	calc3();
	cadd(ans,f[n]);
	cout<<ans<<'\n';
    #ifdef do_while_true
//		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}

詳細信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 3524kb

input:

5 5 1004326439

output:

1281

result:

ok 1 number(s): "1281"

Test #2:

score: 5
Accepted
time: 0ms
memory: 3664kb

input:

5 4 1002682123

output:

649

result:

ok 1 number(s): "649"

Test #3:

score: 5
Accepted
time: 0ms
memory: 3588kb

input:

287 1 1003060133

output:

328406329

result:

ok 1 number(s): "328406329"

Test #4:

score: 5
Accepted
time: 0ms
memory: 3652kb

input:

279 1 1004432189

output:

222258837

result:

ok 1 number(s): "222258837"

Test #5:

score: 5
Accepted
time: 0ms
memory: 3524kb

input:

300 1 1005912203

output:

707086810

result:

ok 1 number(s): "707086810"

Test #6:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

288 5 1003307827

output:

964512417

result:

ok 1 number(s): "964512417"

Test #7:

score: 5
Accepted
time: 0ms
memory: 3484kb

input:

281 5 1008854383

output:

755282155

result:

ok 1 number(s): "755282155"

Test #8:

score: 5
Accepted
time: 0ms
memory: 3512kb

input:

270 5 1007619367

output:

431828317

result:

ok 1 number(s): "431828317"

Test #9:

score: 5
Accepted
time: 0ms
memory: 3568kb

input:

292 5 1002449813

output:

183613546

result:

ok 1 number(s): "183613546"

Test #10:

score: 5
Accepted
time: 0ms
memory: 3664kb

input:

300 5 1005897091

output:

915308166

result:

ok 1 number(s): "915308166"

Test #11:

score: 5
Accepted
time: 0ms
memory: 3572kb

input:

45 50 1009100993

output:

940158800

result:

ok 1 number(s): "940158800"

Test #12:

score: 5
Accepted
time: 0ms
memory: 3648kb

input:

49 50 1001428049

output:

1045902

result:

ok 1 number(s): "1045902"

Test #13:

score: 5
Accepted
time: 0ms
memory: 3512kb

input:

49 50 1007851073

output:

922264698

result:

ok 1 number(s): "922264698"

Test #14:

score: 5
Accepted
time: 0ms
memory: 3520kb

input:

50 50 1005625571

output:

442192770

result:

ok 1 number(s): "442192770"

Test #15:

score: 5
Accepted
time: 0ms
memory: 3480kb

input:

290 300 1005068699

output:

484359497

result:

ok 1 number(s): "484359497"

Test #16:

score: 5
Accepted
time: 0ms
memory: 3568kb

input:

270 300 1003440637

output:

899894137

result:

ok 1 number(s): "899894137"

Test #17:

score: 5
Accepted
time: 0ms
memory: 3512kb

input:

300 300 1008561979

output:

33407754

result:

ok 1 number(s): "33407754"

Test #18:

score: 5
Accepted
time: 0ms
memory: 3668kb

input:

2991 3000 1004658859

output:

167444547

result:

ok 1 number(s): "167444547"

Test #19:

score: 5
Accepted
time: 0ms
memory: 3608kb

input:

2870 3000 1004054173

output:

860666062

result:

ok 1 number(s): "860666062"

Test #20:

score: 5
Accepted
time: 0ms
memory: 3588kb

input:

3000 3000 1009539589

output:

696222334

result:

ok 1 number(s): "696222334"