Number Theory

시간 제한7초메모리 제한1024 MB

요약
n을 1, 11, 111, ... 꼴의 수들의 정수 계수 결합으로 나타낼 때 가중 합 i*|x_i|의 최솟값을 구해 출력한다.
난이도

어려움10점 중 8점

유형
수학, 정수론, 그리디, 동적 계획법
정답자
아직 제출이 없습니다

문제

Let o_i=1…1⏟_i timeso\_i = \underbrace{1 \dots 1}\_{i\text{ times}} be the number which consists of ii ones in its decimal representation.

Bobo has an integer nn. Find a sequence of possibly negative integers (x_1,x_2,…,)(x\_1, x\_2, \dots, ) where

  • ∑_i=1∞o_i⋅x_i=n\sum\_{i = 1}^{\infty} o\_i \cdot x\_i = n,
  • ∑_i=1∞i⋅∣x_i∣\sum\_{i = 1}^{\infty} i \cdot |x\_i| is minimized.

입력

The input consists of several test cases terminated by end-of-file. For each test case,

The first line contains an integer nn.

출력

For each test case, output an integer which denotes the minimum value of ∑_i=1∞i⋅∣x_i∣\sum\_{i = 1}^\infty i \cdot |x\_i|.

제한

  • 1≤n<1050001 \leq n < 10^{5000}
  • In each input, the sum of the number of decimal digits of nn does not exceed 5000050000.

힌트

For the first test case, x_1=x_2=1x\_1 = x\_2 = 1, x_3=x_4=⋯=0x\_3 = x\_4 = \dots = 0. The minimum value is 1×1+2×1=31 \times 1 + 2 \times 1 = 3.

For the second test case, x_1=0x\_1 = 0, x_2=−1x\_2 = -1, x_3=1x\_3 = 1, x_4=x_5=⋯=0x\_4 = x\_5 = \dots = 0. The minimum value is 2×1+3×1=52 \times 1 + 3 \times 1 = 5.

예제1

  1. 예제 1

    입력
    12
    100
    998244353
    
    예상 출력
    3
    5
    76