HDU 1061.Rightmost Digit "number theory and Method" "August 30"

Source: Internet
Author: User

Rightmost Digit

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
HintIn the first case, 3 * 3 * 3 = rightmost, so the digit is 7.In the second case, 4 * 4 * 4 * 4 = The rightmost digit is 6.
give you a number, output the single digit of the n^n. This problem is much simpler than it is. The method of this problem uses the second method of HDU 1097, can refer to my blog .

the mantissa is 0,1,5,6 regardless of how many times the mantissa remains unchanged, while the mantissa is 4 and 9 for every 2 cycles,
2,3,7,8 for every 4 cycles. The loop results are as follows:
0,1,5,6: The number of digits is always 0,1,5,6
2:6,2,4,8 Cycle
3:1,3,9,7 Cycle
4:6,4 Cycle
7:1,7,9,3 Cycle
8:6,8,4,2 Cycle
9:1,9 Cycle

Find this law, this problem is not difficult. Otherwise, no idea.

The code is as follows:

#include <cstdio>int main () {    int t,n,f[10][10]={{1,0},{1,1},{4,2,4,8,6},{4,3,9,7,1},{2,4,6},{1,5},{1,6} , {4,7,9,3,1},{4,8,4,2,6},{2,9,1}};    scanf ("%d", &t);    while (t--) {        scanf ("%d", &n);        int m=n%f[n%10][0];        if (!m) m=f[n%10][0];        printf ("%d\n", F[n%10][m]);    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1061.Rightmost Digit "number theory and Method" "August 30"

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.