Thursday, April 28, 2016

UVA 10515 - Powers Et Al -solution

Unknown
#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;

int main()
{
    char m[111],n[111];
    while (scanf("%s%s",m,n)!=EOF)
    {
        int lm=strlen(m);
        int ln=strlen(n);
        if (lm==1 && ln==1 && m[0]=='0' && n[0]=='0')
        return 0;
        if (ln==1&& n[0]=='0')
        {
            printf("1\n");
            continue;
        }
        int d=m[lm-1]-'0'; //last digit of m
        int cycle;
        if (d==0 || d==1||d==5 ||d==6)
        {
            printf("%d\n",d);
            continue;
        }
        else if (d==2|| d==3 || d==7 || d==8) cycle=4;
        else if (d==4 || d==9) cycle=2;
        int power;
        if (ln==1) power=(n[ln-1]-'0')%cycle;
        else
        power=((n[ln-2]-'0')*10+(n[ln-1]-'0'))%cycle;
        if (power==0) power=cycle;
        d=pow(d,power);
        d=d%10;
        printf("%d\n",d);
    }
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,