結果

問題 No.6 使いものにならないハッシュ
ユーザー motumotumotumotu
提出日時 2014-11-13 19:37:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 2,557 bytes
コンパイル時間 1,263 ms
コンパイル使用メモリ 82,956 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:47:10
合計ジャッジ時間 2,186 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0