QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121905#6522. Digit ModeSorting#WA 176ms3540kbC++203.5kb2023-07-09 00:06:292023-07-09 00:06:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-09 00:06:33]
  • 评测
  • 测评结果:WA
  • 用时:176ms
  • 内存:3540kb
  • [2023-07-09 00:06:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MOD = 1e9 + 7 ;
const int MAXN = 57 ;

int n ;
string a ;
ll ans ;
int freq[ 10 ] ;
ll comb[ MAXN ][ MAXN ] ;
int rem ;
ll precalc[ MAXN ] ;

int lim[ 10 ] ;
ll dp[ 11 ][ MAXN ] ;

void do_dp ( int rem ) {
    for ( int i = 0 ; i <= 10 ; ++ i ) {
        for ( int j = 0 ; j <= rem ; ++ j ) {
            dp[ i ][ j ] = 0 ;
        }
    }
    dp[ 0 ][ 0 ] = 1 ;
    for ( int i = 0 ; i < 10 ; ++ i ) {
        for ( int j = 0 ; j <= rem ; ++ j ) {
            for ( int t = 0 ; t <= min ( rem - j , lim[ i ] ) ; ++ t ) {
                dp[ i + 1 ][ j + t ] = ( dp[ i + 1 ][ j + t ] + dp[ i ][ j ] * comb[ rem - j ][ t ] ) % MOD ;
            }
        }
    }
}

ll proc ( int len , int rem , int spec ) {
    int cnt = 0 ;
    for ( int i = 0 ; i < 10 ; ++ i ) {
        cnt = max ( cnt , freq[ i ] ) ;
    }
    cnt = max ( cnt , ( len + 9 ) / 10 ) ;
    ll ret = 0 ;
    for ( int targ = cnt ; targ <= freq[ spec ] + rem ; ++ targ ) {
        ll add = comb[ rem ][ targ - freq[ spec ] ] ;
        bool bad = false ;
        for ( int i = 0 ; i < 10 ; ++ i ) {
            if ( i == spec ) { lim[ i ] = 0 ; }
            else if ( i < spec ) { lim[ i ] = targ - freq[ i ] ; }
            else { lim[ i ] = targ - freq[ i ] - 1 ; }
            if ( lim[ i ] < 0 ) { bad = true ; }
        }
        if ( bad == true ) { continue ; }
        do_dp ( rem - ( targ - freq[ spec ] ) ) ;
        add = ( add * dp[ 10 ][ rem - ( targ - freq[ spec ] ) ] ) % MOD ;
        ret = ( ret + add ) % MOD ;
    }
    return ret ;
}

void calc ( int spec ) {
    for ( int i = 0 ; i < 10 ; ++ i ) { freq[ i ] = 0 ; }
    for ( int eq = 0 ; eq < n ; ++ eq ) {
        for ( int nxt = 0 ; nxt < a[ eq ] - '0' ; ++ nxt ) {
            if ( nxt == 0 && eq == 0 ) { continue ; }
            ++ freq[ nxt ] ;
            ans = ( ans + proc ( n , n - eq - 1 , spec ) * spec ) % MOD ;
            -- freq[ nxt ] ;
        }
        ++ freq[ ( a[ eq ] - '0' ) ] ;
    }
}


void solve ( ) {
    cin >> a ;
    n = a.size ( ) ;
    ans = 0 ;
    map < int , int > cnt ;
    for ( int i = 0 ; i < n ; ++ i ) {
        ++ cnt[ ( a[ i ] - '0' ) ] ;
    }
    int mx = 0 ;
    for ( auto [ x , hh ] : cnt ) {
        if ( mx <= hh ) { mx = hh ; ans = x ; }
    }
    for ( int spec = 1 ; spec <= 9 ; ++ spec ) {
        calc ( spec ) ;
    }
    for ( int i = 1 ; i < n ; ++ i ) {
        ans += precalc[ i ] ;
    }
    cout << ans << "\n" ;
}

void init ( ) {
    comb[ 0 ][ 0 ] = 1 ;
    for ( int i = 1 ; i < MAXN ; ++ i ) {
        for ( int j = 0 ; j < MAXN ; ++ j ) {
            comb[ i ][ j ] = comb[ i - 1 ][ j ] ;
            if ( j > 0 ) {
                comb[ i ][ j ] = ( comb[ i ][ j ] + comb[ i - 1 ][ j - 1 ] ) % MOD ;
            }
        }
    }
    for ( int i = 1 ; i <= 50 ; ++ i ) {
        for ( int fst = 1 ; fst <= 9 ; ++ fst ) {
            ++ freq[ fst ] ;
            for ( int spec = 0 ; spec <= 9 ; ++ spec ) {
                precalc[ i ] = ( precalc[ i ] + proc ( i , i - 1 , spec ) * spec ) % MOD ;
            }
            -- freq[ fst ] ;
        }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    init ( ) ;
    int t = 1 ; cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 172ms
memory: 3496kb

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

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

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

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

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

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

input:

7
7934351
8474057
1287369
5845624
7796773
5805755
7349121

output:

42465725
45668947
6716401
30094426
41554096
29861098
38756757

result:

ok 7 numbers

Test #8:

score: -100
Wrong Answer
time: 176ms
memory: 3444kb

input:

3
5014252832385738
8762796162648653
919997886706385

output:

3892033359
4297722047
3462512435

result:

wrong answer 1st numbers differ - expected: '892033338', found: '3892033359'