UVA 10515 - Powers Et Al

Finding the exponent of any number can be very troublesome as it grows exponentially. But in this
problem you will have to do a very simple task. Given two non-negative numbers m and n, you will
have to find the last digit of mn in decimal number system.
Input
The input file contains less than 100000 lines. Each line contains two integers m and n (Less than
10101. Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each set of input you must produce one line of output which contains a single digit. This digit is
the last digit of mn.
Sample Input
2 2
2 5
0 0
Sample Output
4
2

题意:给你两个数m,n,让你求m的n次方的最后一位是多少

思路:最后一位只与m的个位相关,又因为0到9的次幂是循环的,找出它们的循环体,然后用n对循环题的长度取余就行(注意,这里n与m太大存不下,要开字符数组或字符串)

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
string x,y;
int a2[]={2,4,8,6};
int a3[]={3,9,7,1};
int a4[]={4,6};
int a7[]={7,9,3,1};
int a8[]={8,4,2,6};
int a9[]={9,1};
int main()
{
    int n,m;
    while(cin>>x>>y)
    {
         if(x=="0"&&y=="0")
            break;
        n=(int)(x[x.size()-1]-'0');
        m=0;
        if(y=="0")
        {
            printf("1\n");
            continue;
        }
        if(n==1||n==5||n==6||n==0)//特殊情况
            printf("%d\n",n);
        if(n==2)
        {
                for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;//次幂求余
        }
            int t;
            t=m%4;
            if(t==0)
                t=3;
            else
                t--;
            printf("%d\n",a2[t]);
        }
        if(n==3)
        {
            for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;
        }
            int t;
            t=m%4;
            if(t==0)
                t=3;
            else
                t--;
            printf("%d\n",a3[t]);
        }
        if(n==4)
        {
            for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;
        }
            int t;
            t=m%2;
            if(t==0)
                t=1;
            else
                t--;
            printf("%d\n",a4[t]);
        }
        if(n==7)
        {
            for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;
        }
           int t;
            t=m%4;
            if(t==0)
                t=3;
            else
                t--;
            printf("%d\n",a7[t]);
        }
        if(n==8)
        {
            for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;
        }
            int t;
            t=m%4;
            if(t==0)
                t=3;
            else
                t--;
            printf("%d\n",a8[t]);
        }
        if(n==9)
        {
            for(int i=0;i<y.size();i++)
        {
            m=(m*10+(int)(y[i]-'0'))%4;
        }
            int t;
            t=m%2;
            if(t==0)
                t=1;
            else
                t--;
            printf("%d\n",a9[t]);
        }
    }
}

发布了261 篇原创文章 · 获赞 14 · 访问量 7399

猜你喜欢

转载自blog.csdn.net/weixin_43244265/article/details/104225035
AL