99502024-03-20 23:54:42111Számlexikoncpp17Wrong answer 0/100256ms2988 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int E(int n,int k,int j,int l){
	int ans=0;
	while(j>0&&l>0&&k/l==n/j){
	L:
		k-=k/l*l;
		n-=n/j*j;
		l/=10;
		j/=10;
	}
	if(j==0||l>0&&k/l>n/j){
		return ans;
	}
	if(l==0){
		ans+=n+1;
		return ans;
	}
	ans+=(n/j-(l>0?k/l+1:0))*j;
	ans+=n%j+1;
	goto L;
}

int F(int k,int l,int o){
	int ans=0;
	while(true){
		if(l==0){
			ans+=o*10;
			break;
		}
		ans+=(10-(l>0?k/l+1:0))*o;
		k-=k/l*l;
		l/=10;
		o/=10;
	}
	return ans;
}

int H(int n,int k){
	if(n==0){
		return 0;
	}
	int j=1,o=1;
	while(j*10<=n){
		j*=10;
		o*=10;
		o++;
	}
	int l=1;
	while(l*10<=k){
		l*=10;
	}
	return E(n,k,j,l)+F(k,l,o/10);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef CB
	freopen("be2.txt","r",stdin);
//	freopen("out.txt","w",stdout);
#endif
	int T;
	cin>>T;
	while(T--){
		int K,P;
		cin>>K>>P;
		int l=K,h=1e18;
		while(l!=h){
			int m=(l+h)/2;
			if(m-H(m,K)>=P){
				h=m;
			}
			else{
				l=m+1;
			}
		}
		cout<<(h-H(h,K)!=P?0:h)<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/100
1Wrong answer0/03ms1892 KiB
2Wrong answer0/0158ms2248 KiB
3Wrong answer0/103ms2332 KiB
4Wrong answer0/103ms2416 KiB
5Wrong answer0/104ms2388 KiB
6Wrong answer0/103ms2388 KiB
7Wrong answer0/104ms2520 KiB
8Wrong answer0/104ms2636 KiB
9Time limit exceeded0/10256ms2988 KiB
10Wrong answer0/10178ms2868 KiB
11Wrong answer0/10180ms2912 KiB
12Wrong answer0/1059ms2900 KiB