acm--Fast Power--hdoj 1061--rightmost Digit

Source: Internet
Author: User


Hdoj Address: http://acm.hdu.edu.cn/showproblem.php?pid=1061

Fast Power algorithm Explanation: http://blog.csdn.net/qq_26891045/article/details/51334101


Rightmost DigitTime Limit: -/1 theMS (java/others) Memory Limit:65536 /32768K (Java/others)Total Submission (s):45746 Accepted Submission (s): 17221

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
 the Hint in the first case, 3 * 3 * 3 = digit, so the rightmost is 7.In the second case, 4 * 4 * 4 * 4 =, so the rightm OST digit is 6.



================================ born proud of the split line =============================




The meaning of the topic has been very clear, that is, to seek the single digit of n^n, it is equivalent to a single digit of the MoD 10 operation, here directly with the fast power algorithm, you can get the result, the topic given n may be relatively large, so a long long is more insurance.


#include <iostream> #include <stdio.h>using namespace std;/**  fast power algorithm */long long calculation (long long A, Long long B,long long c) {   int ans=1;   a=a%c;   while (b>0) {      if (b%2==1)        ans= (ans*a)%c;      B=B/2;      A= (a*a)%c;   }   return ans;} int main () {   int n,n;   cin>>n;   while (n--) {     cin>>n;     printf ("%d\n", Calculation (n,n,10));   }   return 0;}

Reference Blog: http://blog.csdn.net/whjkm/article/details/42803805



acm--Fast Power--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.