HDU 6050 Funny Function

Funny Function

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


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
 

Source
 

Recommend
liuyiding


看到Fn=Fn-1+2*Fn-2,就想到类斐波那契数列,再想到特征根方程。

即两特征根方程为:x^2=x+2

解得x1=2,x2=-1.

Fn=ax1^n+bx2^n.

根据F1=F2=1,

解得Fn=2^n/3-(-1)^n/3.


接下来根据性质三,

不难得出F(m,1)通项为:

F(m,1)=(2*(2^n-1)^(m-1)+(1-(-1)^n)/2)/3。


比赛前并没接触过除法的模除,

可惜了,晒上赛后补题代码。

/*answered by Asmire*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;

ll quick_pow(ll a,ll b,ll m)
{
    ll ans=1;
    while(b)
    {
       if(b&1){
            ans=ans*a%m;
       }
       b=b>>1;
       a=a*a%m;
    }
    return ans;
}

void exgcd(ll a,ll b,ll &x,ll &y)
{
    ll temp;
    if(!b){
        x=1;y=0;
        return ;
    }
    exgcd(b,a%b,x,y);
    temp=x;
    x=y;
    y=temp-a/b*y;
}

int main(){
    int T;
    ll n,m;
    ll x,y;
    ll cnt1,tmp;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d %I64d",&n,&m);
        cnt1=(quick_pow(2,n,mod)-1)%mod;
        tmp=quick_pow(cnt1,m-1,mod)*2%mod;
        exgcd(3,mod,x,y);
        ll k=x;
        if(n%2==0)
            printf("%I64d\n",tmp*k%mod);
        else
            printf("%I64d\n",(tmp+1)*k%mod);
    }
    return 0;
}




  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值