結果

問題 No.6 使いものにならないハッシュ
ユーザー codershifthcodershifth
提出日時 2016-08-11 02:00:38
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,851 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 93,444 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-02 21:22:47
合計ジャッジ時間 2,539 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.stdio;
import std.string;
import std.array;
import std.typecons;
import std.typetuple;
import std.container;
import std.algorithm;
import std.conv;
import std.math;
import std.format;

alias TypeTuple tie;

void readlnToken(T...)(auto ref T args)
{
    import std.stdio;
    import std.conv;
    import std.string;
    import std.array;

    auto line = split(readln().strip);
    foreach(ref arg; args)
    {
        arg = to!(typeof(arg))(line[0]);
        line = line[1..$];
    }
    assert(line.empty()); // got all token??
}

auto sieve(int n)
{
    auto is_prime = new bool[](n+1);
    is_prime[] = true;
    is_prime[0] = is_prime[1] = false;

    for(int i = 2; i <= n; ++i)
    {
        if (is_prime[i])
        {
            for(int j = 2*i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
    int[] ret;
    foreach(i;0..n+1)
    {
        if (is_prime[i])
            ret ~= i;
    }
    return ret;
}

int hash(int x)
{
    if ( x < 10 )
        return x;
    int y = 0;
    while (x)
    {
        y += x%10;
        x /= 10;
    }
    return hash(y);
}
void solve()
{
    int K,N;
    readlnToken(K);
    readlnToken(N);

    auto tmp = sieve(N);

    int[] primes;
    foreach(p;tmp)
    {
        if (p < K)
            continue;
        primes ~= p;
    }
    auto key = primes.map!(hash);
    int ret = 0;
    int ans = 0;
    foreach(i; 0..key.length)
    {
        int size = 1;
        bool[int] exist;
        exist[key[i]] = true;
        foreach(j; i+1..key.length)
        {
            if ( key[j] in exist )
                break;
            exist[key[j]] = true;
            size++;
        }
        if ( ret < size || (ret == size && ans < primes[i]) )
        {
            ret = size;
            ans = primes[i];
        }
    }
    writeln(ans);
}

void main()
{
       solve();
}
0