UVa – 11549 – Calculator Conundrum

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long int Long;
#define d(i) (i > 0 ? int (log10(i)) + 1 : 1)
#define f(k,n) (d(k*k) < n ? (k * k) : ((k * k) / int (pow(10, d(k*k) - n))))

int main() {
	Long k, T, n;
	scanf("%lld", &T);
	while (T-- && scanf("%lld %lld", &n, &k)) {
		Long m = k, tortoise = k, hare = k;
		do {
			m = max((tortoise = f(tortoise, n)), m);
			m = max((hare = f(hare, n)), m);
			m = max((hare = f(hare, n)), m);
		} while (tortoise != hare);
		printf("%lld\n", m);
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: