AtCoder Beginner Contest 174 C Problem (Python)

The first way to think

Make a number in which 7s are lined up obediently, and divide it by K to determine whether it becomes 0. Since K is less than $ 10 ^ 6 $, arrange 7 up to that range. Naturally TLE if you think that 7 is lined up for $ 10 ^ 6 $

K = int(input())
flg = False
for i in range(10 ** 6):
    n = int('7' * (i + 1))
    if n % K == 0:
        flg = True
        break
    
if flg:
    print(len(str(n)))
else:
    print(-1)

Created by reading the solution and official commentary of the god of competition professionals (@tanakh)

It seems that you should implement something that is long-division. Ordinary long division is to add 0 too much and continue the calculation until it is divisible (until you feel like it), but instead add 7. For example, $ 10 \ div 8 $ would normally look like this:


10 \div 8 = 1 \,not really\, 2 \\
20 \div 8 = 2 \,not really\, 4 \\
40 \div 8 = 5 \,not really\, 0 \\
answer: \,1.25

This is done as follows.


10 \div 8 = 1 \,not really\, 2 \\
(20 + 7) \div 8 = 3 \,not really\, 3 \\
(30 + 7) \div 8 = 4 \,not really\, 5 \\
...

When I implement it in Python, it looks like this. (When K = 101 in the numerical example) It's fast because each calculation is too much. ~~ If it is $ 10 ^ 6 $ times, it is acceptable. ~~ (Addition: From the comment, it seems that K times is enough because there can be only K streets below K. Thank you!)

K = 101
cur = 7
for i in range(100):
    mod = cur % K
    print(f'{cur} % {K} = {mod}')
    if mod == 0:
        ans = i + 1
        print(f'Answer: {ans}')
        break
    else:
        cur = mod * 10 + 7

"""
(output)
7 % 101 = 7
77 % 101 = 77
777 % 101 = 70
707 % 101 = 0
Answer: 4
"""

Finally AC below

K = int(input())

cur = 7
flg = False
for i in range(10 ** 6):
    mod = cur % K
    if mod == 0:
        ans = i + 1
        flg = True
        break
    else:
        cur = mod * 10 + 7

if flg:
    print(ans)
else:
    print(-1)

Recommended Posts

AtCoder Beginner Contest 174 C Problem (Python)
AtCoder Beginner Contest # 002 C Problem
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 167 A Problem "Registration" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
AtCoder Regular Contest # 002 C Problem
Atcoder Beginner Contest 152 Kiroku (python)
AtCoder Beginner Contest 170 A Problem "Five Variables" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 169 A Explanation of Problem "Multiplication 1" (Python3, C ++, Java)
AtCoder Beginner Contest 176 A Explanation of problem "Takoyaki" (Python3, C ++, Java)
AtCoder Beginner Contest 175 B Problem "Making Triangle" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 176 B Problem "Multiple of 9" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 A Problem "Air Conditioner" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177
AtCoder Beginner Contest 177 A Problem "Don't be late" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 179
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 173
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 177 C Problem "Sum of product of pairs" Explanation (Python3, C ++, Java)
Atcoder Beginner Contest 153
AtCoder Beginner Contest 165 A Problem "We Love Golf" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 167 B Problem "Easy Linear Programming" Explanation (Python3, C ++, Java)
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
AtCoder Beginner Contest: D Question Answers Python
AtCoder Beginner Contest 152 Review
AtCoder Beginner Contest 181 Note
AtCoder Beginner Contest 187 Note
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 180 Note
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 182 Note
AtCoder Beginner Contest 169 Review
AtCoder Beginner Contest 181 Review
AtCoder Beginner Contest 171 Review
AtCoder Beginner Contest 182 Review
AtCoder Beginner Contest 180 Review
AtCoder Beginner Contest 156 WriteUp
AtCoder Beginner Contest 177 Review
AtCoder Beginner Contest 168 Review
AtCoder Beginner Contest 179 Review
Solve AtCoder Beginner Contest 100-102
AtCoder Beginner Contest 167 Memorandum
AtCoder Beginner Contest 172 Review
AtCoder Beginner Contest 183 Note
AtCoder Beginner Contest 176 Review
AtCoder Beginner Contest 184 Note
AtCoder Beginner Contest 174 Review
AtCoder Beginner Contest 153 Review
AtCoder Beginner Contest 161 Review
AtCoder Beginner Contest 170 Review
AtCoder Beginner Contest 165 Review
AtCoder Beginner Contest 173 Review
AtCoder Beginner Contest 188 Note