結果

問題 No.6 使いものにならないハッシュ
ユーザー myantamyanta
提出日時 2017-04-30 08:32:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 857 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 40,584 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 23:04:45
合計ジャッジ時間 1,745 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 15 ms
4,352 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 4 ms
4,352 KB
testcase_06 AC 8 ms
4,348 KB
testcase_07 AC 5 ms
4,352 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 9 ms
4,352 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,352 KB
testcase_15 AC 7 ms
4,352 KB
testcase_16 AC 6 ms
4,352 KB
testcase_17 AC 11 ms
4,352 KB
testcase_18 AC 13 ms
4,348 KB
testcase_19 AC 11 ms
4,372 KB
testcase_20 AC 9 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 10 ms
4,352 KB
testcase_23 AC 9 ms
4,348 KB
testcase_24 AC 10 ms
4,348 KB
testcase_25 AC 6 ms
4,352 KB
testcase_26 AC 12 ms
4,352 KB
testcase_27 AC 9 ms
4,352 KB
testcase_28 AC 5 ms
4,348 KB
testcase_29 AC 12 ms
4,352 KB
testcase_30 AC 10 ms
4,348 KB
testcase_31 AC 10 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<vector>


using namespace std;


int is_prime(int x)
{
	int i;

	if(x<2) return 0;
	if(x%2==0) return (x==2);

	for(i=3;i*i<=x;i+=2)
	{
		if(x%i==0) return 0;
	}
	return 1;
}


int fhash(int x)
{
	int r;

	for(;x>9;)
	{
		r=0;
		for(;x;)
		{
			r+=x%10;
			x/=10;
		}
		x=r;
	}
	return x;
}


int solve(int k, int n)
{
	vector<int> p, h;
	int i, s, e, l, l_max=0, s_max=0;
	int c[10]={0};

	for(i=k;i<=n;i++)
	{
		if(is_prime(i))
		{
			p.push_back(i);
			h.push_back(fhash(i));
		}
	}

	for(s=e=0;e<h.size();e++)
	{
		c[h[e]]++;
		for(;c[h[e]]>1;s++)
		{
			c[h[s]]--;
		}
		l=e-s+1;
		if(l_max<=l)
		{
//			printf("update l=%d p[s]=%d p[e]=%d\n", l, p[s], p[e]);
			l_max=l;
			s_max=s;
		}
	}
	return p[s_max];
}


int main(void)
{
	int k, n;

	while(scanf("%d%d", &k, &n)==2)
	{
		printf("%d\n", solve(k, n));
	}

	return 0;
}
0