QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65821#4843. Infectious Diseaseclf123456AC ✓3865ms243328kbC++201.3kb2022-12-03 18:42:402022-12-03 18:42:40

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-03 18:42:40]
  • 评测
  • 测评结果:AC
  • 用时:3865ms
  • 内存:243328kb
  • [2022-12-03 18:42:40]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long

using namespace std;
const int maxn=1e7+5e6;
const int mod=1e9+7;
int jc[maxn],inv[maxn],n;
int mp[maxn];
struct hash_map{
	int operator()(const pair<int,int>& p) const{
		return p.first+mp[p.second]*maxn;
	}
};
unordered_map<pair<int,int>,int,hash_map>dp;


int C(int n,int m){
	return jc[n]*inv[n-m]%mod*inv[m]%mod;
}
int ksm(int x,int p,int mod){
	int ans=1;
	while(p){
		if(p&1) ans=ans*x%mod;
		x=x*x%mod;
		p>>=1;
	}
	return ans;
}
int dfs(int x,int y){
	if(x==0) return 0;
	pair<int,int>t=make_pair(x,y);
	if(dp.find(t)!=dp.end()) return dp[t];
	int r=min(2*x,n-y);
	int z=min(2*y,n-y);
	int sum=n-y;
	int mn=max(0LL,r+z-sum);
	int mx=min(r,z);
	int ans=1;
	int fm=ksm(C(n-y,z),mod-2,mod);
	for(int i=mn;i<=mx;i++){
		ans+=C(r,i)*C(n-y-r,z-i)%mod*dfs(r-i,z+y)%mod*fm%mod;
		ans%=mod;
	}
	return dp[t]=ans;
}
void init(){
	jc[0]=inv[0]=1;
	for(int i=1;i<maxn;i++) jc[i]=i*jc[i-1]%mod;
	inv[maxn-1]=ksm(jc[maxn-1],mod-2,mod);
	for(int i=maxn-2;i>=1;i--){
		inv[i]=(i+1)*inv[i+1]%mod;
	}
	mp[1]=0;
	int base=3;
	for(int i=1;;i++){
		if(base>=n){
			mp[n]=i;
			break;
		}
		mp[base]=i;
		//cout<<base<<" "<<mp[base]<<endl;
		base*=3;
		
	}
}
signed main(){
	//memset(dp,-1,sizeof(dp));
	cin>>n;
	init();
	
	int base=1;
	
	cout<<dfs(1,1)<<endl;
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 210ms
memory: 238524kb

input:

2

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 161ms
memory: 237980kb

input:

114

output:

505208013

result:

ok 1 number(s): "505208013"

Test #3:

score: 0
Accepted
time: 3865ms
memory: 243328kb

input:

14000000

output:

44565093

result:

ok 1 number(s): "44565093"

Test #4:

score: 0
Accepted
time: 3846ms
memory: 241384kb

input:

12345678

output:

123143093

result:

ok 1 number(s): "123143093"

Test #5:

score: 0
Accepted
time: 363ms
memory: 239300kb

input:

776777

output:

764022068

result:

ok 1 number(s): "764022068"

Test #6:

score: 0
Accepted
time: 151ms
memory: 239520kb

input:

3

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 184ms
memory: 239148kb

input:

4

output:

666666673

result:

ok 1 number(s): "666666673"

Test #8:

score: 0
Accepted
time: 177ms
memory: 238336kb

input:

5

output:

833333341

result:

ok 1 number(s): "833333341"

Test #9:

score: 0
Accepted
time: 194ms
memory: 238280kb

input:

6

output:

300000004

result:

ok 1 number(s): "300000004"

Test #10:

score: 0
Accepted
time: 170ms
memory: 238992kb

input:

7

output:

533333339

result:

ok 1 number(s): "533333339"

Test #11:

score: 0
Accepted
time: 170ms
memory: 239072kb

input:

8

output:

952380961

result:

ok 1 number(s): "952380961"

Test #12:

score: 0
Accepted
time: 168ms
memory: 238344kb

input:

9

output:

964285723

result:

ok 1 number(s): "964285723"

Test #13:

score: 0
Accepted
time: 160ms
memory: 238704kb

input:

10

output:

416666672

result:

ok 1 number(s): "416666672"

Test #14:

score: 0
Accepted
time: 203ms
memory: 239532kb

input:

26

output:

990086590

result:

ok 1 number(s): "990086590"

Test #15:

score: 0
Accepted
time: 164ms
memory: 238020kb

input:

27

output:

528360342

result:

ok 1 number(s): "528360342"

Test #16:

score: 0
Accepted
time: 190ms
memory: 239488kb

input:

28

output:

273239648

result:

ok 1 number(s): "273239648"

Test #17:

score: 0
Accepted
time: 1143ms
memory: 239400kb

input:

4782967

output:

332194401

result:

ok 1 number(s): "332194401"

Test #18:

score: 0
Accepted
time: 1132ms
memory: 238748kb

input:

4782968

output:

362625027

result:

ok 1 number(s): "362625027"

Test #19:

score: 0
Accepted
time: 1105ms
memory: 239356kb

input:

4782969

output:

971032452

result:

ok 1 number(s): "971032452"

Test #20:

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

input:

4782970

output:

452836984

result:

ok 1 number(s): "452836984"

Test #21:

score: 0
Accepted
time: 1125ms
memory: 239584kb

input:

4782971

output:

349324970

result:

ok 1 number(s): "349324970"

Test #22:

score: 0
Accepted
time: 1150ms
memory: 238944kb

input:

4782972

output:

46862963

result:

ok 1 number(s): "46862963"

Test #23:

score: 0
Accepted
time: 188ms
memory: 238416kb

input:

114514

output:

972669965

result:

ok 1 number(s): "972669965"

Test #24:

score: 0
Accepted
time: 1115ms
memory: 238804kb

input:

1919810

output:

482430785

result:

ok 1 number(s): "482430785"

Test #25:

score: 0
Accepted
time: 3779ms
memory: 239972kb

input:

8877877

output:

486769120

result:

ok 1 number(s): "486769120"