HDU rightmost Digit

Source: Internet
Author: User

Rightmost Digit time limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other) total submission (s): 87 Accepted Submission (s): 38Font:Times New Roman|Verdana|GeorgiaFont Size:← →Problem Descriptiongiven A positive integer N, you should output the most right digit of n^n.
Inputthe input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is contains a single positive integer N (1<=n<=1,000,000,000).
Outputfor Each test case, you should output the rightmost digit of n^n.
Sample Input
234
Sample Output
76
Hintin The first case, 3 * 3 * 3 =, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = at the very rightmost digit is 6. Authorignatius.l n for N and output the last one, because it is the second party so the data will be large to consider using large numbers. But the large number is troublesome, so find the law. By enumerating the data, you will find the last of N's n-side loops every four times. So as long as the loop by 4 times to get the answer. AC Code:
#include <iostream>using namespace Std;int main () {    long n,m,num,p;    cin>>num;    while (num--) {        cin>>n;        m = p = N;        if (n% 4 = = 0) n = 4;        else n = n% 4;        while (--n)  p = (p * m)%;        cout<<p<<endl;    }    return 0; }

Operation Result:

HDU rightmost Digit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.