https://codeforces.com/problemset/problem/1373/E
其实这题挺水的,然而看见F好像更可做一些,就去WA F一直到结束了。。。
看了jly的代码。。清晰易懂思路简单。。。
这题关键的问题在于那种末尾好多个9,然后进位,就一片0,于是并不知道这样如何构造
其实就可以直接枚举末尾有多少个9就行了,枚举完以后,我们算出这d个9和末尾上的数字c,在k+1个数字中会产生多少贡献,然后再看剩下还要凑多少值,如果<=8,那么就是这个数+d个9+末尾c就行了,如果>8,那么就是a99989999c这样的结构能最小。
枚举一个c和d个9,然后构造出最小值,更新记录答案就行了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxl=3e5+10;
int n,m,cas,k,cnt,tot;
int a[maxl],b[maxl];
char s[maxl];
bool in[maxl];
ll ans;
inline void prework()
{
scanf("%d%d",&n,&k);
}
inline void mainwork()
{
ans=-1;int tmp,res;__int128 num;
for(int c=0;c<=9;c++)
for(int d=0;9*d+c<=n;d++)
{
tmp=0;
for(int i=0;i<=k;i++)
if(i+c>=10)
tmp+=1+(i+c)%10;
else
tmp+=9*d+i+c;
res=n-tmp;
num=0;
for(int i=1;i<=d;i++)
num=num*10+9;
num=num*10+c;
if(res<0 || res%(k+1)!=0)
continue;
if(res==0)
{
if(ans==-1 || num<ans)
ans=num;
continue;
}
num=0;res=res/(k+1);
if(res<=8)
{
num=res;
for(int i=1;i<=d;i++)
num=num*10+9;
num=num*10+c;
}
else
{
res-=8;
num=res%9;
res-=num;
for(int i=1;i<=res/9;i++)
num=num*10+9;
num=num*10+8;
for(int i=1;i<=d;i++)
num=num*10+9;
num=num*10+c;
}
if(num<1e18 && (ans==-1 || num<ans))
ans=num;
}
}
inline void print()
{
printf("%lld\n",ans);
}
int main()
{
int t=1;
scanf("%d",&t);
for(cas=1;cas<=t;cas++)
{
prework();
mainwork();
print();
}
return 0;
}