結果

問題 No.2426 Select Plus or Minus
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2023-08-18 22:05:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,152 bytes
コンパイル時間 2,943 ms
コンパイル使用メモリ 155,824 KB
実行使用メモリ 320,484 KB
最終ジャッジ日時 2023-08-18 22:05:30
合計ジャッジ時間 8,835 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 882 ms
79,928 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
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 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <ostream>
#include <optional>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <ranges>
#include <map>
#include <unordered_map>
using namespace std;

#ifndef INCLUDED_MAIN
#define INCLUDED_MAIN
#include __FILE__


using P = pair<i64, i64>;

i32 main() {
    i64 n;
    cin >> n;

    unordered_map<i64, i64> memo;
    unordered_map<i64, char> pref;

    queue<P> q;
    q.emplace(n, 0);

    while (!q.empty()) {
        auto [x, d] = q.front(); q.pop();
        if (memo.count(x)) continue;
        memo[x] = d;
        if (d + 1 > 10'000) continue;
        if (x == 1) break;
        if (x % 2 == 0) {
            if (memo.contains(x / 2)) continue;
            q.emplace(x / 2, d + 1);
            pref[x / 2] = '/';
        } else {
            if (3*x-1 <= 1'000'000'000'000'000'000 && !memo.contains(3*x - 1)) {
                q.emplace(3*x - 1, d + 1);
                pref[3*x - 1] = '-';
            }
            if (3*x+1 <= 1'000'000'000'000'000'000 && !memo.contains(3*x + 1)) {
                q.emplace(3*x + 1, d + 1);
                pref[3*x + 1] = '+';
            }
        }
    }

    i64 now = 1;
    string ans;
    while (now != n) {
        ans += pref[now];
        if (pref[now] == '/') {
            now *= 2;
        } else if (pref[now] == '-') {
            now = (now + 1) / 3;
        } else {
            now = (now - 1) / 3;
        }
    }

    ranges::reverse(ans);
    cout << memo[1] << "\n" << ans << endl;
}








/********************************************************************************/
#else

#ifdef ONLINE_JUDGE
#define debug(x) {}
#else
const int DEBUG_PRINT = 10;
const string bold = "\e[1m";
const string cyan = "\e[36m";
const string red = "\e[31m";
const string reset = "\e[0m";
#define debug(x) {cerr << bold << cyan << "[" << red << __LINE__ << cyan << "] "<< #x << ": " << reset; debgp(x); }
template<class T> void debgp(T &x) { cerr << x; }
template<class T, class U> void debgp(pair<T,U> &p) { cerr << "("; debgp(p.first); cerr << ", "; debgp(p.second); cerr << ")"; }
template<class T> void debgp(vector<T> &v) { cerr << "["; for (int i=0; i<min((int)v.size(), DEBUG_PRINT); i++) { debgp(v[i]); cerr << (i+1!=v.size()?" ":"]\n"); } if (v.size() > DEBUG_PRINT) cerr << " … ]\n"; }
template<class T> void debgp(vector<vector<T>> &v) {cerr<<"[\n"; for (int i=0; i<min((int)v.size(), DEBUG_PRINT); i++) { cerr<<" "; debgp(v[i]); } if (v.size() > DEBUG_PRINT) cerr<<" ︙\n"; ;  cerr << "]\n"; }
#endif


using i32 = int;
using i64 = long long;

const i64 INF = 0x0f0f'0f0f'0f0f'0f0f;

template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

vector dx4 = {1, 0, -1, 0};
vector dy4 = {0, 1, 0, -1};
vector dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector dy8 = {0, 1, 1, 1, 0, -1, -1, -1};

template<class T, class U> istream& operator>>(istream&i,pair<T,U>&v){ i>>v.first>>v.second; return i; }
template<class T> istream& operator>>(istream&i,vector<T>&v){ for(T&e : v) i>>e; return i; }
template<class T, class U> ostream& operator<<(ostream &o, pair<T,U> &p){o<<"("<<p.first<<", "<<p.second<<")";return o; }
template<class T> ostream& operator<<(ostream&o,vector<T>&v){for(int i=0; i<v.size(); i++) { o << v[i] << (i+1!=v.size()?" ":"\n");} return o;}

template<class T> optional<i32> position(vector<T>&v, T x) { auto it = std::find(v.begin(), v.end(), x); if (it == v.end()) return nullopt; return it - v.begin(); }
optional<i32> position(string &v, char x) { auto it = std::find(v.begin(), v.end(), x); if (it == v.end()) return nullopt; return it - v.begin(); }
template<class T> vector<pair<T, i32>> run_length_encoding(vector<T>&v) { vector<pair<T, i32>> res; if (v.empty()) return res; res.emplace_back(v[0], 0); for (const auto& x : v) { if (res.back().first == x) { res.back().second++; } else { res.emplace_back(x, 1); } } return res; }


struct init{init(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}init;
#endif
0