結果

問題 No.2287 ++ -- *=a /=a
ユーザー RubikunRubikun
提出日時 2023-04-28 23:11:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,572 bytes
コンパイル時間 3,214 ms
コンパイル使用メモリ 237,536 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-29 00:25:46
合計ジャッジ時間 11,178 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 42 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005;
const ll INF=1LL<<60;

map<vector<ll>,ll> MA;
ll A;

ll solve(vector<ll> SS,vector<ll> T){
    
    priority_queue<pair<ll,vector<ll>>,vector<pair<ll,vector<ll>>>,greater<pair<ll,vector<ll>>>> PQ;
    PQ.push(mp(0LL,SS));
    MA[SS]=0LL;
    
    ll res=INF;
    
    while(!PQ.empty()){
        auto [d,S]=PQ.top();PQ.pop();
        if(d>=res) break;
        if(MA[S]<d) continue;
        
        if(S==T){
            chmin(res,d);
            continue;
        }
        if(si(S)==1&&S[0]==0){
            chmin(res,d+si(T));
            continue;
        }
        if(si(S)==0){
            chmin(res,d+si(T));
            continue;
        }
        int au=0;
        for(int i=0;i<min(si(S),si(T));i++){
            if(S[i]!=T[i]) break;
            au++;
        }
        if(si(S)<si(T)){
            if(au==si(S)){
                chmin(res,d+si(T)-si(S));
                continue;
            }
        }
        
        ll ss=0;
        for(ll a:S) ss+=a;
        
        ll x=S.back();
        
        if(si(S)<=si(T)&&au==si(S)-1){
            if(x<T[au]){
                S.back()=T[au];
                if(!MA.count(S)||chmin(MA[S],d+T[au]-x)){
                    MA[S]=d+T[au]-x;
                    PQ.push(mp(MA[S],S));
                }
                //chmin(res,T[au]-x+solve(S,T));
                S.back()=x;
            }else{
                ll now=0;
                for(ll a:S){
                    now*=A;
                    now+=a;
                }
                now+=(A+T[au]-x);
                vector<ll> nx;
                while(now){
                    nx.push_back(now%A);
                    now/=A;
                }
                reverse(all(nx));
                if(si(S)==si(nx)){
                    if(!MA.count(nx)||chmin(MA[nx],d+A+T[au]-x)){
                        MA[nx]=d+A+T[au]-x;
                        PQ.push(mp(MA[nx],nx));
                    }
                }
                    //chmin(res,A+T[au]-x+solve(nx,T));
            }
            
            if(x>T[au]){
                S.back()=T[au];
                if(!MA.count(S)||chmin(MA[S],d+x-T[au])){
                    MA[S]=d+x-T[au];
                    PQ.push(mp(MA[S],S));
                }
                //chmin(res,x-T[au]+solve(S,T));
                S.back()=x;
            }else if(si(S)>=2){
                ll now=0;
                for(ll a:S){
                    now*=A;
                    now+=a;
                }
                now-=(x+A-T[au]);
                vector<ll> nx;
                while(now){
                    nx.push_back(now%A);
                    now/=A;
                }
                reverse(all(nx));
                if(si(S)==si(nx)){
                    if(!MA.count(nx)||chmin(MA[nx],d+A+x-T[au])){
                        MA[nx]=d+A+x-T[au];
                        PQ.push(mp(MA[nx],nx));
                    }
                    
                    //chmin(res,(x+A-T[au])+solve(nx,T));
                }
            }
        }
        
        if(ss==1){
            ll now=0;
            for(ll a:S){
                now*=A;
                now+=a;
            }
            now--;
            vector<ll> nx;
            while(now){
                nx.push_back(now%A);
                now/=A;
            }
            reverse(all(nx));
            if(!MA.count(nx)||chmin(MA[nx],d+1)){
                MA[nx]=d+1;
                PQ.push(mp(MA[nx],nx));
            }
            //chmin(res,1+solve(nx,T));
        }
        
        if(S.back()==0){
            S.pop_back();
            if(!MA.count(S)||chmin(MA[S],d+1)){
                MA[S]=d+1;
                PQ.push(mp(MA[S],S));
            }
            //chmin(res,1+solve(S,T));
            S.push_back(x);
        }else{
            {
                S.back()=0;
                if(!MA.count(S)||chmin(MA[S],d+x)){
                    MA[S]=d+x;
                    PQ.push(mp(MA[S],S));
                }
                //chmin(res,x+solve(S,T));
                S.back()=x;
            }
            {
                ll now=0;
                for(ll a:S){
                    now*=A;
                    now+=a;
                }
                now+=(A-x);
                vector<ll> nx;
                while(now){
                    nx.push_back(now%A);
                    now/=A;
                }
                reverse(all(nx));
                if(!MA.count(nx)||chmin(MA[nx],d+A-x)){
                    MA[nx]=d+A-x;
                    PQ.push(mp(MA[nx],nx));
                }
                //chmin(res,A-x+solve(nx,T));
            }
        }
    }
    
    return res;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        ll s,t;cin>>s>>t>>A;
        MA.clear();
        
        vector<ll> S,T;
        while(s){
            S.push_back(s%A);
            s/=A;
        }
        while(t){
            T.push_back(t%A);
            t/=A;
        }
        reverse(all(S));
        reverse(all(T));
        
        cout<<solve(T,S)<<"\n";
    }
}
0