QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134995 | #6300. Best Carry Player 2 | yzhang | 0 | 0ms | 3832kb | C++14 | 2.7kb | 2023-08-05 10:32:21 | 2023-08-05 10:32:29 |
Judging History
answer
//μ's forever
#include <bits/stdc++.h>
#define ll long long
//#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline ll read()
{
register ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
int T,k;
ll n;
int a[40],tot,f[40][40][2];
int mst[40];
int main()
{
T=read();
while(T--){
n=read(),k=read();
tot=0;
memset(a,0,sizeof(a));
while(n){
++tot;
a[tot]=n%10;
n/=10;
}
memset(f,0,sizeof(f));
f[0][0][0]=1;
for(int i=1;i<=tot+k;++i)
for(int j=0;j<=k;++j){
//case 0
f[i][j][0]|=f[i-1][j][0];
if(a[i]!=9) f[i][j][0]|=f[i-1][j][1];
//case 1
if(a[i]!=0) f[i][j][1]|=f[i-1][j-1][0];
f[i][j][1]|=f[i-1][j-1][1];
}
bool fl=0;
if(f[tot+k][k][0]==0&&f[tot+k][k][1]==0) puts("-1");
else{
memset(mst,0,sizeof(mst));
int cur=k;
for(int i=tot+k;i>=1;--i){
int val=-1;
if(a[i]==9){
if(mst[i]==0&&f[i-1][cur][0]) val=0;
else if(cur&&f[i-1][cur-1][1]) val=0,--cur,mst[i-1]=1;
else val=1,--cur;
}else if(a[i]==0){
if(mst[i]==0&&f[i-1][cur][0]|f[i-1][cur][1]){
val=0;
}
else val=9,--cur,mst[i-1]=1;
}else{
if(mst[i]==0&&(f[i-1][cur][0]|f[i-1][cur][1])){
val=0;
if(f[i-1][cur][0]==0) mst[i-1]=1;
}
else if(cur&&f[i-1][cur-1][1]) val=10-a[i]-1,--cur,mst[i-1]=1;
else val=10-a[i],--cur;
}
if(val!=0) fl=1;
if(fl) printf("%d",val);
// cerr<<val;
}
if(!fl){
int pos=1;
while(a[pos]==9) ++pos;
putchar('1'); --pos;
while(pos) putchar('0');
}
puts("");
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3832kb
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1 54322 999999999987654322 9910
result:
ok 4 lines
Test #2:
score: -100
Output 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:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...