矩阵快速幂。

首先得到公式

然后构造矩阵,用矩阵加速

取模函数需要自己写一下,是数论中的取模。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; long long MOD = 1e9 + ;
long long x, y;
int n; long long mod(long long a, long long b)
{
if (a >= ) return a%b;
if (abs(a) % b == ) return ;
return (a + b*(abs(a) / b + ));
} struct Matrix
{
long long A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= C; j++)
for (k = ; k <= C; k++)
c.A[i][j] = mod((c.A[i][j] + mod(A[i][k] * b.A[k][j], MOD)), MOD);
c.R=R; c.C=b.C;
return c;
} void read()
{
scanf("%lld%lld%d", &x, &y, &n);
} void init()
{
n = n - ;
Z.A[][] = x, Z.A[][] = y; Z.R = ; Z.C = ;
Y.A[][] = , Y.A[][] = , Y.A[][] = , Y.A[][] = ; Y.R = ; Y.C = ;
X.A[][] = , X.A[][] = -, X.A[][] = , X.A[][] = ; X.R = ; X.C = ;
} void work()
{
while (n)
{
if (n % == ) Y = Y*X;
n = n >> ;
X = X*X;
}
Z = Z*Y; printf("%lld\n", mod(Z.A[][], MOD));
} int main()
{
read();
init();
work();
return ;
}

CodeForces 450B Jzzhu and Sequences的更多相关文章

  1. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  2. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  3. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  4. codeforces 450B. Jzzhu and Sequences 解题报告

    题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...

  5. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  6. CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解

    思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...

  7. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  8. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  9. Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. python2.x 使用protobuf

    1.在windows下配置protobuf 1.1 下载对应的包,包含两个:protoc.exe和源码文件(protoc也可以自己生成) 下载地址1 --- google code最近在迁移,也许以后 ...

  2. 关于C++ const

    1.Const用途 No. 用途 使用范围 参考代码 1 类型检查 参数传递 void func(const int i){ ... } 2 节省空间,避免不必要的内存分配 代替#define #de ...

  3. SQL Server 2008 2005删除或压缩数据库日志的方法

    由于数据库日志增长被设置为“无限制”,所以时间一长日志文件必然会很大,一个400G的数据库居然有600G的LOG文件,严重占用了磁盘空间.由于主要是做OLAP,所以数据库本身不会有大变动,所以日志也就 ...

  4. runtime基础

    前言 学习Objective-C的运行时Runtime系统是很有必要的.个人觉得,得之可得天下,失之则失天下. Objective-C提供了编译运行时,只要有可能,它都可以动态地运作.这意味着不仅需要 ...

  5. 转换流--InputStreamReader ,OutputStreamWriter

    BufferedReader 字符流,只能装饰字符流,不能装饰字节流.有个重要且好用的方法是readLine()整行读取字符. 但是InputStream是字节流,没有resdLine方法,那么字节流 ...

  6. jsp base标签与meta标签学习小结

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  7. php 生成 验证码的例子

    /** +---------------------------------------------------------- * 生成随机字符串  CuPlayer.com 酷播 +-------- ...

  8. BCDBOOT命令参数介绍

    BCDboot 命令行选项 更新时间: 2013年10月 应用到: Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R ...

  9. js滚动条

    /*滚动条在Y轴上的滚动距离*/function ScrollTop(){ var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0; i ...

  10. hashmap源码

    Java里各种基础容器的实现都是链表外加引用的形式.So,hashmap也不例外. 那小考虑下:hashmap是怎么产生的呢? 常用的两种数据结构数组和链表各有优劣,数组寻址容易,插入和删除困难:而链 ...