QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605094#2945. 1's For AllxyyyAC ✓5209ms4368kbC++171.1kb2024-10-02 15:28:112024-10-02 15:28:12

Judging History

This is the latest submission verdict.

  • [2024-10-02 15:28:12]
  • Judged
  • Verdict: AC
  • Time: 5209ms
  • Memory: 4368kb
  • [2024-10-02 15:28:11]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
int n, a[100010] = {0};

int main() {
    scanf("%d", &n);
    a[0] = 2e9;
    a[1] = 1;
    a[2] = 2;
    
    for (int i = 3; i <= n; i++) {
        a[i] = 2e9;
        for (int j = 1; j * 2 <= i; j++) {  // 优化:只需要遍历到 j <= i/2
            a[i] = min(a[i], a[j] + a[i - j]);
            if (i % j == 0) {  // 处理整除情况
                int k = i / j;
                a[i] = min(a[i], a[j] + a[k]);
            }
        }
        
        int ji = 10;
        for (int k = 1; k <= 5; k++) {  // 优化:按位分割的部分保持不变
            int aa = i % ji;
            int bb = i / ji;
            
            // 这些判断可以简化到一行:跳过无效的部分
            if ((ji == 100 && aa < 10) || (ji == 1000 && aa < 100) || (ji == 10000 && aa < 1000)) {
                ji *= 10;
                continue;
            }
            
            a[i] = min(a[i], a[aa] + a[bb]);
            ji *= 10;
        }
    }
    
    printf("%d", a[n]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5209ms
memory: 4292kb

input:

100000

output:

12

result:

ok single line: '12'

Test #2:

score: 0
Accepted
time: 4296ms
memory: 4284kb

input:

90909

output:

13

result:

ok single line: '13'

Test #3:

score: 0
Accepted
time: 50ms
memory: 3924kb

input:

10101

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 52ms
memory: 3888kb

input:

10001

output:

11

result:

ok single line: '11'

Test #5:

score: 0
Accepted
time: 5193ms
memory: 4368kb

input:

99999

output:

11

result:

ok single line: '11'

Test #6:

score: 0
Accepted
time: 5192ms
memory: 4232kb

input:

99998

output:

14

result:

ok single line: '14'

Test #7:

score: 0
Accepted
time: 5207ms
memory: 4164kb

input:

99997

output:

13

result:

ok single line: '13'

Test #8:

score: 0
Accepted
time: 5200ms
memory: 4308kb

input:

99989

output:

17

result:

ok single line: '17'

Test #9:

score: 0
Accepted
time: 5194ms
memory: 4352kb

input:

99988

output:

16

result:

ok single line: '16'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

2

output:

2

result:

ok single line: '2'

Test #11:

score: 0
Accepted
time: 5201ms
memory: 4356kb

input:

99984

output:

15

result:

ok single line: '15'

Test #12:

score: 0
Accepted
time: 5001ms
memory: 4312kb

input:

98069

output:

18

result:

ok single line: '18'

Test #13:

score: 0
Accepted
time: 4859ms
memory: 4152kb

input:

96721

output:

10

result:

ok single line: '10'

Test #14:

score: 0
Accepted
time: 2591ms
memory: 4004kb

input:

70549

output:

18

result:

ok single line: '18'

Test #15:

score: 0
Accepted
time: 2243ms
memory: 4192kb

input:

65621

output:

9

result:

ok single line: '9'

Test #16:

score: 0
Accepted
time: 1034ms
memory: 4108kb

input:

44521

output:

8

result:

ok single line: '8'

Test #17:

score: 0
Accepted
time: 503ms
memory: 4052kb

input:

31111

output:

7

result:

ok single line: '7'

Test #18:

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

input:

21111

output:

6

result:

ok single line: '6'

Test #19:

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

input:

20158

output:

17

result:

ok single line: '17'

Test #20:

score: 0
Accepted
time: 65ms
memory: 4020kb

input:

11111

output:

5

result:

ok single line: '5'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3932kb

input:

12

output:

3

result:

ok single line: '3'

Test #22:

score: 0
Accepted
time: 48ms
memory: 4016kb

input:

9609

output:

16

result:

ok single line: '16'

Test #23:

score: 0
Accepted
time: 18ms
memory: 3748kb

input:

5738

output:

15

result:

ok single line: '15'

Test #24:

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

input:

2087

output:

14

result:

ok single line: '14'

Test #25:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

1111

output:

4

result:

ok single line: '4'

Test #26:

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

input:

718

output:

13

result:

ok single line: '13'

Test #27:

score: 0
Accepted
time: 0ms
memory: 3936kb

input:

389

output:

12

result:

ok single line: '12'

Test #28:

score: 0
Accepted
time: 0ms
memory: 3932kb

input:

203

output:

11

result:

ok single line: '11'

Test #29:

score: 0
Accepted
time: 0ms
memory: 3948kb

input:

111

output:

3

result:

ok single line: '3'

Test #30:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

58

output:

10

result:

ok single line: '10'

Test #31:

score: 0
Accepted
time: 0ms
memory: 3976kb

input:

57

output:

9

result:

ok single line: '9'

Test #32:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

20

output:

8

result:

ok single line: '8'

Test #33:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

11

output:

2

result:

ok single line: '2'

Test #34:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

10

output:

7

result:

ok single line: '7'

Test #35:

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

input:

7

output:

6

result:

ok single line: '6'

Test #36:

score: 0
Accepted
time: 0ms
memory: 3852kb

input:

5

output:

5

result:

ok single line: '5'

Test #37:

score: 0
Accepted
time: 0ms
memory: 3932kb

input:

4

output:

4

result:

ok single line: '4'

Test #38:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

3

output:

3

result:

ok single line: '3'

Test #39:

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

input:

1

output:

1

result:

ok single line: '1'