QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#167030#6522. Digit Modeucup-team134#AC ✓674ms3960kbC++144.1kb2023-09-06 23:21:322023-09-06 23:21:33

Judging History

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

  • [2023-09-06 23:21:33]
  • 评测
  • 测评结果:AC
  • 用时:674ms
  • 内存:3960kb
  • [2023-09-06 23:21:32]
  • 提交

answer

#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int mod=1e9+7;
int add(int x,int y){x+=y;return x>=mod?x-mod:x;}
int sub(int x,int y){x-=y;return x<0?x+mod:x;}
int mul(int x,int y){return (ll)x*y%mod;}
void ckadd(int&x,int y){x=add(x,y);}
void cksub(int&x,int y){x=sub(x,y);}

const int N=55;
char s[N];
int cnt[10];
int dp[11][N];

int F[N],I[N];
void calc(){
	F[0]=1;
	for(int i=1;i<N;i++)F[i]=mul(F[i-1],i);
	I[0]=I[1]=1;
	for(int i=2;i<N;i++)I[i]=mul(mod-mod/i,I[mod%i]);
	for(int i=1;i<N;i++)I[i]=mul(I[i],I[i-1]);
}
int binom(int n,int k){
	return mul(F[n],mul(I[k],I[n-k]));
}

int DP(int n,int best,int take,int when){
	for(int j=0;j<=10;j++){
		for(int z=0;z<=n;z++){
			dp[j][z]=0;
		}
	}
	dp[0][0]=1;
	int totL=0,totR=0;
	int sb=0;
	for(int cif=0;cif<10;cif++){
		int L=0,R=cnt[best]+take-cnt[cif];
		if(best<cif)R--;
		R=min(R,n);
		if(cif==best){
			L=take;
			R=take;
		}
		if(cif>=when){
			totL=max(totL,1);
			sb=1;
		}
		for(int z=totL;z<=totR;z++){
			for(int j=L;j<=R;j++){
				if(z+j<=n){
					ckadd(dp[cif+1][z+j],mul(dp[cif][z],binom(z+j-sb,j)));
				}
			}
		}
		totL+=L;
		totR+=R;
	}
	return dp[10][n];
}

map<int,int> d;
int Full(int n){
	if(d.count(n))return d[n];
	int ans=0;
	//for(int c=1;c<=9;c++){
	//	cnt[c]++;
	cnt[0]++;
	for(int best=0;best<=9;best++){
		int mn=0;
		for(int i=0;i<10;i++){
			mn=max(mn,cnt[i]+(i>best?1:0));
		}
		int L=max(0,mn-cnt[best]);
		int R=n-1;
		for(int take=L;take<=R;take++){
			cksub(ans,mul(best,DP(n-1,best,take,10)));

		}
	}
	cnt[0]--;

		for(int best=0;best<=9;best++){
			int mn=0;
			for(int i=0;i<10;i++){
				mn=max(mn,cnt[i]+(i>best?1:0));
			}
			int L=max(0,mn-cnt[best]);
			int R=n;
			for(int take=L;take<=R;take++){
				ckadd(ans,mul(best,DP(n,best,take,10)));

				/*for(int j=0;j<=10;j++){
					for(int z=0;z<=n-1;z++){
						dp[j][z]=0;
					}
				}
				dp[0][0]=1;
				for(int cif=0;cif<10;cif++){
					int L=0,R=cnt[best]+take-cnt[cif];
					if(best<cif)R--;
					R=min(R,n-1);
					if(cif==best){
						L=take;
						R=take;
					}
					for(int j=L;j<=R;j++){
						for(int z=j;z<=n-1;z++){
							ckadd(dp[cif+1][z],mul(dp[cif][z-j],binom(z,j)));
						}
					}
				}
				ckadd(ans,mul(best,dp[10][n-1]));*/
			}
		}
		//cnt[c]--;
	//}
	return d[n]=ans;
}
int main(){
	calc();
	int t;
	scanf("%i",&t);
	while(t--){
		scanf("%s",s+1);
		int ans=0;
		int n=strlen(s+1);
		for(int i=1;i<n;i++){
			ckadd(ans,Full(i));
		}
		for(int i=1;i<=n;i++){
			int now=s[i]-'0';
			if(i==1){
				cnt[0]++;
				for(int best=0;best<=9;best++){
					int mn=0;
					for(int i=0;i<10;i++){
						mn=max(mn,cnt[i]+(i>best?1:0));
					}
					int L=max(0,mn-cnt[best]);
					int R=n-i;
					for(int take=L;take<=R;take++){
						cksub(ans,mul(best,DP(n-i,best,take,10)));
					}
				}
				cnt[0]--;
			}
			//for(int c=0;c<now;c++){
			//	if(i==1 && c==0)continue;
			//	cnt[c]++;
				for(int best=0;best<=9;best++){
					int mn=0;
					for(int i=0;i<10;i++){
						mn=max(mn,cnt[i]+(i>best?1:0));
					}
					int L=max(0,mn-cnt[best]);
					int R=n-i+1;
					for(int take=L;take<=R;take++){
						ckadd(ans,mul(best,DP(n-i+1,best,take,now)));
						/*for(int j=0;j<=10;j++){
							for(int z=0;z<=n-i;z++){
								dp[j][z]=0;
							}
						}
						dp[0][0]=1;
						for(int cif=0;cif<10;cif++){
							int L=0,R=cnt[best]+take-cnt[cif];
							if(best<cif)R--;
							R=min(R,n-i);
							if(cif==best){
								L=take;
								R=take;
							}
							for(int j=L;j<=R;j++){
								for(int z=j;z<=n-i;z++){
									ckadd(dp[cif+1][z],mul(dp[cif][z-j],binom(z,j)));
								}
							}
						}
						//printf("prefix:%i c:%i best:%i take:%i  dp:%i\n",i,c,best,take,dp[10][n-i]);
						ckadd(ans,mul(best,dp[10][n-i]));*/
					}
				}
				//cnt[c]--;
			//}
			cnt[now]++;
		}
		int mx=0;
		for(int i=1;i<10;i++){
			if(cnt[i]>=cnt[mx])mx=i;
		}
		ckadd(ans,mx);
		printf("%i\n",ans);

		for(int i=0;i<10;i++)cnt[i]=0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3772kb

input:

5
9
99
999
99999
999999

output:

45
615
6570
597600
5689830

result:

ok 5 number(s): "45 615 6570 597600 5689830"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3812kb

input:

34
7
48
8
76
1
97
7
5
7
7
2
89
9
4
84
46
6
73
86
78
5
3
8
9
31
24
78
7
11
45
2
65
88
6

output:

28
236
36
420
1
597
28
15
28
28
3
525
45
10
484
221
21
399
500
435
15
6
36
45
145
104
435
28
47
215
3
341
516
21

result:

ok 34 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3952kb

input:

16
935
888
429
370
499
881
285
162
178
948
205
858
573
249
773
615

output:

6009
5618
2456
2078
2905
5562
1603
887
993
6121
1174
5378
3333
1374
4724
3631

result:

ok 16 numbers

Test #4:

score: 0
Accepted
time: 2ms
memory: 3960kb

input:

12
1242
9985
6469
9310
4191
9497
3166
3495
9711
9698
4137
2257

output:

7292
63531
37910
58047
23987
59479
18076
19675
61184
61086
23672
12913

result:

ok 12 numbers

Test #5:

score: 0
Accepted
time: 2ms
memory: 3768kb

input:

10
61195
72739
10164
79164
57851
12326
29132
55992
67377
13873

output:

337575
408170
63792
450686
316513
70493
157773
305011
374163
77954

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 3ms
memory: 3956kb

input:

8
529983
127270
421121
291729
461233
695056
365028
271160

output:

2744573
687141
2160067
1500426
2359204
3705475
1851172
1381981

result:

ok 8 numbers

Test #7:

score: 0
Accepted
time: 2ms
memory: 3828kb

input:

7
7934351
8474057
1287369
5845624
7796773
5805755
7349121

output:

42465725
45668947
6716401
30094426
41554096
29861098
38756757

result:

ok 7 numbers

Test #8:

score: 0
Accepted
time: 17ms
memory: 3896kb

input:

3
5014252832385738
8762796162648653
919997886706385

output:

892033338
297722019
462512414

result:

ok 3 number(s): "892033338 297722019 462512414"

Test #9:

score: 0
Accepted
time: 72ms
memory: 3912kb

input:

2
775701797726112292362823101
75927988177061355614

output:

371275551
566830847

result:

ok 2 number(s): "371275551 566830847"

Test #10:

score: 0
Accepted
time: 672ms
memory: 3900kb

input:

1
65760982925996012426370962570581226245366145016666

output:

661063035

result:

ok 1 number(s): "661063035"

Test #11:

score: 0
Accepted
time: 674ms
memory: 3772kb

input:

1
62597468169905757754175023836706426691470692832490

output:

9983261

result:

ok 1 number(s): "9983261"

Test #12:

score: 0
Accepted
time: 669ms
memory: 3900kb

input:

1
78912847369504885593964702297317051208901751786824

output:

817123221

result:

ok 1 number(s): "817123221"

Test #13:

score: 0
Accepted
time: 642ms
memory: 3956kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"

Test #14:

score: 0
Accepted
time: 429ms
memory: 3952kb

input:

1
999999999999999999999999999999999999999999999

output:

439421821

result:

ok 1 number(s): "439421821"

Test #15:

score: 0
Accepted
time: 273ms
memory: 3960kb

input:

1
9999999999999999999999999999999999999999

output:

387537647

result:

ok 1 number(s): "387537647"

Test #16:

score: 0
Accepted
time: 646ms
memory: 3892kb

input:

1
99999999999999999999999998889999898988888889998888

output:

909431898

result:

ok 1 number(s): "909431898"

Test #17:

score: 0
Accepted
time: 647ms
memory: 3908kb

input:

1
99999999999999999999999998989899988889989889999888

output:

289727470

result:

ok 1 number(s): "289727470"

Test #18:

score: 0
Accepted
time: 647ms
memory: 3904kb

input:

1
99999999999999999999999998998988898888898889898999

output:

962896416

result:

ok 1 number(s): "962896416"

Test #19:

score: 0
Accepted
time: 273ms
memory: 3792kb

input:

1
9999999999999999999989988898888989888899

output:

995903330

result:

ok 1 number(s): "995903330"

Test #20:

score: 0
Accepted
time: 274ms
memory: 3776kb

input:

1
9999999999999999999989999889889998998898

output:

385460258

result:

ok 1 number(s): "385460258"

Test #21:

score: 0
Accepted
time: 643ms
memory: 3820kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"