Submission #3009666


Source Code Expand

Copy
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from pprint import pprint
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):
    if S == N:
        return N + 1

    def f(b, n):
        if n < b:
            return n
        return f(b, n//b) + n % b

    for b in range(2, int(math.sqrt(N)) + 1):
        s = f(b, N)
        if s == S:
            return b

    for p in range(1, int(math.sqrt(N))):
        b = (N - S + p) // p
        s = f(b, N)
        if s == S:
            return b

    return -1


def main():
    N = read_int()
    S = read_int()

    print(slv(N, S))


if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task D - 桁和 / Digit Sum
User patahene
Language PyPy3 (2.4.0)
Score 0
Code Size 1626 Byte
Status

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask1_87654_30.txt, subtask1_87654_138.txt, subtask1_87654_45678.txt, subtask1_31415926535_1.txt, subtask1_1_31415926535.txt
All 0 / 500 subtask1_100000000000_1.txt, subtask1_100000000000_100000000000.txt, subtask1_100000000000_2.txt, subtask1_100000000000_3.txt, subtask1_100000000000_50000000000.txt, subtask1_100000000000_50000000001.txt, subtask1_100000000000_99999999999.txt, subtask1_16983563041_1.txt, subtask1_1_1.txt, subtask1_1_2.txt, subtask1_1_31415926535.txt, subtask1_239484768_194586924.txt, subtask1_2_1.txt, subtask1_2_2.txt, subtask1_31415926535_1.txt, subtask1_49234683534_2461734011.txt, subtask1_4_1.txt, subtask1_58640129658_232122496.txt, subtask1_68719476735_35.txt, subtask1_68719476735_36.txt, subtask1_68719476735_37.txt, subtask1_68719476736_1.txt, subtask1_68719476736_2.txt, subtask1_72850192441_16865701.txt, subtask1_79285169301_27.txt, subtask1_82914867733_1676425945.txt, subtask1_8594813796_75700.txt, subtask1_87654_12345.txt, subtask1_87654_138.txt, subtask1_87654_30.txt, subtask1_87654_4294967308.txt, subtask1_87654_45678.txt, subtask1_97822032312_49157112.txt, subtask1_98750604051_977728851.txt, subtask1_99999515529_1.txt, subtask1_99999515529_316226.txt, subtask1_99999515529_316227.txt, subtask1_99999515529_316228.txt, subtask1_99999515529_49999757765.txt, subtask1_99999515529_49999757766.txt, subtask1_99999515530_2.txt, subtask1_99999999977_1.txt, subtask1_99999999977_2.txt, subtask1_99999999977_49999999989.txt, subtask1_99999999977_49999999990.txt, subtask1_99999999999_1.txt, subtask1_99999999999_100000000000.txt
Case Name Status Exec Time Memory
subtask1_100000000000_1.txt 268 ms 63980 KB
subtask1_100000000000_100000000000.txt 267 ms 63724 KB
subtask1_100000000000_2.txt 314 ms 66796 KB
subtask1_100000000000_3.txt 305 ms 66796 KB
subtask1_100000000000_50000000000.txt 309 ms 66796 KB
subtask1_100000000000_50000000001.txt 341 ms 68588 KB
subtask1_100000000000_99999999999.txt
subtask1_16983563041_1.txt 270 ms 63724 KB
subtask1_1_1.txt 267 ms 63724 KB
subtask1_1_2.txt 272 ms 63724 KB
subtask1_1_31415926535.txt 269 ms 63724 KB
subtask1_239484768_194586924.txt 295 ms 66924 KB
subtask1_2_1.txt 275 ms 63724 KB
subtask1_2_2.txt 271 ms 63724 KB
subtask1_31415926535_1.txt 300 ms 66284 KB
subtask1_49234683534_2461734011.txt 305 ms 66540 KB
subtask1_4_1.txt 268 ms 63724 KB
subtask1_58640129658_232122496.txt 311 ms 66796 KB
subtask1_68719476735_35.txt 299 ms 66668 KB
subtask1_68719476735_36.txt 268 ms 63724 KB
subtask1_68719476735_37.txt 308 ms 66668 KB
subtask1_68719476736_1.txt 266 ms 63724 KB
subtask1_68719476736_2.txt 269 ms 63724 KB
subtask1_72850192441_16865701.txt 305 ms 66412 KB
subtask1_79285169301_27.txt 270 ms 63724 KB
subtask1_82914867733_1676425945.txt 306 ms 66540 KB
subtask1_8594813796_75700.txt 289 ms 66028 KB
subtask1_87654_12345.txt 268 ms 63724 KB
subtask1_87654_138.txt 266 ms 63724 KB
subtask1_87654_30.txt 267 ms 63724 KB
subtask1_87654_4294967308.txt
subtask1_87654_45678.txt 269 ms 63852 KB
subtask1_97822032312_49157112.txt 309 ms 66540 KB
subtask1_98750604051_977728851.txt 315 ms 66708 KB
subtask1_99999515529_1.txt 311 ms 66924 KB
subtask1_99999515529_316226.txt 318 ms 67052 KB
subtask1_99999515529_316227.txt 314 ms 66924 KB
subtask1_99999515529_316228.txt 318 ms 67052 KB
subtask1_99999515529_49999757765.txt 313 ms 66924 KB
subtask1_99999515529_49999757766.txt 346 ms 68588 KB
subtask1_99999515530_2.txt 314 ms 67052 KB
subtask1_99999999977_1.txt 319 ms 67052 KB
subtask1_99999999977_2.txt 316 ms 66924 KB
subtask1_99999999977_49999999989.txt 320 ms 66924 KB
subtask1_99999999977_49999999990.txt 343 ms 68588 KB
subtask1_99999999999_1.txt 314 ms 66924 KB
subtask1_99999999999_100000000000.txt