結果
| 問題 | No.6 使いものにならないハッシュ | 
| コンテスト | |
| ユーザー |  lam6er | 
| 提出日時 | 2025-03-20 20:21:41 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 89 ms / 5,000 ms | 
| コード長 | 1,760 bytes | 
| コンパイル時間 | 472 ms | 
| コンパイル使用メモリ | 82,348 KB | 
| 実行使用メモリ | 82,204 KB | 
| 最終ジャッジ日時 | 2025-03-20 20:23:56 | 
| 合計ジャッジ時間 | 3,571 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
from collections import defaultdict
def sieve(n):
    if n < 2:
        return []
    sieve_list = [True] * (n + 1)
    sieve_list[0], sieve_list[1] = False, False
    for i in range(2, int(n ** 0.5) + 1):
        if sieve_list[i]:
            sieve_list[i*i : n+1 : i] = [False] * len(sieve_list[i*i : n+1 : i])
    primes = [i for i, is_prime in enumerate(sieve_list) if is_prime]
    return primes
def main():
    K = int(input())
    N = int(input())
    
    primes_all = sieve(N)
    primes_in_range = [p for p in primes_all if K <= p <= N]
    
    if not primes_in_range:
        print()  # Should not happen as per problem statement
        return
    
    hash_list = [9 if p % 9 == 0 else p % 9 for p in primes_in_range]
    
    max_length = 0
    start_indices = []
    left = 0
    count = defaultdict(int)
    
    for right in range(len(hash_list)):
        current_hash = hash_list[right]
        
        # Move left pointer to ensure all hashes in window are unique
        while count[current_hash] > 0:
            left_hash = hash_list[left]
            count[left_hash] -= 1
            if count[left_hash] == 0:
                del count[left_hash]
            left += 1
        
        count[current_hash] += 1
        
        current_len = right - left + 1
        if current_len > max_length:
            max_length = current_len
            start_indices = [left]
        elif current_len == max_length:
            start_indices.append(left)
    
    # Determine the maximum prime among the start indices
    max_prime = -1
    for idx in start_indices:
        current_p = primes_in_range[idx]
        if current_p > max_prime:
            max_prime = current_p
    
    print(max_prime)
if __name__ == "__main__":
    main()
            
            
            
        