提出 #55825147


ソースコード 拡げる

// https://codeforces.com/blog/entry/125435
#ifdef MAXWELL_LOCAL_DEBUG
#include "debug/debug_template.cpp"
#else
#define debug(...)
#define debugArr(arr, n)
#endif

#define dbg debug()

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using vi = vector<int>;

using tii = tuple<int,int,int>;
// auto [a,b,c] = ...
// .insert({a,b,c})

const int oo = (int)1e9 + 5; //INF to INT
const ll OO = 0x3f3f3f3f3f3f3f3fLL; //INF to LL
   
ll rev(ll x) {
    string s = to_string(x);
    reverse(s.begin(), s.end());
    ll ans = 0;
    for(auto &p : s) {
        ans = 10 * ans + (p - '0');
    }
    return ans;
}

bool zero(ll x) {
    while(x > 0) {
        if(x % 10 == 0) {
            return true;
        }
        x /= 10;
    }
    return false;
}

int palin(ll x) {
    return x == rev(x);
}


// when using pairs
struct custom_hash {
    inline size_t operator ()(const pair<ll, ll> & a) const {
        return (a.first << 6) ^ (a.first >> 2) ^ 2038074743 ^ a.second;
    }
};

vector<ll> factors;

//map<int, map<ll, bool>> tb;

unordered_map<pair<ll, ll>, bool, custom_hash> tb;
//unordered_map<ull, bool> tb;

bool dp(int i, ll val) {

    if(i == -2 && val == 1) {
        return true;
    }

    if(i < 0) {
        return false;
    }

    if(tb.find(make_pair(i, val)) != tb.end()) {
        return tb[make_pair(i, val)];
    }

    //debug(i, val);

    auto& ans = tb[make_pair(i, val)];

    for(auto &d : factors) {
        if(d > val) break;
        if(val % d || zero(d)) continue;
        auto r = rev(d);
        if((val / d) % r) continue;

        auto sz = (int)to_string(d).size();

        ans |= dp(i - sz - 1, val / d / r);
    }

    return ans;

}

void solve(ll N, int INIT) { 
    
    bool odd = false;
    bool ans = false;

    ll start = -1;

    for(auto &d : factors) {
        if(!zero(d)) {
            auto sz = (int)to_string(d).size();
            if(palin(d) && dp(INIT - sz - 1, N / d)) {
                ans = true;
                odd = true;
                start = d;
                debug("mid", d);
                break;
            } 
            auto r = rev(d);
            if((N / d) % r == 0 && dp(INIT - sz - 1, N / d / r)) {
                ans = true;
                start = d;
                debug("mid", d, r);
                break;
            }
        }
    }

    if(!ans) {
        return;
    }

    ll cur = INIT - (int)to_string(start).size() - 1;
    
    vector<ll> p;

    if(odd) {
        N /= start;
    } else {
        N /= start;
        N /= rev(start);
    }

    //p.push_back(start);

    debug(p, N);

    while(cur != -2 || N != 1) {
        debug(cur, N, p);
        for(auto &d : factors) {
            int sz = (int)to_string(d).size();
            if(zero(d) || N % d) continue;

            ll r = rev(d);

            if((N / d) % r == 0 && dp(cur - sz - 1, N / d / r)) {
                debug("pega", d);
                cur = cur - sz - 1;
                N /= d; N /= r;
                p.push_back(d);
                //exit(0);
                break;
            }
        }
    }

    debug(odd);
    debug(p);

    vector<ll> r;

    for(auto &x : p) {
        r.push_back(rev(x));
    }

    reverse(r.begin(), r.end());

    vector<ll> mid = { start };

    if(!odd) {
        mid.push_back(rev(*mid.begin()));
    }

    debug(p, mid, r);

    vector<ll> ans_final;

    for(auto &x : p) ans_final.push_back(x);
    for(auto &x : mid) ans_final.push_back(x);
    for(auto &x : r) ans_final.push_back(x);
    
    for(int i = 0; i < (int)ans_final.size(); i++) {
        if(i) cout << "*";
        cout << ans_final[i];
    }

    cout << "\n";

    exit(0);

}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    ll N;
    cin >> N;

    for(ll i = 1; 1LL * i * i <= N; i++) {
        if(N % i != 0) continue;
        factors.push_back(i);
        if(N / i != i) {
            factors.push_back(N / i);
        }
    }

    sort(factors.begin(), factors.end());

    tb.reserve(1e5);

    debug(factors);

    solve(N, 80);
    solve(N, 80 + 1);

    cout << "-1\n";

}

提出情報

提出日時
問題 F - Palindromic Expression
ユーザ Maxwell01
言語 C++ 20 (gcc 12.2)
得点 500
コード長 4445 Byte
結果 AC
実行時間 1776 ms
メモリ 10796 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 62
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_1_00.txt, 01_random_1_01.txt, 01_random_1_02.txt, 01_random_1_03.txt, 01_random_1_04.txt, 02_random_2_00.txt, 02_random_2_01.txt, 02_random_2_02.txt, 02_random_2_03.txt, 02_random_2_04.txt, 03_random_3_00.txt, 03_random_3_01.txt, 03_random_3_02.txt, 03_random_3_03.txt, 03_random_3_04.txt, 04_random_4_00.txt, 04_random_4_01.txt, 04_random_4_02.txt, 04_random_4_03.txt, 04_random_4_04.txt, 05_random_5_00.txt, 05_random_5_01.txt, 05_random_5_02.txt, 05_random_5_03.txt, 05_random_5_04.txt, 06_hcn_1_00.txt, 06_hcn_1_01.txt, 06_hcn_1_02.txt, 06_hcn_1_03.txt, 06_hcn_1_04.txt, 06_hcn_1_05.txt, 06_hcn_1_06.txt, 06_hcn_1_07.txt, 06_hcn_1_08.txt, 06_hcn_1_09.txt, 07_hcn_2_00.txt, 07_hcn_2_01.txt, 07_hcn_2_02.txt, 07_hcn_2_03.txt, 07_hcn_2_04.txt, 08_hcn_3_00.txt, 08_hcn_3_01.txt, 08_hcn_3_02.txt, 08_hcn_3_03.txt, 08_hcn_3_04.txt, 08_hcn_3_05.txt, 09_hcn_4_00.txt, 09_hcn_4_01.txt, 09_hcn_4_02.txt, 09_hcn_4_03.txt, 09_hcn_4_04.txt, 10_power_of_2_00.txt, 10_power_of_2_01.txt, 11_corner_00.txt, 11_corner_01.txt, 11_corner_02.txt, 11_corner_03.txt, 11_corner_04.txt, 11_corner_05.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3836 KiB
00_sample_01.txt AC 1 ms 3836 KiB
00_sample_02.txt AC 6 ms 4184 KiB
01_random_1_00.txt AC 2 ms 3860 KiB
01_random_1_01.txt AC 3 ms 3956 KiB
01_random_1_02.txt AC 2 ms 3896 KiB
01_random_1_03.txt AC 3 ms 3832 KiB
01_random_1_04.txt AC 4 ms 4028 KiB
02_random_2_00.txt AC 14 ms 4160 KiB
02_random_2_01.txt AC 3 ms 3780 KiB
02_random_2_02.txt AC 5 ms 4076 KiB
02_random_2_03.txt AC 4 ms 3820 KiB
02_random_2_04.txt AC 5 ms 3864 KiB
03_random_3_00.txt AC 47 ms 4796 KiB
03_random_3_01.txt AC 12 ms 4060 KiB
03_random_3_02.txt AC 4 ms 3816 KiB
03_random_3_03.txt AC 3 ms 3952 KiB
03_random_3_04.txt AC 8 ms 4048 KiB
04_random_4_00.txt AC 19 ms 4088 KiB
04_random_4_01.txt AC 4 ms 3812 KiB
04_random_4_02.txt AC 20 ms 4160 KiB
04_random_4_03.txt AC 6 ms 4116 KiB
04_random_4_04.txt AC 18 ms 4180 KiB
05_random_5_00.txt AC 2 ms 3864 KiB
05_random_5_01.txt AC 3 ms 3856 KiB
05_random_5_02.txt AC 2 ms 3844 KiB
05_random_5_03.txt AC 2 ms 3868 KiB
05_random_5_04.txt AC 2 ms 3816 KiB
06_hcn_1_00.txt AC 540 ms 6828 KiB
06_hcn_1_01.txt AC 200 ms 4580 KiB
06_hcn_1_02.txt AC 648 ms 7016 KiB
06_hcn_1_03.txt AC 438 ms 5716 KiB
06_hcn_1_04.txt AC 345 ms 5136 KiB
06_hcn_1_05.txt AC 617 ms 6044 KiB
06_hcn_1_06.txt AC 617 ms 6328 KiB
06_hcn_1_07.txt AC 471 ms 5516 KiB
06_hcn_1_08.txt AC 864 ms 6932 KiB
06_hcn_1_09.txt AC 738 ms 6016 KiB
07_hcn_2_00.txt AC 84 ms 4604 KiB
07_hcn_2_01.txt AC 106 ms 5084 KiB
07_hcn_2_02.txt AC 83 ms 4800 KiB
07_hcn_2_03.txt AC 117 ms 4912 KiB
07_hcn_2_04.txt AC 117 ms 4748 KiB
08_hcn_3_00.txt AC 1043 ms 7772 KiB
08_hcn_3_01.txt AC 1018 ms 7648 KiB
08_hcn_3_02.txt AC 1045 ms 7804 KiB
08_hcn_3_03.txt AC 1776 ms 10796 KiB
08_hcn_3_04.txt AC 1307 ms 8308 KiB
08_hcn_3_05.txt AC 1336 ms 8760 KiB
09_hcn_4_00.txt AC 895 ms 7896 KiB
09_hcn_4_01.txt AC 1005 ms 8068 KiB
09_hcn_4_02.txt AC 401 ms 5180 KiB
09_hcn_4_03.txt AC 763 ms 6736 KiB
09_hcn_4_04.txt AC 589 ms 5872 KiB
10_power_of_2_00.txt AC 3 ms 3932 KiB
10_power_of_2_01.txt AC 4 ms 4192 KiB
11_corner_00.txt AC 1 ms 4032 KiB
11_corner_01.txt AC 1 ms 3856 KiB
11_corner_02.txt AC 1 ms 3952 KiB
11_corner_03.txt AC 1 ms 3820 KiB
11_corner_04.txt AC 1 ms 3936 KiB
11_corner_05.txt AC 5 ms 4156 KiB