結果

問題 No.1458 Segment Function
ユーザー norikamenorikame
提出日時 2021-03-31 23:24:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 4,106 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 222,256 KB
実行使用メモリ 814,720 KB
最終ジャッジ日時 2024-05-09 10:19:23
合計ジャッジ時間 4,240 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 MLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
#define v(t) vector<t>
#define p(t) pair<t, t>
#define p2(t, s) pair<t, s>
#define vp(t) v(p(t))

#define rep(i, n) for (int i=0,i##_len=((int)(n)); i<i##_len; ++i)
#define rep2(i, a, n) for (int i=((int)(a)),i##_len=((int)(n)); i<=i##_len; ++i)
#define repr(i, n) for (int i=((int)(n)-1); i>=0; --i)
#define rep2r(i, a, n) for (int i=((int)(n)),i##_len=((int)(a)); i>=i##_len; --i)

#define repi(itr, c) for (__typeof((c).begin()) itr=(c).begin(); itr!=(c).end(); ++itr)
#define repir(itr, c) for (__typeof((c).rbegin()) itr=(c).rbegin(); itr!=(c).rend(); ++itr)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define eb emplace_back

#define INF (1e9)
#define LINF (1e18)
#define PI (acos(-1))
#define EPS (1e-7)
#define DEPS (1e-10)

const v(int) sval = { 6, 2, 5, 5, 4, 5, 6, 4, 7, 6 };

string s0;
map<p(int), int> memo;
int f1(int l, int r) {
    if (memo.find({l,r}) != memo.end()) return memo[{l,r}];
    string s1 = s0.substr(l, (r-l));
    if (s1.front() == '-') return (memo[{l,r}] = f1(l+1,r)+1);
    else if (sz(s1) == 1) return (memo[{l,r}] = sval[(int)(s1.front()-'0')]);
    else return (memo[{l,r}] = f1(l,l+1)+f1(l+1,r));
}

int f(string s) {
    if (s.front() == '-') return f(s.substr(1))+1;
    else if (sz(s) == 1) return sval[(int)(s.front()-'0')];
    else return f(s.substr(0,1))+f(s.substr(1));
}

ll mod = (ll)(1e9)+7;
// auto mod int
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

int main(){
    string pi, n;
    cin >> pi >> n;
    if (sz(n)==1 && stoll(n)==0LL) {
        cout << pi << endl;
        return 0;
    }
    s0 = pi;
    ll now = f1(0, sz(pi));
    if (sz(n)==1 && stoll(n)==1LL) {
        cout << now << endl;
        return 0;
    }
    map<int, ll> cnt;
    cnt[now] = 1;
    ll icnt = 2, nval = -1;
    ll res = 0;
    if (sz(n) <= 18) nval = stoll(n);
    while (1) {
        int nxt = f(to_string(now));
        if (cnt.find(nxt) != cnt.end()) {
            ll roop = (icnt - cnt[nxt]);
            if (nval != -1) {
                nval = (nval - icnt) % roop;
            }
            else {
                mod = roop;
                mint nval2 = 0;
                rep(j, sz(n)) {
                    nval2 *= 10;
                    nval2 += (ll)(n[j] - '0');
                }
                nval2 -= icnt;
                nval = nval2.x;
            }
            res = nxt;
            rep(j, nval) res = f(to_string(res));
            break;
        }
        else cnt[nxt] = icnt;
        if (nval!=-1 && nval<=icnt) {
            res = nxt;
            break;
        }
        ++icnt;
        now = nxt;
    }
    cout << res << endl;
    return 0;
}
0