HDU 1061 rightmost digit

Source: Internet
Author: User

 

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 Input
234
Sample output
76

 

It is impossible to find a legal question in a harmonious manner and to know the scale of data. In the first place, I thought that only the last bit can be used for computation to simplify computation. However, if you ignore repeated operations, it will still time out. So we can only use the result after the last digit is multiplied to get the last digit to find the rule. In fact, there is a cycle. For example, 2 ^ 2 = 4, 2 ^ 3 = 8, 2 ^ 4 = 6, 2 ^ 5 = 2, 2 ^ 6 = 4 ......

The first thing I wrote was to use if to judge all single digits. The result code was lengthy. By referring to others' practices, you can use arrays to store and simplify code.

# Include <stdio. h> <br/> int main () <br/>{< br/> int N; <br/> int A [10] [4] ={{ 0 }, {1}, {6, 2,}, {1, 3, 9, 7}, {6, 4}, {5}, {6}, {, 9, 3}, {6, 4, 2 }, {1, 9 }}, D, num; <br/> scanf ("% d", & num); <br/> while (Num --) <br/>{< br/> scanf ("% lD", & N); <br/> d = n % 10; <br/> If (D = 0 | D = 1 | D = 5 | D = 6) <br/> printf ("% d/N", d); <br/> else if (D = 4 | D = 9) <br/> printf ("% d/N", a [d] [n % 2]); <br/> else if (D = 2 | D = 3 | D = 7 | D = 8) <br/> printf ("% d/N", a [d] [n % 4]); <br/>}< br/> return 0; <br/>}< br/> 

 

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.