Hdoj 1061 rightmost Digit

Source: Internet
Author: User

Find out the rules of mathematics

Original question:

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 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 = rightmost, so the-the-digit is 7. In the second case, 4 * 4 * 4 * 4 = at the very rightmost digit is 6.

Explanation: (excerpt from the Internet)

For digital 0~9, their n-th side, we only see the last number:
0^1=0; 0^2=0; Cycle cycle t=1;
1^1=1; 1^2=1; Cycle cycle t=1;
2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; Cycle cycle t=4;
3^1=3; 3^2=9; 3^3=7; 3^4=1;3^5=3; Cycle cycle t=4;
4^1=4; 4^2=6; 4^3=4; Cycle cycle t=2;
  
You can figure it out by yourself, and you'll get a rule:
Their cycle time only 1,2,4 these three kinds, so for the source code of a, we can only calculate a%4 times can be, greatly reduce the number of cycles

Source:

1#include <iostream>2 using namespacestd;3 4 intzhou[Ten];5 6 intMain () {7     intN CIN >>N;8      while(n--)    {9         Long Long intnum;TenCIN >>num; One         intA = num%Ten; A         intb = (A * a)%Ten; -         intc = (b * a)%Ten; -         intD = (c * a)%Ten; the         intm = num%4; -         Switch(m) { -              Case 0: cout << D << Endl; Break; -              Case 1: cout << a << Endl; Break; +              Case 2: cout << b << Endl; Break; -              Case 3: cout << c << Endl; Break; +         } A     } at     return 0; -}

Hdoj 1061 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.