64-bit integer multiplication rapid power

Title: The result of evaluating the a * b% p, (1 <= a, b, p <= 10 ^ 18)

This question is similar to the previous question and just need to multiply into a plus (ans initially 0)

Such as : 2 * 15 to seek 2 * 7 + 2 * 7 + 2 * 1 (request 2 * 15), and then seek 2 * 3 + 2 * 3 + 2 * 1 (request 2 * 7) The last request 2 * 2 + 1 2 * (2 * 3 request);

code: 

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define int long long
using namespace std;
int a,b,ans=0,p;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
signed main()
{
    int a1,b1;
    a=read(),b=read(),p=read();
    a1=a,b1=b;
    while(b){
        if(b%2==1)ans=(ans+a)%p;
        a=(a+a)%p;
        b=b/2;
    }
    printf("%lld",years);
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/nlyzl/p/11371458.html