Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

题意:

给定f1和f2,求fn

分析:

特判f1,f2

当n>=3时使用矩阵快速幂即可( 简单题 )

将公式转化一下 , 可以得到一个变换矩阵

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR( a, b )    memset( a, b, sizeof(a) )
#define MAT_SIZE 2
#define MOD 1000000007
typedef long long LL;
struct Mat
{
    LL mat[MAT_SIZE][MAT_SIZE];
    void zero()
    {
        for( int i = 0; i < MAT_SIZE; ++i )
            for( int j = 0; j < MAT_SIZE; ++j )
                mat[i][j] = 0;
    }
    void init()
    {
        for( int i = 0; i < MAT_SIZE; ++i )
            for( int j = 0; j < MAT_SIZE; ++j )
                mat[i][j] = ( i == j );
    }
    void setv( int v )
    {
        for( int i = 0; i < MAT_SIZE; ++i )
            for( int j = 0; j < MAT_SIZE; ++j )
                mat[i][j] = v;
    }
    Mat operator*( const Mat &b)const
    {
        Mat c;
        c.zero();
        for( int i =0; i < MAT_SIZE; ++i )
            for( int j =0; j < MAT_SIZE; ++j )
                for( int k = 0; k < MAT_SIZE; ++k )
                    c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;
        return c;
    }
};

Mat fast_mod( Mat a, int b )
{
    Mat res;
    res.init();
    while( b )
    {
        if( b & 1 ) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

void Orz()
{
    int n;
    LL x, y;
    while( ~scanf( "%lld %lld %d", &x, &y, &n ) )
    {
        if( n == 1 )    printf( "%lld\n", ( x % MOD + MOD ) % MOD );
        else if( n == 2 )    printf( "%lld\n", ( y % MOD + MOD ) % MOD );
        else
        {
            Mat t;
            t.mat[0][0] = 0;
            t.mat[0][1] = 1;
            t.mat[1][0] = -1;
            t.mat[1][1] = 1;
            t = fast_mod( t, n-2 );
            LL res = ( ( t.mat[1][0] * x + t.mat[1][1] * y ) % MOD + MOD ) % MOD;
            printf( "%lld\n", res );
        }
    }
}

int main()
{
    Orz();
    return 0;
}

代码君

时间: 2024-08-02 21:34:44

Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )的相关文章

Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo 1000000007 (109 + 7). Input The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single i

Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. 1 /* 2 | 1, -1 | | fn | 3 | 1, 0 | | fn-1 | 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 typedef __int64 LL; 10 LL mod =

Jzzhu and Sequences (矩阵快速幂 + 取模)

题目链接 Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo 1000000007 (109?+?7). Input The first line contains two integers x and y (|x|,?|y|?≤?109). The second line contains a sin

【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)

[题目链接]click here~~ [题目大意] Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo1000000007(109?+?7). [解题思路] /*A - Jzzhu and Sequences Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

codeforces 678D Iterated Linear Function 矩阵快速幂

矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵  { g[n] }    =  {A   B}  * { g[n-1]} {   1   }         {0   1}     {    1    } 然后矩阵快速幂就好 矩阵快速幂的题要多做,多构造矩阵 注:其实这个题可以直接等比数列求求和,单数矩阵快速幂对于这类题更具有普遍性 #include <cstdio> #include <iostream> #include <ctime>

CodeForces 450B Jzzhu and Sequences

矩阵快速幂. 首先得到公式 然后构造矩阵,用矩阵加速 取模函数需要自己写一下,是数论中的取模. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long MOD = 1e9 + 7; long long x, y; int n; long long mod(long l

CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 const int M = 1e5+5; 8 const int

Codeforces Round #257 (Div. 2)B 矩阵快速幂

B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn m

CodeForces 185A. Plant(矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/185/A Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides int