记录编号 607755 评测结果 AAAAAAAAAA
题目名称 2949.[SYOI 2018] WHZ 的数字 最终得分 100
用户昵称 Gravatar梦那边的美好TE 是否通过 通过
代码语言 C++ 运行时间 0.086 s
提交时间 2025-10-20 10:09:26 内存使用 3.68 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef __int128 ll;
bool read(ll &x){
	unsigned long long val;
	if(!(cin>>val))return 0;
	x=val;return 1;
}
void write(ll x){
	unsigned long long val=x;
	cout<<val<<endl;
	return;
}
ll pw[21],f[21],g[21],n,k;
int c[21],t;
ll ask(ll x){
	if(x<0)return 0;
	if(x<10)return 1;
	t=0;while(x)c[++t]=x%10,x/=10;
	ll cnt=0,tmp=0;
	if(t>=2)cnt+=g[t-1];
	for(int i=t;i>=1;i--){
		for(int j=(i==t);j<c[i];j++){
			cnt+=f[i-1];
			cnt+=pw[i-1]*(tmp+(j==0));
		} 
		if(c[i]==0)tmp++;
	}
	cnt+=tmp;
	return cnt;
}
void work(){
	ll sum=ask(n);
	ll L=0,R=n,mid;
	while(L<R){
		mid=(L+R+1)>>1;
		if(sum-ask(mid-1)>=k)L=mid;
		else R=mid-1;
	}
	write(L);
	return;
}
ll count(ll x){
	ll res=(x==0);
	while(x){
		if(!(x%10))res++;
		x/=10;
	}
	return res;
}
ll query(ll x){
	ll cnt=0;
	for(ll i=0;i<=x;i++)cnt+=count(i);
	return cnt;
}
int main(){
	freopen("whz_number.in","r",stdin);
	freopen("whz_number.out","w",stdout); 
	pw[0]=1,f[1]=1,g[1]=1;
	for(int i=1;i<=20;i++)pw[i]=pw[i-1]*10;
	for(int i=2;i<=20;i++)f[i]=10*f[i-1]+pw[i-1];
	for(int i=2;i<=20;i++)g[i]=g[i-1]+9*f[i-1];
	while(read(n))read(k),work();
	return 0;
}