[Hoj 2901] calculation [binary theorem]

Source: Internet
Author: User

Most of the explanations are as follows:

Http://hi.baidu.com/timer/item/ebc8c7ef908085215b2d6476

Question:

(1 ^ B + 2 ^ B +... + A ^ B) %

1 ≤ A ≤ 1000000000, 1 ≤ B ≤ 1000000000

Ideas:

A and B have a large range. It is certainly not possible to directly calculate or directly use a rapid power modulo.

Note the question condition: B must be an odd number.

Observe the original formula and find [C ^ B + (a-c) ^ B] % A = 0.

(Proof: Just split (a-c) ^ B with the binary theorem)

So it is easy: When a is an odd number, the original formula is 0; When a is an even number, the original formula is = (a/2) ^ B %. and then quickly modulo out the solution.

Click it by yourself:

#include <cstdio>using namespace std;typedef long long ll;int mod;ll mypow(ll a, ll n){    ll ret = 1;    while(n)    {        if(n%2) ret = (ret*a)%mod;        a = (a*a)%mod;        n >>= 1;    }    return ret;}int cal(int a, int b){    if(a&1) return 0;    return mypow(a/2,b);}int main(){    int a,b;    while(scanf("%d %d",&a,&b)==2)    {        mod = a;        printf("%d\n",cal(a,b));    }}

It's still a pattern...

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.