LoginSignup
0
1

More than 3 years have passed since last update.

はじめに

二分探索の問題を解きます。

Buy an Integer

売られている全ての整数を試すとTLEになります。ですので、うまく計算量を落さないといけません、そこで二分探索を使います。二分探索は雑に説明すると、両端から範囲を狭めていって目的の解を探す手法です。詳しい解説
二分探索の計算量は$O(log N)$なので間に合います。

a,b,x = map(int,input().split())

max_n = 10**9+1 #nの最大値が10**9なので+1
min_n = 0
while max_n - min_n > 1:
    mid_n = (max_n+min_n)//2
    if x < a*(mid_n)+b*len(str(mid_n)):
        max_n = mid_n
    else:
        min_n = mid_n

print(min_n)

まとめ

二分探索をうまく使えるようになれば、大きく計算量を落せるのでマスターしたい。ではまた。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1