結果
問題 |
No.719 Coprime
|
ユーザー |
![]() |
提出日時 | 2025-03-31 17:59:10 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,917 bytes |
コンパイル時間 | 313 ms |
コンパイル使用メモリ | 82,568 KB |
実行使用メモリ | 69,864 KB |
最終ジャッジ日時 | 2025-03-31 18:00:00 |
合計ジャッジ時間 | 4,551 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 WA * 34 |
ソースコード
def main(): import sys N = int(sys.stdin.readline().strip()) if N < 2: print(0) return # Step 1: Generate smallest prime factors (SPF) using sieve spf = [0] * (N + 1) spf[0], spf[1] = 0, 1 # 1 is not a prime for i in range(2, N + 1): if spf[i] == 0: spf[i] = i for j in range(i*i, N+1, i): if spf[j] == 0: spf[j] = i else: continue # Function to get distinct prime factors of x def get_primes(x): primes = set() while x != 1: p = spf[x] primes.add(p) while x % p == 0: x //= p return primes sum_primes = 0 primes_set = set() candidates = [] for x in range(2, N + 1): if spf[x] == x: # x is a prime sum_primes += x primes_set.add(x) else: # Get distinct primes of x primes = get_primes(x) sum_p = sum(primes) gain = x - sum_p if gain > 0: candidates.append(( -gain, x, primes )) # Negative gain for min-heap sort, but we'll sort in reverse # Sort candidates in descending order of gain (hence reverse sorted of the first element) candidates.sort() # Extract primes and original gain sorted_candidates = [ ( -gc, x, ps ) for (gc, x, ps) in candidates ] # Now iterate and select composites used = set() total_gain = 0 for gain, x, primes in sorted_candidates: # Check if any prime in primes is already used conflict = False for p in primes: if p in used: conflict = True break if not conflict: total_gain += gain for p in primes: used.add(p) print(sum_primes + total_gain) if __name__ == '__main__': main()