ACM水题系列 HDOJ 1061

来源:互联网 发布:交通大数据应用 编辑:程序博客网 时间:2024/05/18 03:13

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6526 Accepted Submission(s): 1688 
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.
题意,求N^N最右边的数。
思想:因为相同数相乘的个位数是有周期性规律的,周期不大于10,我们可以先求到N相乘的周期T,同时记录下不同的个位数存在数组r[]里,然后再用(N-1)%T得到该数的下标。
#include <iostream>#include <string.h>using namespace std;int main(){    int t;    int r[10];    bool l[10];    cin>>t;    while(t--){        memset(r,0,sizeof(r));        memset(l,0,sizeof(l));        int a,b,n=1;        unsigned long m;        cin>>m;        a=m%10;        b=a;        r[0]=b;        l[b]=true;        b = (b*a)%10;        while(!l[b]){            l[b] = true;            r[n++]=b;            b = (b*a)%10;        }        cout<<r[(m-1)%n]<<endl;    }    return 0;}


0 0