5950 Recursive sequence (矩陣快速冪)

來源:互聯網
上載者:User

標籤:快速冪   遞推公式   scanf   targe   get   line   long   target   inline   

題意:遞推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,讓求 Fn;

析:很明顯的矩陣快速冪,因為這個很像Fibonacci數列,所以我們考慮是矩陣,然後我們進行推公式,因為這樣我們是無法進行運算的。好像有的思路,最後也沒想出來,還是參考的大牛的部落格

http://blog.csdn.net/spring371327/article/details/52973534

那是講的很詳細了,就不多說了,注意這個模數不是1e9+7,一開始忘了。。

代碼如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <string>#include <cstdlib>#include <cmath>#include <iostream>#include <cstring>#include <set>#include <queue>#include <algorithm>#include <vector>#include <map>#include <cctype>#include <cmath>#include <stack>#define debug puts("+++++")//#include <tr1/unordered_map>#define freopenr freopen("in.txt", "r", stdin)#define freopenw freopen("out.txt", "w", stdout)using namespace std;//using namespace std :: tr1;typedef long long LL;typedef pair<int, int> P;const int INF = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;const LL LNF = 0x3f3f3f3f3f3f;const double PI = acos(-1.0);const double eps = 1e-8;const int maxn = 1e5 + 5;const LL mod = 2147493647;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }inline int lcm(int a, int b){  return a * b / gcd(a, b); }int n, m;const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};inline int Min(int a, int b){ return a < b ? a : b; }inline int Max(int a, int b){ return a > b ? a : b; }inline LL Min(LL a, LL b){ return a < b ? a : b; }inline LL Max(LL a, LL b){ return a > b ? a : b; }inline bool is_in(int r, int c){    return r >= 0 && r < n && c >= 0 && c < m;}struct Matrix{    LL a[7][7];    Matrix operator * (const Matrix &p){        Matrix res;        for(int i = 0; i < 7; ++i)            for(int j = 0; j < 7; ++j){                res.a[i][j] = 0;                for(int k = 0; k < 7; ++k)                    res.a[i][j] = (res.a[i][j] + a[i][k] * p.a[k][j]) % mod;            }        return res;    }};Matrix quick_pow(Matrix b, LL n){    Matrix res;    memset(res.a, 0, sizeof res.a);    for(int i = 0; i < 7; ++i)  res.a[i][i] = 1;    while(n){        if(n & 1)  res = res * b;        b = b * b;        n >>= 1;    }    return res;}int main(){    Matrix x;    memset(x.a, 0, sizeof x.a);    x.a[0][0] = 1;  x.a[0][1] = 2; x.a[0][2] = 1; x.a[0][3] = 4;  x.a[0][4] = 6;    x.a[0][5] = 4;  x.a[0][6] = 1; x.a[1][0] = 1; x.a[2][2] = 1;  x.a[2][3] = 4;    x.a[2][4] = 6;  x.a[2][5] = 4; x.a[2][6] = 1; x.a[3][3] = 1;  x.a[3][4] = 3;    x.a[3][5] = 3;  x.a[3][6] = 1; x.a[4][4] = 1; x.a[4][5] = 2;  x.a[4][6] = 1;    x.a[5][5] = 1;  x.a[5][6] = 1; x.a[6][6] = 1;    int T;  cin >> T;    while(T--){        LL n, a, b;        scanf("%I64d %I64d %I64d", &n, &a, &b);        if(1 == n)  printf("%I64d\n", a);        else if(2 == n)  printf("%I64d\n", b);        else{            Matrix res = quick_pow(x, n-2);            LL ans = 0;            ans = (ans + res.a[0][0] * b) % mod;            ans = (ans + res.a[0][1] * a) % mod;            ans = (ans + res.a[0][2] * 16) % mod;            ans = (ans + res.a[0][3] * 8) % mod;            ans = (ans + res.a[0][4] * 4) % mod;            ans = (ans + res.a[0][5] * 2) % mod;            ans = (ans + res.a[0][6]) % mod;            printf("%I64d\n", ans);        }    }    return 0;}

 

5950 Recursive sequence (矩陣快速冪)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.