CodeForces 450B Jzzhu and Sequences
矩阵快速幂。
首先得到公式
然后构造矩阵,用矩阵加速
取模函数需要自己写一下,是数论中的取模。
#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的更多相关文章
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- codeforces 450B. Jzzhu and Sequences 解题报告
题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- 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 ...
随机推荐
- 字符函数库 cctype
<cctype> (ctype.h) Character handling functions This header declares a set of functions to cla ...
- C++字符串(1)
C++ 拼接字符串常量 C++允许拼接字符串字面值,即将两个用引号括起的字符串合并为一个.事实上,任何两个由空白(空格,制表符和换行符)分隔的字符串常量都将自动拼接成一个. 例子: cout < ...
- OpenLayer 3 鼠标位置坐标显示控件
<body> <div id="map"> <div id="mouse-position"></div> &l ...
- UVALive 2678 大于s的最短子序列和
input n s 10<=n<=100000,s<1e9 a1 a2 ... an ai<=10000 output 大于s的最短子序列和的长度,没有输出0 #includ ...
- HDU<1372>/bfs
题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...
- druid 文档 和 源码地址
Druid文档 :https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 maven仓库:http://c ...
- 第一个python实例程序
#!/usr/bin/python2.7 import os ls = os.linesep fname = raw_input("fname:"); while True: if ...
- Hibernate锁机制
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...
- MaterialWidgetLibrary 学习
studio项目地址:https://github.com/keithellis/MaterialWidget 修改后的eclipse项目地址: 修改后的eclipse项目 Demo地址: activ ...
- wamp开机自动启动
控制面板--管理工具--服务 将其中的wampapache和wampmysql的属性均改为“自动”即可.