QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#531860#6522. Digit ModehewanyingAC ✓633ms3876kbC++142.3kb2024-08-24 22:43:592024-08-24 22:44:00

Judging History

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

  • [2024-08-24 22:44:00]
  • 评测
  • 测评结果:AC
  • 用时:633ms
  • 内存:3876kb
  • [2024-08-24 22:43:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define poly vector<int>

const int N=105,H=1e9+7;
int n,ct[N],fac[N],ifac[N],c[N],ans=0,a[N];
string s;

int adc(int a,int b){return a+b>=H?a+b-H:a+b;}
int dec(int a,int b){return a<b?a-b+H:a-b;}
int mul(int a,int b){return 1ll*a*b%H;}
void add(int &a,int b){a=adc(a,b);}
void del(int &a,int b){a=dec(a,b);}

int qpow(int a,int b=H-2){
  int res=1;
  while(b){if(b&1) res=mul(res,a);a=mul(a,a),b>>=1;}
  return res;
}

void init(){
  fac[0]=1;
  for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i);
  ifac[N-1]=qpow(fac[N-1]);
  for(int i=N-1;i>=1;i--) ifac[i-1]=mul(ifac[i],i);
}

int binom(int n,int m){
  if(m<0||n<m) return 0;
  return mul(fac[n],mul(ifac[m],ifac[n-m]));
}

poly operator *(poly a,poly b){
  int len=(int)a.size();
  poly c(len);
  for(int i=0;i<len;i++) for(int j=0;j<len-i;j++) add(c[i+j],mul(a[i],b[j]));
  return c;
}

int calc(int len){
  poly res(len+1);res[0]=1;

  for(int i=0;i<10;i++){
    if(!ct[i]) continue;
	poly cur(len+1);
	cur[0]=1;
	for(int j=1;j<=min(len,ct[i]);j++) cur[j]=ifac[j];
	res=res*cur;
  }
  return mul(res[len],fac[len]);
}

int chk(int x,int y,int len){
  
  for(int i=0;i<10;i++) if(c[i]>y||(c[i]==y&&i>x)) return 0;
  if(len-y+c[x]<0) return 0;

  for(int i=0;i<10;i++){
	ct[i]=0;
	if(i==x) continue;
	if(i<x) ct[i]=y-c[i];
	else ct[i]=y-1-c[i];
  }
  int res=calc(len-y+c[x]);
  return mul(x,mul(res,binom(len,y-c[x])));
}

void Do(int id,int i){
  c[i]++;
  for(int j=0;j<10;j++)
    for(int k=1;k<=n;k++)
	  add(ans,chk(j,k,n-id));
  c[i]--;
}

void dfs(int id,bool lim){
  if(id==n+1){
	if(lim){
	  int nw=0,mx=0;
	  for(int i=0;i<10;i++) if(c[i]>=mx) mx=c[i],nw=i;
	  add(ans,nw);
	}
	return;
  }

  if(!lim){
	for(int i=1;i<10;i++) Do(id,i);
	return dfs(id+1,0);
  }
  if(id==1){
	dfs(id+1,0);
	for(int i=1;i<a[id];i++) Do(id,i);
  }else{
	for(int i=0;i<a[id];i++) Do(id,i);
  }
  c[a[id]]++,dfs(id+1,1),c[a[id]]--;
}

void SOLVE(){
  cin>>s;n=(int)s.size();ans=0;

  for(int i=0;i<10;i++) c[i]=ct[i]=0;
  for(int i=1;i<=n;i++) a[i]=s[i-1]-'0';

  dfs(1,1);
  cout<<ans<<'\n';
}

int main(){
  /*2024.8.24 H_W_Y P9640 [SNCPC2019] Digit Mode 数位 dp*/
  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  int _;cin>>_;init();
  while(_--) SOLVE();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3556kb

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: 2ms
memory: 3600kb

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: 3ms
memory: 3568kb

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: 5ms
memory: 3600kb

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: 7ms
memory: 3856kb

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: 10ms
memory: 3620kb

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: 37ms
memory: 3512kb

input:

3
5014252832385738
8762796162648653
919997886706385

output:

892033338
297722019
462512414

result:

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

Test #9:

score: 0
Accepted
time: 91ms
memory: 3592kb

input:

2
775701797726112292362823101
75927988177061355614

output:

371275551
566830847

result:

ok 2 number(s): "371275551 566830847"

Test #10:

score: 0
Accepted
time: 586ms
memory: 3600kb

input:

1
65760982925996012426370962570581226245366145016666

output:

661063035

result:

ok 1 number(s): "661063035"

Test #11:

score: 0
Accepted
time: 600ms
memory: 3636kb

input:

1
62597468169905757754175023836706426691470692832490

output:

9983261

result:

ok 1 number(s): "9983261"

Test #12:

score: 0
Accepted
time: 612ms
memory: 3604kb

input:

1
78912847369504885593964702297317051208901751786824

output:

817123221

result:

ok 1 number(s): "817123221"

Test #13:

score: 0
Accepted
time: 618ms
memory: 3624kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"

Test #14:

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

input:

1
999999999999999999999999999999999999999999999

output:

439421821

result:

ok 1 number(s): "439421821"

Test #15:

score: 0
Accepted
time: 275ms
memory: 3640kb

input:

1
9999999999999999999999999999999999999999

output:

387537647

result:

ok 1 number(s): "387537647"

Test #16:

score: 0
Accepted
time: 618ms
memory: 3636kb

input:

1
99999999999999999999999998889999898988888889998888

output:

909431898

result:

ok 1 number(s): "909431898"

Test #17:

score: 0
Accepted
time: 620ms
memory: 3600kb

input:

1
99999999999999999999999998989899988889989889999888

output:

289727470

result:

ok 1 number(s): "289727470"

Test #18:

score: 0
Accepted
time: 633ms
memory: 3796kb

input:

1
99999999999999999999999998998988898888898889898999

output:

962896416

result:

ok 1 number(s): "962896416"

Test #19:

score: 0
Accepted
time: 270ms
memory: 3876kb

input:

1
9999999999999999999989988898888989888899

output:

995903330

result:

ok 1 number(s): "995903330"

Test #20:

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

input:

1
9999999999999999999989999889889998998898

output:

385460258

result:

ok 1 number(s): "385460258"

Test #21:

score: 0
Accepted
time: 618ms
memory: 3816kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"