53442023-04-26 09:46:47kohumarkSzámlexikoncpp17Time limit exceeded 10/100300ms3540 KiB
#include <iostream>
#define ull unsigned long long
using namespace std;

ull p10(int k){
	if(k==0) return 1;
	else return 10*p10(k-1);
}

ull ctoi(char x){
	return (ull)x-48;
}

ull end(int x){
	if(x<0) return 0;
	if(x==0) return 1;
	else return p10(x)+end(x-1);
}

ull qh(string k){
	if(k.length()==1) return ctoi(k[0])+1;
	else if((int)k.length()==0) return 0;
	else return ctoi(k[0])*end((int)k.length()-1)+qh(k.substr(1))+1;
}

///1+10+10*10+10*10*10

ull q(string k, ull p){
	int len=(int)(k.length());
	int ct=end(len-1)*(ctoi(k[0])-1);
	ct+=qh(k.substr(1));
	//cout << ct;
	return ct;
}

bool jo(string n, string k){
	bool ok=false;
	for(int i=0; i<(int)k.length(); i++){
		if(n[i]<k[i]){ok=true;break;}
		else if(n[i]>k[i]) break;
	}
	return ok;
}

ull pq(string n, string k){
	if(n.length()>k.length()){
		int ln=(int)n.length();
		int lk=(int)k.length();
		int d=0;
		if(ln>lk){
			string ek(ln-lk-1,'0'); k+=ek;
			d=ln-lk-2;
		}
		return q(k,0)-q(n.substr(0,ln-1),0)+q(n,0)-d;
	}
	else return 0;
}

int main(){
	int t; cin >> t;
	
	for(int i=0; i<t; i++){
		string k; int p; cin >> k >> p;
		ull n=0; while(p10(n)<(ull)stoi(k)) n++;
		n=p10(n);
		while(pq(to_string(n),k)<(ull)p) n*=10;
		ull ma=n; n/=10;
		while(pq(to_string(n),k)!=(ull)p&&ma!=n) n++;
		if(n==ma) cout << 0 << '\n';
		else cout << n << '\n';
	}

}
SubtaskSumTestVerdictTimeMemory
base10/100
1Time limit exceeded0/0254ms1780 KiB
2Time limit exceeded0/0282ms1264 KiB
3Accepted10/103ms2236 KiB
4Wrong answer0/103ms2460 KiB
5Time limit exceeded0/10264ms2512 KiB
6Time limit exceeded0/10259ms2640 KiB
7Time limit exceeded0/10279ms2760 KiB
8Time limit exceeded0/10256ms2280 KiB
9Time limit exceeded0/10275ms3204 KiB
10Time limit exceeded0/10279ms2696 KiB
11Time limit exceeded0/10268ms3540 KiB
12Time limit exceeded0/10300ms3192 KiB