Submission #72910944


Source Code Expand

import sys
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lii = lambda: list(mii())
MOD = 10**9+7
inf = 1<<64
from math import gcd
import math
def phi(n):
    res = n
    i = 2
    while i*i<=n:
        if n % i == 0:
            res= (res // i) * (i-1)
            while n % i == 0:
                n //= i
        i += 1
    
    if n > 1:
        res = (res // n) * (n-1)
    
    return res

class Solution:
    def smallestRepunitDivByK(self, k: int) -> int:
        return self.smallestRepunitDivByK_general(k, 1)
    def smallestRepunitDivByK_general(self, k: int, repunit: int) -> int:
        # 计算出repunit有多少位
        bit_length = len(str(repunit))
        
        # 首先处理i=0的特殊情况(因为不能对0取模)
        if repunit % k == 0:
            return 1 * bit_length   # 最小的n就是repunit的位数

        # 同余式a * x^i ≡ a (mod k)成立,那么我们有:
        # d = gcd(a, k)
        # d|k, d|a => a` * x^i ≡ a` (mod k`)
        # 其中 a` = a / d, k` = k / d
        # 而同余式 a` * x^i ≡ a` (mod k`) 有解的充分必要条件是:
        # gcd(x, k`) = 1

        '''
        启发自lr580的题解 https://leetcode.cn/problems/smallest-integer-divisible-by-k/solutions/2265460/ju-yi-fan-san-kuo-zhan-dao-geng-da-de-k-i55fc/

        证明:如果a与m互质,那么同余式 a * x^i ≡ a (mod m), i=1, 2, 3... 有解的充分必要条件是 gcd(x, m) = 1
        证明过程如下:
        必要性:
            由欧拉定理可知,若 gcd(x, m) = 1, 则有:
                x^i ≡ 1 (mod m), 其中 i = φ(m)
            两边同乘以 a 得到:
                a * x^i ≡ a (mod m)
            必要性得证。
        充分性:
            已知a与m互质,则在模m意义下,a的逆元a^(-1)存在。
            将同余式两边同乘以a^(-1),得到:
                x^i ≡ 1 (mod m)
            我们有:
                x * x^(i-1) ≡ 1 (mod m)
            这表明 x^(i-1) 是 x 的逆元。
            逆元存在的充分必要条件是 gcd(x, m) = 1。
            因此,充分性得证。
        '''

        # 这里我们要解决的是同余式:repunit` * 10^bit_length ≡ repunit` (mod k`)
        # 其中 repunit` = repunit / g
        # k_hat = k * (10^bit_length - 1)
        # k` = k_hat / g
        # g = gcd(repunit, k_hat)

        # 利用必要性,验证同余式是否有解
        b = pow(10, bit_length)     # e.g. 10/1000
        p = (b-1) * k               # e.g. 9k/999k

        g = math.gcd(repunit, p)
        repunit //= g
        p //= g
        if math.gcd(b, p) != 1:
            return -1

        # 已知 同余式 repunit` * 10^i ≡ repunit` (mod k`) 有解,最大解为 φ(k`)
        # 枚举所有 φ(k`) 的因子,找到最小的解

        # 计算φ(k`)
        m = phi(p)

        i = 1
        while i * i <= m:
            if m % i == 0 and pow(b, i, p) % p == 1:
                return i * bit_length
            i += 1
        
        i -= 1
        while i > 0:
            if m % i == 0 and pow(b, m//i, p) % p == 1:
                return (m // i) * bit_length
            i -= 1

        return -1       # 理论上不会到达这里
def solve():
    n = ii()
    f = Solution()
    if n<10:
        print(n)
        return
    if n%10==0:
        print(-1)
        return
    ans = []
    for i in range(1,10):
        ans.append(f.smallestRepunitDivByK_general(n,i))
    def check(x):
        return str(x)==''.join(sorted(str(x)))
    mi = -1
    for i in range(1,10000):
        if check(i*n):
            mi = str(i*n)
            break
    for i,d in enumerate(ans):
        if d==-1:
            continue
        if mi==-1:
            mi = str(i+1)*d
        if d<len(mi):
            mi = str(i+1)*d
            continue
        if len(mi)==d:
            if mi>str(i+1)*d:
                mi = str(i+1)*d
    print(mi)
    
    
                    
t = 1
# t = ii()
for _ in range(t):
    solve()

Submission Info

Submission Time
Task F - Non-Increasing Number
User xiaoe
Language Python (PyPy 3.11-v7.3.20)
Score 0
Code Size 4210 Byte
Status WA
Exec Time 85 ms
Memory 116912 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 525
Status
AC × 4
AC × 20
WA × 47
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_max_00.txt, 01_max_01.txt, 01_max_02.txt, 01_max_03.txt, 01_max_04.txt, 01_max_05.txt, 01_max_06.txt, 01_max_07.txt, 01_max_08.txt, 01_max_09.txt, 01_max_10.txt, 01_max_11.txt, 01_max_12.txt, 01_max_13.txt, 01_max_14.txt, 01_max_15.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt, 03_handmade_05.txt, 03_handmade_06.txt, 03_handmade_07.txt, 03_handmade_08.txt, 03_handmade_09.txt, 03_handmade_10.txt, 03_handmade_11.txt, 03_handmade_12.txt, 03_handmade_13.txt, 03_handmade_14.txt, 03_handmade_15.txt, 03_handmade_16.txt, 03_handmade_17.txt, 03_handmade_18.txt, 03_handmade_19.txt, 03_handmade_20.txt, 03_handmade_21.txt, 03_handmade_22.txt, 03_handmade_23.txt, 03_handmade_24.txt, 03_handmade_25.txt, 03_handmade_26.txt, 03_handmade_27.txt, 03_handmade_28.txt, 03_handmade_29.txt, 03_handmade_30.txt, 03_handmade_31.txt, 03_handmade_32.txt, 03_handmade_33.txt, 03_handmade_34.txt, 03_handmade_35.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 58 ms 80376 KiB
00_sample_01.txt AC 57 ms 80320 KiB
00_sample_02.txt AC 57 ms 79912 KiB
00_sample_03.txt AC 70 ms 95908 KiB
01_max_00.txt WA 75 ms 99652 KiB
01_max_01.txt WA 79 ms 105052 KiB
01_max_02.txt WA 73 ms 98676 KiB
01_max_03.txt WA 72 ms 98060 KiB
01_max_04.txt WA 71 ms 97676 KiB
01_max_05.txt WA 71 ms 97456 KiB
01_max_06.txt AC 57 ms 79928 KiB
01_max_07.txt AC 61 ms 86208 KiB
01_max_08.txt WA 73 ms 98840 KiB
01_max_09.txt AC 72 ms 97856 KiB
01_max_10.txt WA 73 ms 98808 KiB
01_max_11.txt WA 73 ms 98836 KiB
01_max_12.txt WA 74 ms 99164 KiB
01_max_13.txt WA 78 ms 102672 KiB
01_max_14.txt WA 72 ms 97940 KiB
01_max_15.txt WA 73 ms 98696 KiB
02_random_00.txt WA 77 ms 104992 KiB
02_random_01.txt WA 85 ms 116912 KiB
02_random_02.txt WA 77 ms 101908 KiB
02_random_03.txt WA 76 ms 100184 KiB
02_random_04.txt WA 79 ms 104120 KiB
02_random_05.txt WA 77 ms 104432 KiB
02_random_06.txt AC 57 ms 80072 KiB
02_random_07.txt WA 73 ms 98284 KiB
02_random_08.txt WA 77 ms 102356 KiB
02_random_09.txt WA 75 ms 100224 KiB
02_random_10.txt WA 76 ms 101252 KiB
03_handmade_00.txt AC 57 ms 79900 KiB
03_handmade_01.txt AC 58 ms 79900 KiB
03_handmade_02.txt AC 58 ms 80192 KiB
03_handmade_03.txt AC 57 ms 79892 KiB
03_handmade_04.txt AC 58 ms 79872 KiB
03_handmade_05.txt AC 71 ms 97408 KiB
03_handmade_06.txt AC 71 ms 97048 KiB
03_handmade_07.txt WA 72 ms 98088 KiB
03_handmade_08.txt WA 74 ms 98748 KiB
03_handmade_09.txt WA 75 ms 99768 KiB
03_handmade_10.txt WA 79 ms 109372 KiB
03_handmade_11.txt AC 71 ms 97380 KiB
03_handmade_12.txt AC 69 ms 95832 KiB
03_handmade_13.txt WA 74 ms 99184 KiB
03_handmade_14.txt WA 74 ms 98596 KiB
03_handmade_15.txt AC 73 ms 99004 KiB
03_handmade_16.txt AC 58 ms 80092 KiB
03_handmade_17.txt WA 72 ms 97732 KiB
03_handmade_18.txt WA 72 ms 97220 KiB
03_handmade_19.txt WA 71 ms 97476 KiB
03_handmade_20.txt WA 71 ms 97476 KiB
03_handmade_21.txt WA 71 ms 97828 KiB
03_handmade_22.txt WA 74 ms 98520 KiB
03_handmade_23.txt WA 74 ms 98932 KiB
03_handmade_24.txt WA 73 ms 98496 KiB
03_handmade_25.txt WA 74 ms 98340 KiB
03_handmade_26.txt WA 74 ms 98188 KiB
03_handmade_27.txt WA 75 ms 100176 KiB
03_handmade_28.txt AC 75 ms 99796 KiB
03_handmade_29.txt WA 74 ms 98688 KiB
03_handmade_30.txt WA 74 ms 99000 KiB
03_handmade_31.txt WA 74 ms 99660 KiB
03_handmade_32.txt WA 76 ms 100952 KiB
03_handmade_33.txt WA 76 ms 99924 KiB
03_handmade_34.txt WA 76 ms 100332 KiB
03_handmade_35.txt WA 72 ms 97804 KiB