QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#275321#6522. Digit Modeucup-team1321#AC ✓233ms3856kbC++232.9kb2023-12-04 16:42:462023-12-04 16:42:46

Judging History

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

  • [2023-12-04 16:42:46]
  • 评测
  • 测评结果:AC
  • 用时:233ms
  • 内存:3856kb
  • [2023-12-04 16:42:46]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int N=55;
const int MOD=1000000007;
int qpow(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b&1) res=(long long)res*a%MOD;
        a=(long long)a*a%MOD,b>>=1;
    }
    return res;
}
int getinv(int x)
{
    return qpow(x,MOD-2);
}
int fac[N],ifac[N];
void init(int n=50)
{
    fac[0]=1;
    for(int i=1;i<=n;i++)
        fac[i]=(long long)fac[i-1]*i%MOD;
    ifac[n]=getinv(fac[n]);
    for(int i=n;i>=1;i--)
        ifac[i-1]=(long long)ifac[i]*i%MOD;
    return;
}
int a[N];
int s[N];
int n;
int f[10][N];
int calc(int p)
{
    int cnt[10]={};
    for(int i=1;i<=p;i++)
        cnt[s[i]]++;
    for(int i=1;i<=p&&s[i]==0;i++)
        cnt[s[i]]--;
    if(p==n)
    {
        int mx=*max_element(cnt,cnt+9+1);
        if(mx==0) return 0;
        for(int i=9;i>=0;i--)
            if(cnt[i]==mx) return i;
        return 0;
    }
    int res=0;
    for(int m=1;m<=9;m++)
    {
        for(int c=0;c<=n-p;c++)
        {
            int ret=n-p-c;
            bool flag=true;
            vector<int>v;
            for(int i=0;i<=9;i++)
                if(i!=m)
                {
                    int lim=c+cnt[m]-(i>m)-cnt[i];
                    v.emplace_back(i);
                    if(lim<0)
                    {
                        flag=false;
                        break;
                    }
                }
            if(!flag) continue;
            for(int i=0;i<=9;i++)
                for(int j=0;j<=ret;j++)
                    f[i][j]=0;
            f[0][0]=1;
            for(int i=1;i<=9;i++)
            {
                int lim=c+cnt[m]-(v[i-1]>m)-cnt[v[i-1]];
                for(int j=0;j<=ret;j++)
                    if(f[i-1][j])
                        for(int k=0;k<=lim&&j+k<=ret;k++)
                            f[i][j+k]=(f[i][j+k]+(long long)f[i-1][j]*ifac[k])%MOD;
            }
            res=(res+(long long)f[9][ret]*ifac[c]%MOD*fac[n-p]%MOD*m)%MOD;
        }
    }
    return res;
}
void solve()
{
    string str;
    cin>>str;
    n=str.size();
    for(int i=1;i<=n;i++)
        a[i]=str[i-1]-'0';
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(i>=2)
        {
            for(int j=1;j<=9;j++)
            {
                s[i]=j;
                ans=(ans+calc(i))%MOD;
            }
        }
        s[i]=0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=(i==1?1:0);j<a[i];j++)
        {
            s[i]=j;
            ans=(ans+calc(i))%MOD;
        }
        s[i]=a[i];
    }
    ans=(ans+calc(n))%MOD;
    cout<<ans<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    init();
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3572kb

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

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

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

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

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

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

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

input:

3
5014252832385738
8762796162648653
919997886706385

output:

892033338
297722019
462512414

result:

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

Test #9:

score: 0
Accepted
time: 35ms
memory: 3844kb

input:

2
775701797726112292362823101
75927988177061355614

output:

371275551
566830847

result:

ok 2 number(s): "371275551 566830847"

Test #10:

score: 0
Accepted
time: 204ms
memory: 3564kb

input:

1
65760982925996012426370962570581226245366145016666

output:

661063035

result:

ok 1 number(s): "661063035"

Test #11:

score: 0
Accepted
time: 209ms
memory: 3856kb

input:

1
62597468169905757754175023836706426691470692832490

output:

9983261

result:

ok 1 number(s): "9983261"

Test #12:

score: 0
Accepted
time: 212ms
memory: 3620kb

input:

1
78912847369504885593964702297317051208901751786824

output:

817123221

result:

ok 1 number(s): "817123221"

Test #13:

score: 0
Accepted
time: 233ms
memory: 3628kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"

Test #14:

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

input:

1
999999999999999999999999999999999999999999999

output:

439421821

result:

ok 1 number(s): "439421821"

Test #15:

score: 0
Accepted
time: 106ms
memory: 3844kb

input:

1
9999999999999999999999999999999999999999

output:

387537647

result:

ok 1 number(s): "387537647"

Test #16:

score: 0
Accepted
time: 232ms
memory: 3500kb

input:

1
99999999999999999999999998889999898988888889998888

output:

909431898

result:

ok 1 number(s): "909431898"

Test #17:

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

input:

1
99999999999999999999999998989899988889989889999888

output:

289727470

result:

ok 1 number(s): "289727470"

Test #18:

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

input:

1
99999999999999999999999998998988898888898889898999

output:

962896416

result:

ok 1 number(s): "962896416"

Test #19:

score: 0
Accepted
time: 116ms
memory: 3848kb

input:

1
9999999999999999999989988898888989888899

output:

995903330

result:

ok 1 number(s): "995903330"

Test #20:

score: 0
Accepted
time: 107ms
memory: 3560kb

input:

1
9999999999999999999989999889889998998898

output:

385460258

result:

ok 1 number(s): "385460258"

Test #21:

score: 0
Accepted
time: 228ms
memory: 3848kb

input:

1
99999999999999999999999999999999999999999999999999

output:

25251932

result:

ok 1 number(s): "25251932"