Rightmost digit ACM hdu1061

Source: Internet
Author: User
Rightmost digit

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 12357 accepted submission (s): 4773

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 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 ).

Outputfor each test case, You shoshould output the rightmost digit of N ^ N.

Sample input2
3
4

Sample output7
6

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.

 

Authorignatius. L is mainly used to master the Modulo-power operation, and to obtain a single digit.
 # Include  <  Stdio. h  >  
# Include < Iostream >
Using Namespace STD;
Int Solve ( Long Long N)
{
Int T, B;
Long Long I;
B = N % 10 ;
T = 1 ;
If (B = 0 ) Return 0 ;
While (N)
{
If (N % 2 = 1 )
{
T * = B;
T % = 10 ;
}
B * = B;
B % = 10 ;
N /= 2 ;

}
Return T;
}
Int Main ()
{
Int T;
Long Long N;
CIN > T;
While (T -- )
{
CIN > N;
Cout < Solve (N) < Endl;
}
Return 0 ;
}

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.