QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#260061 | #6300. Best Carry Player 2 | icreiys | RE | 0ms | 0kb | C++23 | 3.0kb | 2023-11-21 19:30:10 | 2023-11-21 19:30:11 |
answer
#include<bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;
#define endl '\n'
const ll NO=3e18;
//string str;
ll dp[25][25][2];
ll mi[20]={1};
ll inline fun1(ll a,ll b ){
return (a/mi[b])%10;
}
ll inline 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;
cin>>x>>k;
//str="";
if(k==0){
ll y=1;
while(x%10==9){
y*=10;
x/=10;
}
//str+=to_string(y);
//str+='\n';
printf("%lld\n",y);
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<=7)cout<<"dp["<<i<<"]["<<j<<"]="<<(dp[i][j][0]==NO?-1:dp[i][j][0]-x)<<" "<<(dp[i][j][1]==NO?-1:dp[i][j][1]-x)<<endl;
//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(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);
dp[i+ch][j+ch][1]=min(dp[i+ch][j+ch][1],b);
//cout<<" dp["<<i+ch<<"]["<<j+ch<<"][1]="<<min(dp[i+ch][j+ch][1],b)-x<<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);
dp[i+ch][j+ch][1]=min(dp[i+ch][j+ch][1],b);
//cout<<" dp["<<i+ch<<"]["<<j+ch<<"][1]="<<min(dp[i+ch][j+ch][1],b)-x<<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;
}
//cout<<miny<<endl;
//str+=to_string(miny-x);
printf("%lld",miny-x);
while(len--){
//str+='0';
printf("0");
}
printf("\n");
//str+='\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();
//cout<<str;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1