99492024-03-20 23:52:34111Számlexikoncpp17Time limit exceeded 90/100259ms3324 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(true){
		while(j>0&&l>0&&k/l==n/j){
			k-=k/l*l;
			n-=n/j*j;
			l/=10;
			j/=10;
		}
		if(j==0||l>0&&k/l>n/j){
			break;
		}
		if(l==0){
			ans+=n+1;
			break;
		}
		ans+=(n/j-(l>0?k/l+1:0))*j;
		ans+=n%j+1;
		n=j-1+k/l*j;
	}
	return ans;
}

int F(int k,int j,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;
		j/=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,j/10,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
base90/100
1Accepted0/03ms1888 KiB
2Accepted0/0177ms2252 KiB
3Accepted10/103ms2312 KiB
4Accepted10/103ms2524 KiB
5Accepted10/104ms2640 KiB
6Accepted10/103ms2708 KiB
7Accepted10/103ms2708 KiB
8Accepted10/104ms2816 KiB
9Time limit exceeded0/10259ms2144 KiB
10Accepted10/10192ms3032 KiB
11Accepted10/10200ms3108 KiB
12Accepted10/1061ms3324 KiB