結果

問題 No.2426 Select Plus or Minus
ユーザー Manuel1024
提出日時 2023-08-18 23:18:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,234 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 97,396 KB
実行使用メモリ 961,536 KB
最終ジャッジ日時 2024-11-28 10:17:50
合計ジャッジ時間 71,967 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 MLE * 1
other AC * 14 TLE * 14 MLE * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(){
    ll n; cin >> n;

    map<ll, int> mem;
    map<ll, ll> pre;
    queue<ll> Q;
    Q.emplace(n);
    mem[n] = 0;
    while(!Q.empty()){
        ll cur = Q.front(); Q.pop();
        if(cur == 1) break;
        if(cur%2 == 0){
            if(mem.count(cur/2) == 1) continue;
            mem[cur/2] = mem[cur]+1;
            pre[cur/2] = cur;
            if(cur/2 == 1) break;
            Q.emplace(cur/2);
        }else{
            for(auto &it: {cur*3+1, cur*3-1}){
                if(1000000000000000000 < it) continue;
                if(mem.count(it) == 1) continue;
                mem[it] = mem[cur]+1;
                pre[it] = cur;
                if(it == 1) break;
                Q.emplace(it);
            }
        }
    }

    string ans = "";
    ll x = 1;
    while(x != n){
        ll p = pre[x];
        if(x*2 == p) ans += "/";
        else if(p*3+1 == x) ans += "+";
        else ans += "-";
        x = p;
    }
    reverse(ans.begin(), ans.end());
    cout << ans.size() << endl;
    cout << ans << endl;
    return 0;
}
0