QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#257685 | #6300. Best Carry Player 2 | pengpeng_fudan# | TL | 0ms | 3788kb | C++14 | 2.0kb | 2023-11-19 11:53:53 | 2023-11-19 11:53:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using big=long long;
using ll=long long;
int n;
ll a;int k;
big dp[37][20][2];
void get(big& a,big b){
if(a==-1) a=b;
else if(b!=-1){
if(a>b) a=b;
}
}
void pt(big k){
vector<int> ans;
while(k){
ans.push_back(k%10);
k/=10;
}
int sz=ans.size();
for(int i=sz-1;i>=0;i--) cout<<ans[i];
cout<<'\n';
}
void solve(){
cin>>a>>k;
if(k==0){
long long z=1;
for(int i=1;i<=18;i++){
if(a%10!=9){
cout<<z<<'\n';
return ;
}
a/=10;
z=z*10;
}
cout<<z<<'\n';
return ;
}
for(int i=0;i<=18;i++) for(int j=0;j<=k;j++) dp[i][j][0]=dp[i][j][1]=-1;
dp[0][0][0]=0;
big zp=1;
for(int i=1;i<=18;i++){
int t=a%10;
a/=10;
for(int j=0;j<=k;j++){
get(dp[i][j][0],dp[i-1][j][0]);
if(t!=9) get(dp[i][j][0],dp[i-1][j][1]);
if(j!=0&&dp[i-1][j-1][0]!=-1&&t!=0) get(dp[i][j][1],dp[i-1][j-1][0]+zp*(10-t));
if(j!=0&&dp[i-1][j-1][1]!=-1) get(dp[i][j][1],dp[i-1][j-1][1]+zp*(10-t-1));
}
zp=zp*10;
}
//pt(dp[1][1][1]);
int l=k;
while(l>=0&&dp[18][l][0]==-1&&dp[18][l][1]==-1) l--;
if(l==k){
big k;
if(dp[18][l][0]==-1) k=dp[18][l][1];
else if(dp[18][l][1]==-1) k=dp[18][l][0];
else k=min(dp[18][l][0],dp[18][l][1]);
cout<<k<<'\n';
return ;
}
else{
big k=dp[18][l][1];
for(int i=l+1;i<=k;i++) cout<<9;
vector<int> ans;
for(int i=1;i<=18;i++){
ans.push_back(k%10);
k/=10;
}
for(int i=17;i>=0;i--) cout<<ans[i];
cout<<'\n';
return ;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int _=1;
cin>>_;
while(_--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3788kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1 54322 999999999987654322 9910
result:
ok 4 lines
Test #2:
score: -100
Time Limit Exceeded
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 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...