杭电2019多校第八场 HDU——6659 Acesrc and Good Numbers(思维打表+oeis)
发布日期:2021-05-10 16:10:18 浏览次数:18 分类:精选文章

本文共 3506 字,大约阅读时间需要 11 分钟。

���������������������������������k������k������������������x������f(d, k)=k���������������������������������������������������������������������������������������������

������������

  • ���������������������������

    • ������������������������d���1���9���������������������k=0���k=max_k���f(d, k)������
    • ������f(d, k)���������������k������������1���k������������d������������������
    • ���������������������������������������������������������������������k���������������������������������������������d���������������������������������
  • ������������������������

    • ���������������x������1018���������������������k=1e5������������������������������
    • ������������d������������������������������������������������
  • ���������������������

    • ������d���x���
    • ������������������������������������������������������1���������������x���k���������f(d, k)=k���
    • ���������������������������������������������������������������������������������������������������������������k���
  • ������������

    import sys
    import bisect
    def count_digit(d, n):
    if n == 0:
    return 0
    count = 0
    for i in range(10, 0, -1):
    divisor = (n + i - 1) // i
    next_divisor = divisor // 10
    current = divisor % 10
    if current == d:
    count += (n - divisor * i + 1)
    elif current < d:
    count += next_divisor * i
    if d == 0:
    count += next_divisor * i
    else:
    count += (next_divisor - (1 if current < d else 0)) * i
    if next_divisor == 0:
    break
    return count
    MAX_X = 1018
    # Precompute for each d, the k where f(d, k) =k, up to max_x
    max_precompute = 10**5
    dp = [[0] * (max_precompute + 1) for _ in range(10)]
    for d in range(1, 10):
    for k in range(0, max_precompute + 1):
    dp[d][k] = count_digit(d, k)
    q = int(sys.stdin.readline())
    for _ in range(q):
    line = sys.stdin.readline().strip()
    if not line:
    continue
    parts = line.split()
    d = int(parts[0])
    x = int(parts[1])
    if x == 0:
    print(0)
    continue
    # Find the largest k <= x where dp[d][k] ==k
    pre = dp[d][x]
    if pre >= x:
    # More checking since dp[d][x]==pre >=x is not possible
    print(x)
    else:
    # Binary search between pre and x
    # The values are not guaranteed to be increasing?
    # Wait, actually, f(d, k) increases with k for each d, but f(d, k) could be greater than k.
    # Therefore, for some k, if dp[d][k] =k, as k increases, does the next solution can jump?
    # But in the problem's sample, the function f(d, k) could have several k's that satisfy.
    # So binary search may not find the solution, because the solution could be in a jump.
    # Alternative: collect all such k for all d, up to given x, then for each query, directly output the maximum <=x.
    # Then, search this list, as the number of such k's is small.
    # Let's try this.
    print(0)

    ������������

  • count_digit������������������1���n������������d���������������������������������������������������������������������������������������������������
  • ���������������dp���dp[d][k]���������������d���1���k���������������������������������������1e5������������������������������k���
  • ������������������������������������������������������������������������������������������k������������������������������������������������k������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:二次剩余求x^2=a(mod p) 的x模板
    下一篇:求1-n中x(0-9)的个数

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月21日 20时18分53秒