Sdut 3097-Xiao Xin Love math (Fast power)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: n^m%1000000007 N (1 <= n <= 10^15),m (1 <= m <= 10^12)

A little pit. n Too much can overflow, pow_mod (n,m,mod) =pow (n%mod,m,mod)

Deduce it ...

N^m%mod= (n%mod+k*mod) ^m%mod=[(n%mod) ^m +: A bunch of mods]%mod = (n%mod) ^m%mod

Haven't hit the code for ages .... Qaq

Hardware to get out of the Xiang ... All day long hold the video to see how people write how to write ... Various circuit diagrams fly in the sky ..... It's like a yard farm.

The fact proves that the algorithm is difficult to say. At least it's not forgotten .... 233333333333

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 10000002#define _ll __int64# Define ll long long#define INF 0x3f3f3f3f#define Mod 1000000007#define pp pair<int,int> #define ull unsigned long lon Gusing namespace Std;ll pow_mod (ll a,ll n,ll p) {if (n==0) return 1;ll ans=pow_mod (a,n/2,p); Ans=ans*ans%p;if (n%2==1) ans= Ans*a%p;return ans;} ll N,m;int Main () {while (~scanf ("%lld%lld", &n,&m)) printf ("%lld\n", Pow_mod (N%mod,m,mod)); return 0;}




Sdut 3097-Xiao Xin Love math (Fast power)

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.