2017杭电多校联赛第二场-Funny Function (hdu6050)快速幂解数学方程

Funny Function

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


Problem Description
Function  Fx,y satisfies:

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
  
  
2 2 2 3 3
 

Sample Output
  
  
2

33

题目大意:给你n,m,求f(m,1)

由矩阵快速幂解决。

ac代码:

// F(m,1)=(2*k1^(m-1)+(1+(-1)^(m+1))/2)/3 其中k1=(2^n-1); 
#include <iostream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
const long long mod = 1e9+7;
long long f(long long x,long long k)
{
    if (k==0) return 1;
    if (k&1) 
    {
        return f(x,k-1)*x%mod;
    }else {
      long long t=f(x,k/2);
      return t*t%mod;
    } 
}
void exgcd(long long a,long long b,long long &x,long long &y)
{
    if (!b) {
        x=1;y=0;
        return;
    }
    exgcd(b,a%b,y,x);
    y-=x*(a/b);
}
long long x,y;
int main()
{
    int T;
    exgcd(3,mod,x,y);
    x=(x+mod)%mod;
    long long n3=x;
    scanf("%d",&T);
    while (T--)
    {
        long long  n,m;
        scanf("%I64d%I64d",&n,&m);
        long long k1=(f(2,n)-1+mod)%mod;
        long long tmp1=2*f(k1,m-1)%mod;
        if (n&1)
        {
            printf("%I64d\n",(tmp1+1+mod)%mod*n3%mod);
        }else printf("%I64d\n",tmp1*n3%mod);
    }
}

题目链接: 点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=6050

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值