HDOJ-1061-Rightmost Digit (求n^n的最低位)

来源:互联网 发布:在线图片合成软件 编辑:程序博客网 时间:2024/05/29 10:54

/*

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6515    Accepted Submission(s): 2454


Problem Description
Given a positive integer N, you should output the most right digit of N^N.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
 

Output
For each test case, you should output the rightmost digit of N^N.
 

Sample Input
234
 

Sample Output
76
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

 

 

*/

 

比求最高位简单多了,任何两个数相乘的最低位一定是它们最低位相乘所得结果的最低位

如9999 可转化为求99个9相乘后最低位是什么,而同一个数连乘结果是具有周期性的,周期不大于10

 

 

#include <iostream>

#include <stdio.h>

using namespace std;

bool l[10];

int r[10];

int main()

{

    int t;

    while(cin>>t)

    {

        while(t--)

        {

            memset(l,0,sizeof(l));

            memset(r,0,sizeof(r));

            unsigned long n;

            cin>>n;

            int a=n%10;

            int b=a;

            int i=1;

            l[b]=true;r[0]=b;

            b=(b*a)%10;

            while(!l[b])

            {

                l[b]=true;

                r[i++]=b;

                b=(b*a)%10;

            }

            cout<<r[(n-1)%i]<<endl;

        }

    }

    return 0;

}

原创粉丝点击