zoukankan      html  css  js  c++  java
  • Codeforces450 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 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).

    #include <iostream>
    #include <cstring>
    using namespace std;
    /*矩阵快速幂*/
    #define mod 1000000007
    #define ll long long int
    #define sec 2
    struct mat
    {
        ll arr[sec][sec];
    };
    mat mul(mat a, mat b)
    {
        mat res;
        memset(res.arr, 0, sizeof(res.arr));
        for (int i = 0; i < sec; i++)
        for (int j = 0; j < sec; j++)
        for (int k = 0; k < sec; k++)
        {
            res.arr[i][j] = ((res.arr[i][j] + a.arr[i][k] * b.arr[k][j]) % mod + mod) % mod;
        }
        return res;
    }
    mat expo(mat ori, int n)
    {
        mat res;
        memset(res.arr, 0, sizeof(res.arr));
        for (int i = 0; i < sec; i++)
            res.arr[i][i] = 1;
        while (n)
        {
            if (n & 1)
                res = mul(res, ori);
            ori = mul(ori, ori);
            n >>= 1;
        }
        return res;
    }
    int main()
    {
        int x, y, n;
        while (cin >> x >> y >> n)
        {
            x = (x + mod) % mod;
            y = (y + mod) % mod;
            if (n == 1)
                cout << x << endl;
            else
            if (n == 2)
                cout << y << endl;
            else
            {
                mat a;
                a.arr[0][0] = a.arr[1][0] = 1;
                a.arr[0][1] = -1;
                a.arr[1][1] = 0;
                a = expo(a, n - 2);
                cout << ((a.arr[0][0] * y) % mod + (a.arr[0][1] * x) % mod) % mod << endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    现存的数据库的解决方案的原理解析
    HTTP代理实现请求报文的拦截与篡改
    ucos软件结构
    微软官方Windows 8设计
    asp.net对CNMARC
    jQuery弹出层插件
    从"Hello World!"到面向对象
    git 的使用命令:克隆
    第一个C程序
    云计算平台简介(App Engine)
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3897436.html
Copyright © 2011-2022 走看看