1. 程式人生 > >hdu 1005Number Sequence (矩陣快速冪)

hdu 1005Number Sequence (矩陣快速冪)

F(n)                                 a                     b                                                   F(n-1)

                              =                                                            *                            

F(n-1 )                            1                   0                                                    F(n-2)

矩陣快速冪  (跟著大師學矩陣快速冪   )

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct Matrix
{
    int m[2][2];
}E,A;
void init()
{
    for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
    E.m[i][j]=(i==j);
}
Matrix Mut(Matrix A,Matrix B,int mod)
{
    Matrix ans;
    int i,j,k;
    for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    {
        ans.m[i][j]=0;
        for(k=0;k<2;k++)
        ans.m[i][j]=(ans.m[i][j]+(A.m[i][k]%mod)*(B.m[k][j]%mod))%mod;
    }
    return ans;
}
Matrix quick(Matrix A,int n,int mod)
{
    Matrix t=A,d=E;
    while(n)
    {
        if(n%2) d=Mut(d,t,mod);
        n/=2;
        t=Mut(t,t,mod);
    }
    return d;
}
int main()
{
    init();
    int a,b,n;
    while(scanf("%d%d%d",&a,&b,&n)!=EOF)
    {
        if(a==0&&b==0&&n==0)
        break;
        if(n==1||n==2)
        {
            printf("1\n");
        }
        else
        {
            A.m[0][0]=a;
            A.m[0][1]=b;
            A.m[1][0]=1;
            A.m[1][1]=0;
            Matrix ans=quick(A,n-2,7);
            printf("%d\n",(ans.m[0][0]+ans.m[0][1])%7);
        }
    }
    return 0;
}


相關推薦

hdu 1005Number Sequence 矩陣快速

F(n)                                 a                     b                                                   F(n-1)                    

HDU 5667 Sequence矩陣快速

Problem Description Holion August will eat every thing he has found. Now there are many foods,b

HDU5950 C - Recursive sequence矩陣快速

題目連結:傳送門 題目: Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4173

Recursive sequence矩陣快速

題目大意: f(n+1)=2*f(n-1)+f(n)+(n+1)^4 f(1)=a;f(2)=b; 求第n項。要mod 思路:裸的。。。構造矩陣 附上隊友醜陋優秀的程式碼: #include<bits/stdc++.h> ///#define mo

P5136 sequence矩陣快速

傳送門 數列的特徵方程和特徵根老師上課好像講過然而我沒聽……以後老師上數學課要認真聽了QAQ 設\(x=\frac{1+\sqrt{5}}{2},y=\frac{1-\sqrt{5}}{2}\),那麼\(x,y\)是\(t^2=t+1\)的兩個解,也就是數列\(F_n=F_{n-1}+F_{n-2}\)的

HDU 2604 Queuing 矩陣快速

/* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: 2604.cpp * Create Date: 2014-08-02 21:20

hdu 5950 Recursive sequence矩陣快速,構造

N較大,直接遞推會超時,可以用矩陣快速冪 也是的函式,且 所以在構造的矩陣中維護到 #include <iostream> #include <cstdio> #include <algorithm> #include &

hdu 4965 Fast Matrix Calculation矩陣快速

觀察 while code 開始 mat col power tmp style 題意: 給你一個N*K的矩陣A和一個K*N的矩陣B,設矩陣C=AB,M=C^(N*N),矩陣Mmod6後,所有數的和是多少 思路: 剛開始我是直接計算的

第十場 hdu 6172 Array Challenge矩陣快速

不知道 log tar 4.6 width += arr open ret http://acm.hdu.edu.cn/showproblem.php?pid=6172 題目大意:按照給出的公式算出an 解題思路:an=4an-1+17an-2-12an-3,不要問

hdu 6198矩陣快速

text cto http tdi mis nbsp style hdu mil number number number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav

HDU 4549 M斐波那契數列矩陣快速

題目連結:M斐波那契數列 列舉幾項會發現$ F[n]=a^{fib(n-1)} * b^{fib(n)} $ 斐波那契數列用矩陣快速冪求即可。但是因為n很大,fib會爆掉。這時候可以引入費馬小定理。 證明:$a^x \% p = a^{x \%(p-1)} \%p$ 1.$a^x \% p = a^{

hdu 1005矩陣快速

題目傳送門 題目描述的是一個遞推,並且沒有其他變數與n有關,是一個典型的矩陣快速冪模板題(有多種解法,只提這一種)。 {1,1} * A{A,1}   = {f(n),f(n-1)}              {B,0} /** 矩陣快速冪 模板 **/ #incl

HDU 4686 Arc of Dream 矩陣快速

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

hdu 2157 矩陣快速

把給定的圖轉為鄰接矩陣,即A(i,j)=1當且僅當存在一條邊i->j。令C=A*A,那麼C(i,j)=ΣA(i,k)*A(k,j),實際上就等於從點i到點j恰好經過2條邊的路徑數(列舉k為中轉點)。類似地,C*A的第i行第j列就表示從i到j經過3條邊的路徑數。 通過

hdu 3509矩陣快速

題意:按照所給的最後一個公式推導,然後矩陣快速冪 把圖中的矩陣最上面的0改成1,最後的f(n-2)轉換為f(n-1) 然後就矩陣快速冪就行了 #include <set> #include <map> #include <stack>

poj 3735 Training little cats 矩陣快速

log ack make .cn code little logs 矩陣快速冪 style 題目鏈接: http://poj.org/problem?id=3735 題意: 有n只貓咪,開始時每只貓咪有花生0顆,現有一組操作,由下面三個中的k個操作組成:

[luoguP1962] 斐波那契數列矩陣快速

truct ons 技術 pan opera http 快速冪 printf ble 傳送門 解析詳見julao博客連接 http://worldframe.top/2017/05/10/清單-數學方法-——-矩陣/ —&

hdu4565 So Easy!矩陣快速

利用 ace namespace ret bsp cst () easy for 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 題解:(a+√b)^n=xn+yn*√b,(a-&rad

P3216 [HNOI2011]數學作業 矩陣快速

接下來 hnoi2011 輸入 matrix spl play clu 快速冪 define P2774 方格取數問題 題目背景 none! 題目描述 在一個有 m*n 個方格的棋盤中,每個方格中有一個正整數。現要從方格中取數,使任意 2 個數所在方格沒有公共邊,且取

食物矩陣快速DP

技術 img ring 矩陣快速冪 快速 我們 long tps ret 這個題。。我們可以想到用遞推寫!!qwq(好吧,其實我的DP水平不高啊qwq) 然後我們看到數據範圍。。。好大呀qwq線性算法肯定會T啊qwq,那。。。。寫矩陣加速吧!qwq #include<