結果

問題 No.6 使いものにならないハッシュ
ユーザー Tawara
提出日時 2016-07-26 23:38:38
言語 Python2
(2.7.18)
結果
AC  
実行時間 77 ms / 5,000 ms
コード長 734 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-09-16 16:34:27
合計ジャッジ時間 2,757 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from collections import defaultdict as ddict
def sieve (n):
	check = [1]*(n+1)
	check[0] = check[1] = 0
	for d in xrange(2,int(math.sqrt(n))+1):
		if check[d] == 0: continue
		for comp in xrange(d*2,n+1,d):
			check[comp] = 0
	return check
def convert(n):
	while n > 9:
		tmp = 0
 		while n > 0:
 			tmp += n%10
 			n /= 10
 		n = tmp
 	return n
K = int(raw_input())
N = int(raw_input())
prime = sieve(N)
l = [i for i in xrange(K,N+1) if prime[i]]
ulhash = ddict(int)
ans = l[0]
maxlen = 1
ulhash[convert(l[0])] = lpos = 0
for rpos in xrange(1,len(l)):
	r = convert(l[rpos])
	if ulhash[r] >= lpos:
		lpos = ulhash[r] + 1
	ulhash[r] = rpos
	if maxlen <= rpos - lpos + 1:
		ans = l[lpos]
		maxlen = rpos - lpos + 1
print ans
0