hdu2035 A^b (congruence and its basic properties, fast power)

Source: Internet
Author: User

began to use the POW function, 6789^10000 calculate the negative, it should be super, but the internet search for a bit said C + + POW () is to support the Longlong to provide overloaded functions of 51456681, is the result of super???

Later, I found that the loop internal variables with the int ...

And then ... Circular *a, or wa ...

Focus on this: have been riding down, the result will be super, there is a mathematical rule: the result of each time to 1000 to take the remainder, the last three will not change, do not understand a word, fill up the knowledge of number theory (congruence operation and its basic properties)

http://www.matrix67.com/blog/archives/236

Code:

#include <iostream>#include<stdio.h>using namespacestd;intMain () {Long Long intn,m;  while(cin>>n>>m) {if(n==0&&m==0)         Break; Long Long intt=1;  for(Long Long inti =1; i<=m;i++) {T*=N; if(t>= +) T= t% +; } cout<<t<<Endl; }    return 0;}
View Code

Fast power:

The power operation can be calculated efficiently by the fast power. If we use loops to calculate, then the time complexity is O (n), using the fast power of the word only with O (log n).

If we solve 2^k. It can be represented as

X^n = ((x2) 2 .....)

As long as the K-squared operation is possible, we can think of, first, n is represented as 2 power-side second and

n = 2^k1 + 2^k2 + 2^k3 ...

's Got

X^n = x^ (2^K1) x^ (2^K2) x^ (2^K3) ...

Quick Power Templates:

typedefLong Longll//Note that this is not always a long long and sometimes int is okll Mod_pow (ll X, ll N, ll MoD) {ll res=1;  while(N >0 ){         if(N &1res = res * x% MoD;//n&1 actually here and n%2 expressed a meaningx = x * x%MoD; N>>=1;//N >>= 1 This and n/=2 expression is a meaning    }    returnRes;}

Recursive version:

Long Long Ll;ll Mod_pow (ll X, ll N, ll MoD) {    if0return1;     2 , MoD);     if 1 ) res = res * x% mod;     return Res;}

Solve this problem with a quick power:

#include <stdio.h>intMod_pow (intXintNintMoD) {//Fast Power    intres =1;  while(N >0 ){        if(N &1) Res = res * x%MoD; X= x * x%MoD; N>>=1; }    returnRes;}intMain () {intM,n;  while(SCANF ("%d%d", &m,&n), n| |m) printf ("%d\n", Mod_pow (M,n, +)); return 0;}
View Code

hdu2035 A^b (congruence and its basic properties, 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.