結果

問題 No.6 使いものにならないハッシュ
ユーザー kou6839
提出日時 2014-11-09 01:26:34
言語 Java
(openjdk 23)
結果
AC  
実行時間 194 ms / 5,000 ms
コード長 1,406 bytes
コンパイル時間 4,166 ms
コンパイル使用メモリ 79,248 KB
実行使用メモリ 56,656 KB
最終ジャッジ日時 2024-09-16 16:22:10
合計ジャッジ時間 9,366 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
 

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int K=sc.nextInt();
		int N = sc.nextInt();
		boolean[] nera = new boolean[N+1];
		Arrays.fill(nera, true);
		for(int i=2;i*i<=N;i++){
			if(nera[i]){
				for(int j=i*2;j<=N;j+=i){
					if(nera[j])nera[j]=false;
				}
			}
		}
		ArrayList<Integer> sosuu = new ArrayList<>();
		ArrayList<Integer> ha = new ArrayList<>();
		for(int i=K;i<=N;i++){
			if(i==1) continue;
			if(nera[i]){
				int temp=i;
				sosuu.add(i);
				while(temp>9){
					int temp2=temp;
					temp=0;
					while(temp2!=0){
						temp+=temp2%10;
						temp2=temp2/10;
					}
				}
				ha.add(temp);
			}
		}
		int t=0;
		int s=0;
		Queue<Integer> queue = new LinkedList<>();
		int ans=0;
		int ansmemo=0;
		int temp=0;
		int max=0;
		while(true){
			while(t<ha.size()){				
				if(queue.contains(ha.get(t))){
					if(t-s>=ans){
						ans=t-s;
						max=sosuu.get(s);
					}
					break;
				}
				if(t==ha.size()-1){
					int temp1=ha.get(t);
					queue.offer(temp1);
					if(t-s+1>=ans){
						max=sosuu.get(s);
					}
					System.out.println(max);
					return;
					
				}
				queue.offer(ha.get(t++));
			}
			while(true){
				int temp1=queue.poll();
				s++;
				if(temp1==ha.get(t)){
					break;
				}
				
			}
			
			
		}
		
	}
}	
0