zoukankan      html  css  js  c++  java
  • 斐波那契数列的第N项

     1 #include <bits/stdc++.h>
     2 #define _xx ios_base::sync_with_stdio(0);cin.tie(0);
     3 using namespace std;
     4 typedef long long ll;
     5 const ll p = 1000000009ll;
     6 struct node
     7 {
     8     ll data[2][2];
     9 };
    10 node operator * (const node& t1, const node& t2)
    11 {
    12     node ans;
    13     for(int i = 0; i < 2; i++)
    14     {
    15         for(int j = 0; j < 2; j++)
    16         {
    17             ans.data[i][j] = 0;
    18             for(int k = 0; k < 2; k++)
    19                 ans.data[i][j] += t1.data[i][k]*t2.data[k][j];
    20                 ans.data[i][j] %= p;
    21         }
    22     }
    23     return ans;
    24 }
    25 node operator % (const node& t1, const ll& p)
    26 {
    27     node ans;
    28     for(int i = 0; i < 2; i++)
    29         for(int j = 0; j < 2; j++)
    30             ans.data[i][j] = t1.data[i][j]%p;
    31     return ans;
    32 }
    33 node fastm(node a, ll b)
    34 {
    35     node ans;
    36     for(int i = 0; i < 2; i++)
    37         for(int j = 0; j < 2; j++)
    38             if(i == j) ans.data[i][j] = 1;
    39             else ans.data[i][j] = 0;
    40     while(b)
    41     {
    42         if(b%2 == 1)
    43         {
    44             ans = ans*a;
    45             ans = ans%p;
    46         }
    47         a = a*a;
    48         a = a%p;
    49         b /= 2;
    50     }
    51     return ans;
    52 }
    53 int main()
    54 {_xx
    55     ll n;
    56     cin >> n;
    57     if(n == 0) cout << 0 << endl;
    58     else if(n == 1) cout << 1 << endl;
    59     else
    60     {
    61         node a, b;
    62         for(int i = 0; i < 2; i++)
    63             for(int j = 0; j < 2; j++)
    64                 b.data[i][j] = a.data[i][j] = 0;
    65         a.data[1][0] = a.data[2][0] = 1;
    66         b.data[0][1] = b.data[1][0] = b.data[1][1] = 1;
    67         b = fastm(b, n - 1);
    68         a = b*a;
    69         cout << a.data[1][0]%p << endl;
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    SQLyog快捷键
    程序员常用工具下载地址及方式
    mysql基础命令
    Excel常用快捷键
    PS快捷键
    MySQL安装及配置
    Excel常用公式
    数据库函数
    mysql update语句的用法
    MySQL使用存储过程批量更新数据库所有表某个字段值
  • 原文地址:https://www.cnblogs.com/NWUACM/p/6640824.html
Copyright © 2011-2022 走看看