zoukankan      html  css  js  c++  java
  • Codeforces Round #118 (Div. 1) A 矩阵快速幂

    A. Plant
     

    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 into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.

    Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.

    Input

    The first line contains a single integer n (0 ≤ n ≤ 1018) — the number of full years when the plant grew.

    Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64dspecifier.

    Output

    Print a single integer — the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).

    Examples
    input
    Copy
    1
    output
    Copy
    3
    input
    Copy
    2
    output
    Copy
    10
    Note

    The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.

    一个向上的三角形会变成三个向上的三角形和一个向下的三角形。

    一个向下的三角形会变成三个向下的三角形和一个向上的三角形。

    设当前有Xn个向上的三角形,Yn个向下的三角形。

    那个优

    Xn      3     1   Xn-1 

        =

    Yn          1      3     Yn-1

    有公式就好写代码了。

     1 // an = 2*a(n-1)+4^(n-1)
     2 // 设向上的有Xn,向下有Yn
     3 // 那么有:
     4 // Xn = 3*X(n-1) + Y(n-1)
     5 // Yn = X(n-1) + 3*Y(n-1)
     6 #include <bits/stdc++.h>
     7 #define ll long long
     8 using namespace std;
     9 const ll mod = 1000000007;
    10 struct mat{
    11     ll m[2][2];
    12     mat(){
    13         memset(m, 0, sizeof(m));
    14     }
    15 };
    16 
    17 mat mul(mat &A, mat &B) {
    18     mat C;
    19     for(int i = 0; i < 2; i++) {
    20         for(int j = 0; j < 2; j ++) {
    21             for(int k = 0; k < 2; k ++) {
    22                 C.m[i][j] = (C.m[i][j] + A.m[i][k]*B.m[k][j]) % mod;
    23             }
    24         }
    25     }
    26     return C;
    27 }
    28 
    29 mat pow(mat A, ll n) {
    30     mat B;
    31     for(int i = 0; i < 2; i ++) B.m[i][i] = 1;
    32     while(n) {
    33         if(n&1) B = mul(B, A);
    34         A = mul(A, A);
    35         n >>= 1;
    36     }
    37     return B;
    38 }
    39 int main() {
    40     ll n;
    41     cin >> n;
    42     mat A;
    43     A.m[0][0] = A.m[1][1] = 3;
    44     A.m[0][1] = A.m[1][0] = 1;
    45     A = pow(A, n);
    46     cout << A.m[0][0]<< endl;
    47     return 0;
    48 }
  • 相关阅读:
    Orient DB 0.9.6 Beta 发布下载
    Java 近些年一直群龙无首 Google 为 Java 前途担忧
    VC和LUA混合开发之VC程序调用Lua脚本函数
    NetBeans 时事通讯(刊号 # 99 Apr 16, 2010)
    Cassandra 0.6 发布下载
    JDK 6 Update 20 发布下载JDK 6最新版本下载
    Java 近些年一直群龙无首 Google 为 Java 前途担忧
    Simple way to export SQL Server data to Text Files
    GFORTRAN
    The Stanford NLP (Natural Language Processing) Group
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9000087.html
Copyright © 2011-2022 走看看