5344 2023. 04. 26 09:46:47 kohumark Számlexikon cpp17 Time limit exceeded 10/100 300ms 3540 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';
	}

}
Subtask Sum Test Verdict Time Memory
base 10/100
1 Time limit exceeded 0/0 254ms 1780 KiB
2 Time limit exceeded 0/0 282ms 1264 KiB
3 Accepted 10/10 3ms 2236 KiB
4 Wrong answer 0/10 3ms 2460 KiB
5 Time limit exceeded 0/10 264ms 2512 KiB
6 Time limit exceeded 0/10 259ms 2640 KiB
7 Time limit exceeded 0/10 279ms 2760 KiB
8 Time limit exceeded 0/10 256ms 2280 KiB
9 Time limit exceeded 0/10 275ms 3204 KiB
10 Time limit exceeded 0/10 279ms 2696 KiB
11 Time limit exceeded 0/10 268ms 3540 KiB
12 Time limit exceeded 0/10 300ms 3192 KiB