hdu6050Funny Function(快速幂,高校2)

来源:互联网 发布:省市区json数据 编辑:程序博客网 时间:2024/06/09 18:39

Funny Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1224    Accepted Submission(s): 604


Problem Description
Function Fx,ysatisfies:

For given integers N and M,calculate Fm,1 modulo 1e9+7.
 

Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
 

Output
For each given N and M,print the answer in a single line.
 

Sample Input
22 23 3
 

Sample Output
233
 

Source
2017 Multi-University Training Contest - Team 2


题意:给你,n,m,求出f(m,1),
由公式推出通项公式:
n为偶数时F(m,1)=2*(2^n-1)^(m-1)/3
n为奇数时F(m,1)=(2*(2^n-1)^(m-1)+1)/3

由于指数较大,用快速幂

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <iostream>#define LL unsigned long long#define inf 0x3f3f3f3f#define mod 1000000007using namespace std;LL pow3(LL a,LL b)///a的b次方取余r{    a%=mod;    if(a==0)    return 0;    LL ans = 1,base = a;    while(b)    {        if(b&1)//奇数时            ans = (ans*base)%mod;        base= (base*base)%mod;        b>>=1;    }    return ans;}int main(){    int t;    LL n,m,ans;    scanf("%d",&t);    while(t--)    {        scanf("%lld %lld",&n,&m);        if(n%2==0)        ans=(2*pow3((pow3(2,n)-1)%mod,m-1))*333333336%mod;        else        ans=(2*pow3((pow3(2,n)-1)%mod,m-1)+1)*333333336%mod;        cout<<ans%mod<<endl;    }    return 0;}



原创粉丝点击