ABC193 C - Unexpressed

備忘録

問題

atcoder.jp

回答

import sys
import os
import math
import bisect
import itertools
import collections
import heapq
import queue
import array
import time
import datetime

# 時々使う
# import numpy as np
# from decimal import Decimal, ROUND_HALF_UP
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

# 再帰の制限設定
sys.setrecursionlimit(10000000)


def ii(): return int(sys.stdin.buffer.readline().rstrip())
def il(): return list(map(int, sys.stdin.buffer.readline().split()))
def it(): return tuple(map(int, sys.stdin.buffer.readline().split()))
def fl(): return list(map(float, sys.stdin.buffer.readline().split()))
def iln(n): return [int(sys.stdin.buffer.readline().rstrip())
                    for _ in range(n)]


def iss(): return sys.stdin.buffer.readline().decode().rstrip()
def sl(): return list(map(str, sys.stdin.buffer.readline().decode().split()))
def isn(n): return [sys.stdin.buffer.readline().decode().rstrip()
                    for _ in range(n)]


def lcm(x, y): return (x * y) // math.gcd(x, y)


# MOD = 10 ** 9 + 7
MOD = 998244353
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()

    a = 2
    ret = set()
    while a < N:
        if a > 10 ** 5 + 1:
            break
        b = 2
        while a ** b <= N:
            ret.add(a ** b)
            b += 1
        a += 1
    print(N-len(ret))


if __name__ == '__main__':
    main()

考え方

aの取りうる範囲について考えます。
まず、最低値は問題の条件により2です。
次に最大値は10 ** 5となります。

Nは最大10 ** 10です。
仮にa = 10 ** 5, b = 2のとき、Nの最大値と同じ値となります。
つまり、a10 ** 5を上回るときの値は全て条件を満たすことが出来ません。

よって、2 <= a <= 10 ** 5の範囲でa ** b <= Nとなる値を全て求めることによって回答できます。