結果

問題 No.600 かい文回
ユーザー xuzijian629xuzijian629
提出日時 2018-09-14 15:12:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 4,526 bytes
コンパイル時間 3,439 ms
コンパイル使用メモリ 133,924 KB
実行使用メモリ 5,740 KB
最終ジャッジ日時 2023-09-19 03:53:36
合計ジャッジ時間 3,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
5,460 KB
testcase_02 AC 4 ms
5,436 KB
testcase_03 AC 3 ms
5,472 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
5,536 KB
testcase_06 AC 4 ms
5,512 KB
testcase_07 AC 6 ms
5,416 KB
testcase_08 AC 6 ms
5,524 KB
testcase_09 AC 5 ms
5,468 KB
testcase_10 AC 8 ms
5,468 KB
testcase_11 AC 8 ms
5,520 KB
testcase_12 AC 7 ms
5,524 KB
testcase_13 AC 7 ms
5,452 KB
testcase_14 AC 8 ms
5,600 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 7 ms
5,740 KB
testcase_17 AC 14 ms
5,544 KB
testcase_18 AC 3 ms
5,696 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 14 ms
5,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <chrono>
#include <random>
#include <bitset>
#include <iterator>
#include <functional>
#include <utility>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "STACK:36777216")
using namespace std;
using i64 = int64_t;
using i128 = __int128_t;
constexpr i64 MOD = 1e9 + 7;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
using vi = vector<i64>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ii = pair<i64, i64>;

i64 modpow(i128 a, i128 n, i128 mod) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        i128 t = modpow(a, n / 2, mod);
        return t * t % mod;
    }
    return a * modpow(a, n - 1, mod) % mod;
}

i128 modinv(i128 n, i128 mod) {
    return modpow(n, mod - 2, mod);
}

bool is_prime(i128 n, int k) {
    if (n == 2) return true;
    if (n < 2 || n % 2 == 0) return false;
    i128 d = n - 1;
    while (d % 2 == 0) {
        d /= 2;
    }
    for (int i = 0; i < k; i++) {
        i128 a = rnd() % (n - 2) + 1;
        i128 t = d;
        i128 y = modpow(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = modpow(y, 2, n);
            t *= 2;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}

i64 gen_prime(int bit) {
    assert(bit > 32);
    while (1) {
        i128 d = (i128(rnd()) << (bit - 32)) + rnd();
        d |= 1;
        if (is_prime(d, 50)) {
            return d;
        }
    }
}

class RollingHash {
    i128 hash[202020];
    i128 pows[202020];
    i128 p, m;
public:
    RollingHash(string s, i64 p = gen_prime(40), i64 m = gen_prime(50)) {
        assert(s.size() < 202020);
        hash[0] = 1;
        pows[0] = 1;
        for (int i = 0; i < s.size(); i++) {
            hash[i + 1] = (hash[i] * p + s[i]) % m;
            pows[i + 1] = pows[i] * p % m;
        }
        this->p = p;
        this->m = m;
    }
    
    i128 encode(int l, int r) {
        // inclusive
        return ((hash[r + 1] - hash[l] * pows[r - l + 1]) % m + m) % m;
    }
    
    bool eq(int l0, int r0, int l1, int r1) {
        return encode(l0, r0) == encode(l1, r1);
    }
};

i64 cnt(string s) {
    int n = s.size();
    RollingHash hash(s);
    if (n % 2) {
        vi dp(n / 2 + 1, 1);
        for (int i = 1; i <= n / 2; i++) {
            int l = n / 2 - i;
            int r = n / 2 + i;
            for (int d = 0; d < i; d++) {
                if (hash.eq(l, l + d, r - d, r)) {
                    assert(i - 1 - d >= 0);
                    dp[i] += dp[i - 1 - d];
                    dp[i] %= MOD;
                }
            }
        }
        return dp[n / 2];
    } else {
        vi dp(n / 2 + 1, 1);
        for (int i = 1; i <= n / 2; i++) {
            int l = n / 2 - i;
            int r = n / 2 - 1 + i;
            assert(r > l);
            for (int d = 0; d < i; d++) {
                if (hash.eq(l, l + d, r - d, r)) {
                    assert(i - 1 - d >= 0);
                    dp[i] += dp[i - 1 - d];
                    dp[i] %= MOD;
                }
            }
        }
        return dp[n / 2];
    }
}

string to_str(deque<char> d) {
    string ret = "";
    for (auto c: d) {
        ret += c;
    }
    return ret;
}

i64 cnt(deque<char> d) {
    return cnt(to_str(d));
}

char rot(char a, int i) {
    return (a - 'a' + i) % 26 + 'a';
}

int main() {
    int n;
    cin >> n;
    deque<char> ans = {'a'};
    string b = "";
    while (n) {
        b += '0' + (n & 1);
        n >>= 1;
    }
    reverse(b.begin(), b.end());
    i64 c = 1;
    for (int i = 0; i < b.size(); i++) {
        if (i != 0 && b[i] == '1') {
            for (int j = 1; j < 26; j++) {
                ans.push_front(rot(ans.front(), j));
                ans.push_back(rot(ans.back(), j));
                if (cnt(ans) == c + 1) {
                    c += 1;
                    break;
                } else {
                    ans.pop_front();
                    ans.pop_back();
                }
            }
        }
        if (i != b.size() - 1) {
            ans.push_front(ans.front());
            ans.push_back(ans.back());
            c *= 2;
        }
    }
    for (int i = 0; i < ans.size() - 1; i++) {
        cout << ans[i];
    }
    cout << ans.back() << endl;
}
0