結果
問題 | No.6 使いものにならないハッシュ |
ユーザー |
![]() |
提出日時 | 2017-04-30 08:32:33 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 16 ms / 5,000 ms |
コード長 | 857 bytes |
コンパイル時間 | 406 ms |
コンパイル使用メモリ | 38,528 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-16 16:38:29 |
合計ジャッジ時間 | 1,511 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
#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; }