QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#259993#6300. Best Carry Player 2ariesTL 1ms3856kbC++203.4kb2023-11-21 17:32:572023-11-21 17:32:58

Judging History

你现在查看的是最新测评结果

  • [2023-11-21 17:32:58]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3856kb
  • [2023-11-21 17:32:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef __int128 ll;
#define endl '\n'


//__int128的read和write  好像不能和sync共同使用
inline __int128 read(){
    __int128 num=0; char g=getchar(); while(g<48||57<g) g=getchar();
    while(47<g&&g<58) num=(num<<1)+(num<<3)+g-48,g=getchar(); return num;
}
inline void write(__int128 u){
    if(u>9) write(u/10); putchar(u%10+'0');
}


const ll NO=3e18;

ll dp[25][25][2];
ll mi[20]={1};

ll fun1(ll a,ll b ){
    return (a/mi[b])%10;
}

ll fun2(ll a,ll b){
    return a*mi[b];
}

ll fun3(ll a,ll b){
    int cnt=0;
    while(a||b){
        if(a%10!=b%10)cnt++;
        a/=10;
        b/=10;
    }
    return cnt-1;
}


void solve(){
    for(int i=0;i<=21;i++){
        for(int j=0;j<=21;j++){
            dp[i][j][1]=dp[i][j][0]=NO;
        }
    }
    ll x;
    ll k;

    x=read();k=read();
    if(k==0){
        ll y=1;
        while(x%10==9){
            y*=10;
            x/=10;
        }
        write(y);
        putchar('\n');
        return;

    }


    int len=0;
    while(x%10==0){
        len++;
        x/=10;
    }

    for(int i=0;i<=20;i++){
        dp[i][0][0]=x;
    }
    for(int i=0;i<=18;i++){
        for(int j=0;j<=18;j++){
            
            if(i!=0)dp[i][j][0]=min(dp[i][j][0],dp[i-1][j][0]);
            if(i!=0)dp[i][j][0]=min(dp[i][j][0],dp[i-1][j][1]);
            //if(1)cout<<"i="<<i<<" j="<<j<<" dp="<<dp[i][j][0]-x<<" "<<dp[i][j][1]-x<<endl;
            if(dp[i][j][1]!=NO){
                int tem=fun1(dp[i][j][1],i);
                if(tem!=0){
                    tem=10-tem;
                    ll b=dp[i][j][1]+fun2(tem,i);
                    int ch=fun3(dp[i][j][1],b);
                    //cout<<"a="<<dp[i][j]<<" b="<<b<<" ch="<<ch<<endl;
                    dp[i+1][j+ch][1]=min(dp[i+1][j+ch][1],b);
                    
                    //cout<<"dp"<<i+1<<" "<<j+ch<<" ="<<min(dp[i+1][j+ch][1],b)<<endl;
                }
                dp[i+1][j][0]=min(dp[i+1][j][0],dp[i][j][1]);
                
            }
            if(dp[i][j][0]!=NO){
                int tem=fun1(dp[i][j][0],i);
                if(tem!=0){
                    tem=10-tem;
                    ll b=dp[i][j][0]+fun2(tem,i);
                    int ch=fun3(dp[i][j][0],b);
                    //cout<<"a="<<dp[i][j]<<" b="<<b<<" ch="<<ch<<endl;
                    dp[i+1][j+ch][1]=min(dp[i+1][j+ch][1],b);
                    
                    //cout<<"dp"<<i+1<<" "<<j+ch<<" ="<<min(dp[i+1][j+ch][1],b)<<endl;
                }
                dp[i+1][j][0]=min(dp[i+1][j][0],dp[i][j][0]);
                
            }
            
        }
    }
    ll miny=NO;
    for(int i=0;i<19;i++){
        
        miny=min(miny,dp[i][k][0]);
    }
    //cout<<miny<<endl;
    for(int i=0;i<19;i++){
        miny=min(miny,dp[i][k][1]);
        //if(dp[i][k][1]<=0)cout<<i<<endl;
    }
        
   write(miny-x);
    
    while(len--){
        cout<<0;
    }
    putchar('\n');
    //cout<<min(dp[18][k]-x,dp[17][k]-x)<<endl;
}


int main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    int tt=1;
    cin>>tt;
    for(int i=1;i<=19;i++){
        mi[i]=mi[i-1]*10;
    }
    while(tt--){
        solve();
    }
    

    return 0;
}

/*
4
12345678 0
12345678 5
12345678 18
990099 5
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3856kb

input:

4
12345678 0
12345678 5
12345678 18
990099 5

output:

1
54322
999999999987654322
9910

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 3800kb

input:

21
999990000099999 0
999990000099999 1
999990000099999 2
999990000099999 3
999990000099999 4
999990000099999 5
999990000099999 6
999990000099999 7
999990000099999 8
999990000099999 9
999990000099999 10
999990000099999 11
999990000099999 12
999990000099999 13
999990000099999 14
999990000099999 15
999...

output:

100000
10000
1000
100
10
1
900001
9900001
99900001
999900001
10000000001
9999910000
9999901000
9999900100
9999900010
9999900001
9000009999900001
99000009999900001
999000009999900001
99999999999999999900000000000000000
1000000000000000000

result:

ok 21 lines

Test #3:

score: -100
Time Limit Exceeded

input:

100000
119111011091190000 10
1911011191011999 16
110099199000119 0
19009911191091011 13
199090909919000900 17
19009010011919110 5
90910190019900091 18
10911100000101111 1
110090011101119990 4
100909999119090000 12
90901119109011100 2
111010119991090101 4
900991019109199009 5
100919919990991119 8
911...

output:

88988908810000
8088988808988001
10
88808908989
9800909090080999100
80890
909089809980099909
9
80010
9090000880910000
8900
9909
991
9008900
8880880090
8080090801
8009900808909899
80880898981
909
8800909
99988889901
89908888089
980908890980099000
100
9889801
81
908890008099900891
880990801
9998099
890...

result: