hdu 2817 A sequence of numbers——快速幂取模

来源:互联网 发布:虚拟机 linux nat 编辑:程序博客网 时间:2024/06/18 00:13

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817


快速幂模板题,含有加、减、乘的取模运算都可以先取模再运算。快速幂取模算法网上有很多详细的解释,这里贴一个while版

#include<iostream>#include<cstdio>using namespace std;#define ll long longconst int mod=200907;int qmod(ll a,ll b){    int t=1;    a%=mod;    while(b)    {        if(b&1) t=(t*a)%mod;         a=(a*a)%mod;        b/=2;    }    return t;}int main(){    ll a,b,c,k,res;    int T;    cin>>T;    while(T--)    {        cin>>a>>b>>c>>k;        if(c-b==b-a)        {            res=a%mod+((k-1)%mod)*((b-a)%mod);            res%=mod;        }        else        {            res=qmod(b/a,k-1);            res=(a%mod)*res%mod;        }        cout<<res<<endl;    }}

0 0