HDU 6050 Funny Function

来源:互联网 发布:淘宝开店考试题 编辑:程序博客网 时间:2024/05/01 15:36

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,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
 

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;}




原创粉丝点击