UVa : 11549 (CALCULATOR CONUNDRUM)


U will get a sequence that will be repeated forever

for example, n = 1 and k = 6
>> 3
>> 9
>> 8
>> 6
>> 3
>> 9
>> 8
>> 6
>> 3
… so on

for n = 2 and k = 99
>> 98
>> 96
>> 92
>> 84
>> 70
>> 49
>> 24
>> 57
>> 32
>> 10
>> 10
….

// http://uva.onlinejudge.org/external/115/11549.html
// Runtime : 1.736s
// Tag : Repeated sequence 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long
using namespace std;

LL firstN (LL k, int n)
{
    char a [30];
    sprintf (a, "%ld", k);
    a [n] = 0;
    return atol (a);
}

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int n;
        LL k;

        scanf ("%d %ld", &n, &k);

        map <LL, bool> m;

        LL maxi = k;

        while ( !m [k] ) {
            m [k] = true;
            k *= k;
            k = firstN (k, n);
            maxi = maxi > k ? maxi : k;
        }

        printf ("%ld\n", maxi);
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

One thought on “UVa : 11549 (CALCULATOR CONUNDRUM)

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