zoukankan      html  css  js  c++  java
  • hdu 1005 Number Sequence

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1005  

    Number Sequence

    Description

    A number sequence is defined as follows:

    f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

    Given A, B, and n, you are to calculate the value of f(n).

    Input

    The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

    Output

    For each test case, print the value of f(n) on a single line.

    Sample Input

    1 1 3
    1 2 10
    0 0 0

    Sample Output

    2
    5

    矩阵快速幂。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<map>
    using std::map;
    using std::min;
    using std::find;
    using std::pair;
    using std::vector;
    using std::multimap;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int M = 7;
    const int N = 100001;
    const int INF = 0x3f3f3f3f;
    struct Matrix {
        typedef vector<int> vec;
        typedef vector<vec> mat;
        inline mat mul(mat &A, mat &B) {
            mat C(sz(A), vec(sz(B[0])));
            rep(i, sz(A)) {
                rep(k, sz(B)) {
                    rep(j, sz(B[0])) {
                        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
                    }
                }
            }
            return C;
        }
        inline mat pow(mat &A, int n) {
            mat ret(sz(A), vec(sz(A[0])));
            rep(i, sz(A)) ret[i][i] = 1;
            while(n) {
                if(n & 1) ret = mul(ret, A);
                A = mul(A, A);
                n >>= 1;
            }
            return ret;
        }
        inline void solve(int a, int b, int n) {
            mat ans(2, vec(2));
            ans[0][0] = a, ans[0][1] = b;
            ans[1][0] = 1, ans[1][1] = 0;
            ans = pow(ans, n - 2);
            printf("%d
    ", (ans[0][0] + ans[0][1]) % M);
        }
    }go;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int a, b, n;
        while(~scanf("%d %d %d", &a, &b, &n), a + b + n) {
            if(1 == n || 2 == n) { puts("1"); continue; }
            go.solve(a, b, n);
        }
        return 0;
    }
  • 相关阅读:
    转 UICollectionView 详解
    springboot配置ssl证书
    服务器ganglia安装(带有登录验证)
    eureka配置说明
    Servlet中获取请求参数问题
    apidoc学习(接口文档定义取代word)
    markdown语法
    JVM分析
    ftp上传或下载文件工具类
    ubuntu命令安装
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4792580.html
Copyright © 2011-2022 走看看