Last digit of 51nod1004 n ^ n, 51nod1004 last digit

Source: Internet
Author: User

Last digit of 51nod1004 n ^ n, 51nod1004 last digit
Source: Author Ignatius. L (Hdu 1061) benchmark time limit: 1 second space limit: 131072 KB score: 5 difficulty: Level 1 algorithm question collection attention to give an integer N, output the last digit in decimal format of N ^ N (N's N power. Input

N (1 <= N <= 10 ^ 9)
Output
Output the last number of N ^ N
Input example
13
Output example
3
Li Ye (subject provider) C ++ runtime: 1000 MS, space limit: for the 131072 KB example and language description, please refer to the following example for a fast power bare question, but you can find the pattern of the circular section by making a table.
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define LL  long long  6 using namespace std; 7 const LL MAXN=200000001; 8 inline LL read() 9 {10     char c=getchar();LL flag=1,x=0;11     while(c<'0'||c>'9')    {if(c=='-')    flag=-1;c=getchar();}12     while(c>='0'&&c<='9')    x=(x*10+c-48),c=getchar();    return x*flag;13 }14 LL a[15]={1,1,4,4,2,1,1,4,4,2};15 LL n;16 int main()17 {18     n=read();19     LL p=n%10;20     n=n%a[p];21     if(n==0)22     {23         LL ans=1;24         for(LL i=1;i<=a[p];i++)25             ans=ans*p;26         cout<<ans%10;27     }28     else29     {30         LL ans=1;31         for(LL i=1;i<=n;i++)32             ans=ans*p;33         cout<<ans%10;    34     }35     return 0;36 }

 

 

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.