UVA 10515 (single digit for m^n)

Source: Internet
Author: User

Test instructions: Give two positive integers m,n (0<=m,n<=10^101) for the last digit of m^n

Idea: To find the law, for M words only consider single-digit line, one-digit number will not be multiplied by the carry and change, for index n table found 2,3,7,8 are in every four consecutive times a loop, 4 and 9 to 2 for the loop

So

Take the last k,n of M to take the last two bits D (judging if the positive integer divisible 4 takes the last two bits on the line, good proof), the last digit of M^n is:

Ans = (k^p)%10

p = d%4 = = 0? 4:d%4;


Expand Knowledge Points:

How can I tell if a very large number n (beyond long Long) is divisible by a very small m?

Recursion: \

S[0] = 0;

S[i] = (s[i-1]*10 + a[i])% m;

The final Judgment s[n.length-1] is equal to 0


#include <bits/stdc++.h>using namespace Std;int main () {#ifdef XXZ   //Freopen ("in", "R", stdin); #endif//XXZ    string m, N;    while (cin>>m>>n,m+n! = "XX")    {        int k = M[m.length ()-1]-' 0 ';        int d = (n.length () = = 1? N[0]: N[n.length ()-2]*10 + n[n.length ()-1])-' 0 ';        int p = d%4 = = 0? 4:d%4;        int ans = POW (k,p);        cout<<ans%10<<endl;    }    return 0;}



UVA 10515 (single digit for m^n)

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.