結果

問題 No.6 使いものにならないハッシュ
ユーザー T1610T1610
提出日時 2018-01-13 01:02:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 3,160 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 170,908 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2023-10-14 23:10:03
合計ジャッジ時間 3,399 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
7,560 KB
testcase_01 AC 12 ms
7,316 KB
testcase_02 AC 16 ms
7,928 KB
testcase_03 AC 12 ms
7,368 KB
testcase_04 AC 12 ms
7,300 KB
testcase_05 AC 12 ms
7,584 KB
testcase_06 AC 14 ms
7,820 KB
testcase_07 AC 13 ms
7,580 KB
testcase_08 AC 14 ms
7,536 KB
testcase_09 AC 11 ms
7,448 KB
testcase_10 AC 12 ms
7,488 KB
testcase_11 AC 13 ms
7,420 KB
testcase_12 AC 15 ms
7,808 KB
testcase_13 AC 12 ms
7,320 KB
testcase_14 AC 12 ms
7,580 KB
testcase_15 AC 14 ms
7,672 KB
testcase_16 AC 12 ms
7,516 KB
testcase_17 AC 14 ms
7,756 KB
testcase_18 AC 15 ms
7,892 KB
testcase_19 AC 15 ms
7,740 KB
testcase_20 AC 14 ms
7,596 KB
testcase_21 AC 12 ms
7,320 KB
testcase_22 AC 13 ms
7,600 KB
testcase_23 AC 14 ms
7,668 KB
testcase_24 AC 14 ms
7,824 KB
testcase_25 AC 13 ms
7,368 KB
testcase_26 AC 14 ms
7,844 KB
testcase_27 AC 13 ms
7,972 KB
testcase_28 AC 13 ms
7,444 KB
testcase_29 AC 16 ms
7,824 KB
testcase_30 AC 15 ms
7,752 KB
testcase_31 AC 14 ms
7,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

const int MAX_N = 1e6+10;
vector<int> primes;
vector<int> isPrime(MAX_N, 1);
void buildPrimes() {
    isPrime[0] = isPrime[1] = 0;
    REP(i, 2, MAX_N) {
        if(isPrime[i]) {
            primes.push_back(i);
            for(int j = i+i; j < MAX_N; j += i) isPrime[j] = false;
        }
    }
}

int f(int n) {
    if(n < 10) return n;
    int s = 0;
    while(n > 0) {
        s += n%10;
        n/= 10;
    }
    return f(s);
}

template <typename T>
struct SegTree{
    vector<T> dat;
    int n;
    const T Default;
    SegTree(int n_, const T& def) : Default(def){init(n_);}
    void init(int n_){
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n-1, Default);
    }
    T f(const T& a, const T& b) {
        return min(a, b);
    }
    void update(int k, const T& a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1) / 2;
            dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b, int k, int l, int r ){
        if(r <= a || b <= l) return Default;
        if(a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) ;
#define LINE     ;
#define dumpV(v);
#define STOP     ;
#endif
// #define mp make_pair

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


int main(){
    buildPrimes();
    int l, r;
    cin >> l >> r;
    ++r;
    vi v;
    REP(i, l, r) if(isPrime[i]) v.pb(i);
    if(v.size() == 1) {
        cout << v[0] << endl;
        return 0;
    }
    int n = v.size();
    vector<int> idx(10, v.size());
    SegTree<ll> st(v.size()+10, ll(1e18));
    st.update(n, n);
    ll x = -1, y = -1;
    repr(i, v.size()) {
        int k = f(v[i]);
        dump(v[i]);
        dump(k);
        ll ma = min((ll)idx[k], st.query(i, idx[k]));
        ll len = ma - i;
        if(len > x) {
            x = len;
            y = i;
        }
        st.update(i, idx[k]);
        dump(len);
        dump(y);
        idx[k] = i;
    }
    dump(x);
    ll ans = (y == -1 ? v[0] : v[y]);
    cout << ans << endl;

    return 0;
}
0