結果
| 問題 | No.6 使いものにならないハッシュ | 
| コンテスト | |
| ユーザー |  motumotu | 
| 提出日時 | 2014-11-13 19:37:17 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 11 ms / 5,000 ms | 
| コード長 | 2,557 bytes | 
| コンパイル時間 | 977 ms | 
| コンパイル使用メモリ | 86,872 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-09-16 16:22:20 | 
| 合計ジャッジ時間 | 1,989 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <functional>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)
#define CLEAR(d) memset((d), 0, (sizeof((d))))
#define ALL(c) (c).begin(), (c).end()
#define ABS(x) ((x < 0) ? -(x) : (x))
#define SORT(x) sort((x).begin(), (x).end())
#define RSORT(x) sort((x).begin(), (x).end(), greater<int>() )
#define SIZE(a) ((int)((a).size()))
#define MAX3(a, b, c) (max(max((a), (b)), (c)))
#define MIN3(a, b, c) (min(min((a), (b)), (c)))
#define MOD 1000000007
#define EPS 1e-5
#define PI  (acos(-1))
#define INF 10000000
//===================================================
template<class T> inline std::string toString(T x) { std::ostringstream oss; oss << x; return oss.str(); }
typedef pair<int, int> PA;
vector<int> sieveOfEratosthenes(int n)
{
    vector<int> arr(n+1);
    for (int i = 0; i <= n; i++) arr[i] = 1;
    arr[0] = arr[1] = 0;
    for (int i = 2; i*i <= n; i++)
    if (arr[i]) for (int j = i * i; j <= n; j += i)
        arr[j] = 0;
    return arr;
}
int hashDigi(int num)
{
    int hash = num;
    while (hash >= 10) {
        string s = toString(hash);
        hash = 0;
        REP(i, s.length()) hash += (int)(s[i] - '0');
    }
    return hash;
}
int main()
{
    int n, k;
    cin >> k >> n;
    vector<int> arr = sieveOfEratosthenes(n);
    vector<PA> hash;
    for (int i = k; i <= n; i++) {
        if (arr[i]) hash.push_back(PA(hashDigi(i), i));
    }
    int t[10], ans = -1, front = 0, len = 0, ma[20];
    CLEAR(ma);
    CLEAR(t);
    REP(i, hash.size()) {
        if (t[hash[i].first]) {
            for (; hash[front].first != hash[i].first; front++) {
                t[hash[front].first] = 0; len--;
            }
            //t[hash[i].first] = 1; len++;
            front++;
            ma[len] = max(ma[len], hash[front].second);
        }
        else {
            t[hash[i].first] = 1;
            len++;
            ma[len] = max(ma[len], hash[front].second);
        }
    }
    
    for (int i = 15; i >= 1; i--) {
        if (ma[i] != 0) {
            ans = ma[i];
            break;
        }
    }
    
    printf("%d\n", ans);
    return 0;
}
            
            
            
        