zoukankan      html  css  js  c++  java
  • CodeForces 450B

    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 modulo 1000000007 (109 + 7).

    Input

    The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).

    Output

    Output a single integer representing fn modulo 1000000007 (109 + 7).

    Sample test(s)
    Input
    2 3
    3
    Output
    1
    Input
    0 -1
    2
    Output
    1000000006
    Note

    In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.

    In the second sample, f2 =  - 1;  - 1 modulo (109 + 7) equals (109 + 6).

    /**
              题意:f[i] = f[i-1] + f[i+1]
              做法:矩阵  如题要求建一个二维矩阵,
                        0   1
                       -1   0
                       然后求解
    **/
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define SIZE 2
    #define MOD 1000000007
    #define clr( a, b ) memset( a, b, sizeof(a) )
    typedef long long LL;
    
    struct Mat
    {
        LL mat[ SIZE ][ SIZE ];
        int n;
        Mat( int _n )
        {
            n = _n;
            clr( mat, 0 );
        }
        void init()
        {
            for( int i = 0; i < n; ++i )
                for( int j = 0; j < n; ++j )
                    mat[i][j] = ( i == j );
        }
        Mat operator * ( const Mat &b ) const
        {
            Mat c( b.n );
            for( int k = 0; k < n; ++k )
                for( int i = 0; i < n; ++i )    if( mat[i][k] )
                        for( int j = 0; j < n; ++j )
                            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( a.n );
        res.init();
        while( b )
        {
            if( b & 1 ) res = res * a;
            a = a * a;
            b >>= 1;
        }
        return res;
    }
    
    
    int main()
    {
        LL x, y, n, res;
        scanf( "%lld %lld %lld", &x, &y, &n );
        if( n == 1 )
        {
            printf( "%lld
    ", ( x % MOD + MOD ) % MOD );
        }
        else if( n == 2 )
        {
            printf( "%lld
    ", ( y % MOD + MOD ) % MOD );
        }
        else
        {
            n -= 2;
            Mat C( 2 );
            C.mat[0][0] = 0;
            C.mat[0][1] = 1;
            C.mat[1][0] = -1;
            C.mat[1][1] = 1;
            C = fast_mod( C, n );
            res = ( ( x * C.mat[1][0] + y * C.mat[1][1] )% MOD + MOD ) % MOD;
            printf( "%lld
    ", res );
        }
        return 0;
    }
  • 相关阅读:
    Liunx之django项目部署
    Liunx之nginx配置
    Liunx之基础学习
    Linux之防火墙【CentOS 7】
    Linux之各程序安装
    Linux之基础命令
    攻城狮必备神注释
    Django-rbac权限
    "/var/lib/mysql/mysql.sock"不存在解决办法
    72张三国历史演变地图
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4521473.html
Copyright © 2011-2022 走看看