hdu1588 Gauss Fibonacci(矩陣快速冪+二分求矩陣等比和),hdu1588fibonacci

來源:互聯網
上載者:User

hdu1588 Gauss Fibonacci(矩陣快速冪+二分求矩陣等比和),hdu1588fibonacci

題目:

Gauss FibonacciTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2858    Accepted Submission(s): 1186


Problem DescriptionWithout expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.

Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.

Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)

The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead. 
InputThe input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
 
OutputFor each line input, out the value described above. 
Sample Input
2 1 4 1002 0 4 100
 
Sample Output
2112
 
AuthorDYGG 
SourceHDU “Valentines Day” Open Programming Contest 2007-02-14

題意:給一個函數g(i)=k*i+b,求f(g(0))+f(g(1))+....+f(g(n-1)),f是斐波拉契數列。

思路:由矩陣快速冪可以求得某個斐波拉契項的值,但每項都求出來複雜度是NlogN,N<=10^9,所以不可取,由於f(k*i+b)=A^(k*i+b),A是遞推矩陣也就是[(1,1),(1,0)],提取因子就得到A^b((A^k)*0+(A^k)+(A^k)^2+...+(A^k)^(n-1)),而矩陣的等比求和可以二分遞推來求,假設當前有6個矩陣,那麼A^1+A^2+...+A^6=A^1+A^2+A^3+A^3(A^1+A^2+A^3)=(A^1+A^2+A^3)(I+A^3),可以參見matrix67的部落格,也可以看代碼。

代碼:

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include<climits>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,x,n) for(int i=x;i<(n);++i)#define FOR(i,l,h) for(int i=(l);i<=(h);++i)#define FORD(i,h,l) for(int i=(h);i>=(l);--i)#define SZ(X) ((int)(X).size())#define ALL(X) (X).begin(), (X).end()#define RI(X) scanf("%d", &(X))#define RII(X, Y) scanf("%d%d", &(X), &(Y))#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))#define DRI(X) int (X); scanf("%d", &X)#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define OI(X) printf("%d",X);#define RS(X) scanf("%s", (X))#define MS0(X) memset((X), 0, sizeof((X)))#define MS1(X) memset((X), -1, sizeof((X)))#define LEN(X) strlen(X)#define F first#define S second#define Swap(a, b) (a ^= b, b ^= a, a ^= b)#define Dpoint  strcut node{int x,y}#define cmpd int cmp(const int &a,const int &b){return a>b;} /*#ifdef HOME    freopen("in.txt","r",stdin);    #endif*/const int MOD = 1e9+7;typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;//#define HOMEint Scan(){int res = 0, ch, flag = 0;if((ch = getchar()) == '-')//判斷正負flag = 1;else if(ch >= '0' && ch <= '9')//得到完整的數res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/int k,b,n,M;struct Matrix{    int m[5][5];    void init()    {        MS0(m);    }    void print()    {        printf("%d %d\n%d %d\n",m[0][0],m[0][1],m[1][0],m[1][1]);    }};Matrix I;Matrix mul(Matrix a,Matrix b){    Matrix c;    c.init();    for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        for(int k=0;k<2;k++)    {        c.m[i][j]=(c.m[i][j]+(long long)a.m[i][k]*b.m[k][j])%M;    }    return c;}Matrix mypow(Matrix a,int k){    Matrix ans;    ans.init();    for(int i=0;i<2;i++)        ans.m[i][i]=1;    Matrix temp=a;    while(k)    {        if(k&1)            ans=mul(ans,temp);        k>>=1;        temp=mul(temp,temp);    }    return ans;}Matrix add(Matrix a,Matrix b){    Matrix ans;    ans.init();    for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        ans.m[i][j]=(a.m[i][j]+b.m[i][j])%M;    return ans;}Matrix solve(Matrix a,int k){    if(k==0)    {        Matrix b;        b.init();        return b;    }    if(k==1)        return a;    if(k&1)        return add(solve(a,k-1),mypow(a,k));    else        return mul(solve(a,k>>1),add(I,mypow(a,k>>1)));}int main(){I.init();for(int i=0;i<2;i++)    I.m[i][i]=1;while(scanf("%d%d%d%d",&k,&b,&n,&M)!=EOF){    if(n==0)    {printf("0\n");    continue;}    Matrix A;A.init();A.m[0][0]=A.m[1][0]=A.m[0][1]=1;A.m[1][1]=0;Matrix B=mypow(A,b);Matrix C=mypow(A,k);//C.print();Matrix D=solve(C,n-1);//D.print();Matrix E=add(I,D);//E.print();Matrix F=mul(B,E);//F.print();//H.print();int ans=F.m[1][0];printf("%d\n",ans);}        return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.