提出 #61474069


ソースコード 拡げる

"""
<方針>
- 明らかに `O(N)` は間に合わない.
- 桁ごとでうまくみていけば,あとは実装力の問題だ.
  - `L~R` という条件が知りたければ,`X` 以下さえ分かる関数 `snake` を求めれば, `snake(R) - snake(L-1)` を求めれば良い.
  - <パターンA> `X` の桁数よりも小さい桁を全部列挙すれば良い.
  - <パターンB> `X` の桁数において,`X` の先頭の数を `X` の実際の先頭の数いかを試せば良い.
  - <パターンC> `X` の桁数において,`X` の先頭も固定した状態で考える.その時,「`X` の先頭の数」進数で考えると良さそう.
"""
# 入力
L, R = map(int, input().split())

def snake(X):
  # L==10の時,L-1==0になるため,その対処
  if X<=9:return 0
  
  # 基本情報
  block = str(X)
  head = int(block[0])
  length = len(block)
  
  # 出力
  ans = 0
  
  ## パターンA
  for i in range(2, length):
    for j in range(1, 10):
      ans += j**(i-1)
  
  ## パターンB
  for i in [length]:
    for j in range(1, head):
      ans += j**(i-1)

  ## パターンC
  tmp = "" # head進数を作る
  fla = True # head<=iを見てないフラグ
  for ind, s in enumerate(block[1:]):
    i = int(s)
    if(head>i and fla):
      tmp += s
    else:
      tmp += str(head-1)
      fla = False
  # head進数の計算
  for i, s in enumerate(reversed(tmp)):
    ans += int(s)*(head**i)
  
  # 「10」のパターンを足す
  ans += 1
  
  # 出力
  return ans

ans = snake(R)-snake(L-1)

print(ans)

提出情報

提出日時
問題 C - Snake Numbers
ユーザ mattsunkun
言語 Python (PyPy 3.10-v7.3.12)
得点 350
コード長 1622 Byte
結果 AC
実行時間 57 ms
メモリ 76612 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 350 / 350
結果
AC × 3
AC × 29
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 03_random3_04.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt, 04_handmade_05.txt, 04_handmade_06.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 56 ms 76536 KiB
00_sample_01.txt AC 56 ms 76536 KiB
00_sample_02.txt AC 55 ms 76528 KiB
01_random_00.txt AC 55 ms 76492 KiB
01_random_01.txt AC 55 ms 76424 KiB
01_random_02.txt AC 55 ms 76440 KiB
01_random_03.txt AC 56 ms 76416 KiB
01_random_04.txt AC 56 ms 76608 KiB
01_random_05.txt AC 55 ms 76612 KiB
01_random_06.txt AC 55 ms 76404 KiB
01_random_07.txt AC 56 ms 76548 KiB
01_random_08.txt AC 55 ms 76536 KiB
01_random_09.txt AC 55 ms 76432 KiB
02_random2_00.txt AC 55 ms 76272 KiB
02_random2_01.txt AC 55 ms 76436 KiB
02_random2_02.txt AC 56 ms 76604 KiB
02_random2_03.txt AC 56 ms 76596 KiB
03_random3_00.txt AC 55 ms 76224 KiB
03_random3_01.txt AC 56 ms 76552 KiB
03_random3_02.txt AC 55 ms 76332 KiB
03_random3_03.txt AC 55 ms 76548 KiB
03_random3_04.txt AC 56 ms 76456 KiB
04_handmade_00.txt AC 55 ms 76260 KiB
04_handmade_01.txt AC 56 ms 76568 KiB
04_handmade_02.txt AC 57 ms 76572 KiB
04_handmade_03.txt AC 56 ms 76560 KiB
04_handmade_04.txt AC 55 ms 76188 KiB
04_handmade_05.txt AC 55 ms 76416 KiB
04_handmade_06.txt AC 56 ms 76412 KiB