hdu 6050 funny function

标签: 斐波那契
1029人阅读 评论(4) 收藏 举报
分类:

F1,1=F1,2=1
F1,i=F1,i1+2F1,i2|i>=3
Fi,j=j+N1k=jFi1,k
给定N,M,求Fm,1

首先昨天先写了几个,发现Fi,j=Fi,j1+2Fi,j2也是成立的,于是就有下面的:
Fi,j+1Fi,j=2Fi,j1
Fi,jFi,j1=2Fi,j2

Fi,3Fi,2=2Fi,1
迭加得:
Fi,j+1Fi,2=2j1k=1Fi,k
Fi,j+1Fi,1=Fi,1+Fi,2+...+Fi,j1+A
这里
A=2Fi,2+Fi,3+...+Fi,j1
j+1为奇数时
A=Fi,j

Fi,j+1Fi,1=jk=1Fi,k
j+1为偶数时,
2Fi,j+Fi,j+1=Fi,j+2Fi,j+1+Fi,1Fi,2=k=jk=1Fi,k

n为偶数时
Fi,1=nk=1Fi1,k=Fi1,n+1Fi1,1

[Fi,j+1Fi,j]=[1120][Fi,jFi,j1]

Fi,1=Fi1,1AnB0 A,B0
Fm,1=F1,1(AnB0)m1

同理n为奇数时一样,
Fm,1=F1,1(AnB1)m1
其中,

A=[1120]

n偶数
B=[1001]

n奇数
B=[0121]

#include<cstdio> 
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll P = 1000000007;
const int N=2;
ll n,m;

struct matrix{
    ll a[N][N];
    int row,col;
    matrix():row(N),col(N){memset(a,0,sizeof(a));}
    matrix(int x,int y):row(x),col(y){memset(a,0,sizeof(a));}
    ll* operator [] (int x){return a[x];}
    matrix operator * (matrix x){
        matrix tmp ;
        for (int i=0;i<=n+1;i++)
            for (int j=0;j<=n+1;j++){
                tmp[i][j]=0;
                for (int k=0;k<=n+1;k++)
                    tmp[i][j]=(tmp[i][j]+a[i][k]*x[k][j])%P;
            }
        return tmp;
    }   
    matrix operator - (matrix x){
        matrix tmp ;
        for (int i=0;i<=n+1;i++)
            for (int j=0;j<=n+1;j++){
                tmp[i][j]=a[i][j] - x[i][j] + P;
                tmp[i][j]%=P;
            }
        return tmp;
    }   
    void operator *= (matrix x){*this = *this * x;}
    matrix operator ^ (ll x){
        matrix ret;
        for (int i=0;i<=n+1;i++)ret[i][i]=1;
        matrix tmp = *this;
        for (;x;x>>=1,tmp*=tmp){if(x&1)ret *=tmp;}
        return ret;
    }
};

int main(){
    long long T,nn,mm;
    cin>>T;
    n=0;
    while(T--){
        scanf("%lld%lld",&nn,&mm);
        matrix A,B,ans;
        A[0][0]=1;A[0][1]=2;A[1][0]=1;A[1][1]=0;
        if(nn&1){
            B[0][0]=0;B[0][1]=2;
            B[1][0]=1;B[1][1]=-1;
        } else {
            B[0][0]=1;B[0][1]=0;
            B[1][0]=0;B[1][1]=1;
        }
        ans = ((A ^ nn) - B) ^ (mm-1);
        cout<<(ans[0][0])<<endl;
    }
    return 0;
}

然后用特征值算矩阵n次方,可以求得
Fm,1=(2n1)m123|n
Fm,1=(2n1)m12+13|n
除三要么用逆元,要么取模的时候先对3000000021取模,最后再对1000000007取模。
于是,

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

const ll P = 3000000021LL;
const ll MOD = 1000000007;

ll pow(ll a,ll n){
    if(n==0) return 1LL;
    ll tmp = pow(a,n/2);
    if(n&1)
        return tmp*tmp%P*a%P;
    return tmp*tmp%P;
}

int main(){
    int T;
    ll n,m;
    cin>>T;
    while(T--){
        scanf("%lld%lld",&n,&m);
        ll t = pow(2LL,n);
        t = (t - 1) % P;
        t = pow(t,m-1);
        if(n&1) t = t * 2LL + 1;
        else t = t * 2LL;
        t /= 3LL;
        cout<<t%MOD<<endl;
    }
    return 0;
}
查看评论

hdu6050 Funny Function【打表+找规律+矩阵快速幂】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6050 题意:看公式,求fm,1 解析:打表找规律 直接找答案的规律,根据题目的公式来推,太难了,...
  • Richie_ll
  • Richie_ll
  • 2017-07-27 21:11:23
  • 450

HDU6050(矩阵快速幂)

矩阵快速幂
  • Cai_Haiq
  • Cai_Haiq
  • 2017-08-01 10:54:23
  • 153

(多校)hdu 6050 Funny Function

Funny Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
  • nare123
  • nare123
  • 2017-07-27 19:27:05
  • 1119

Hdu6050 Funny Function(2017多校第2场)

Funny Function                                                                          Time Limit: ...
  • Jelly_acm
  • Jelly_acm
  • 2017-07-28 09:51:12
  • 669

HDU6050-Funny Function

Funny Function                                                                  Time Limit: 2000/100...
  • a664607530
  • a664607530
  • 2017-07-27 22:56:46
  • 1130

【HDU 6050 Funny Function】 + 数学

Funny FunctionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T...
  • WYK1823376647
  • WYK1823376647
  • 2017-07-28 11:51:54
  • 563

hdu 6050 funny function

F1,1=F1,2=1F_{1,1} = F_{1,2} = 1 F1,i=F1,i−1+2F1,i−2|i>=3F_{1,i} = F_{1,i-1} + 2F_{1,i-2} | i>=3 F...
  • RedPolya
  • RedPolya
  • 2017-07-28 14:28:12
  • 1029

HDU 6050 Funny Function

Funny Function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
  • zcy_2016
  • zcy_2016
  • 2017-07-31 21:55:47
  • 85

HDU 6050 Funny Function

HDU 6050 Funny Function Problem Description Function Fx,y
  • qq_36539373
  • qq_36539373
  • 2017-07-27 22:28:51
  • 318

Funny Function(HDU 6050)

Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e9+7. There is one inte...
  • luyehao1
  • luyehao1
  • 2017-07-29 18:50:12
  • 239
    个人资料
    持之以恒
    等级:
    访问量: 2万+
    积分: 720
    排名: 7万+