QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#130964 | #6300. Best Carry Player 2 | q | WA | 0ms | 3432kb | C++23 | 1.4kb | 2023-07-25 21:41:29 | 2023-07-25 21:41:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace IO{
template<typename T> void read(T &x){ x=0; bool f=0; char ch=getchar(); for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+(ch^48); if(f) x=-x; }
template<typename T> void write(T x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); }
}
const int U=39,C=20,K=2;
using i128=__int128;
int t;
int k,s[U];
i128 x,f[U][C][K],p[U];//f[i][j][0/1]表示当前考虑到第i位,进位了j次,当前位是否进位的最小答案
void init(){
p[0]=1;
for(int i=1;i<U;i++) p[i]=p[i-1]*(i128)10;
}
void work(){
IO::read(x),IO::read(k);
int n=0;
for(int tmp=x;tmp;s[++n]=tmp%10,tmp/=10);
n+=k;
memset(f,0x3f,sizeof f);
i128 INF=f[0][0][0];
for(int i=1;i<=n;i++) f[i][0][0]=0;
for(int i=1;i<=n;i++){
for(int j=0;j<=i;j++){
f[i][j][0]=min(f[i][j][0],f[i-1][j][0]);
if(s[i]>0) f[i][j][1]=min(f[i][j][1],f[i-1][j-1][0]+p[i]*(10-s[i]));
if(s[i]>0&&j) f[i][j][1]=min(f[i][j][1],f[i-1][j-1][1]+p[i]*(10-s[i]-1));
if(s[i]<9&&j) f[i][j][0]=min(f[i][j][0],f[i-1][j][1]);
}
}
i128 ans=min(f[n][k][0],f[n][k][1]);
IO::write(ans==INF?-1:ans),puts("");
}
int main(){
init();
// IO::write(p[38]);
IO::read(t);
for(;t--;) work();
return 0;
}
/*
4
12345678 0
12345678 5
12345678 18
990099 5
1
54322
999999999987654322
9910
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3432kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
0 6543300 -1 870100100
result:
wrong answer 1st lines differ - expected: '1', found: '0'