【Codeforces】450 B(div2)
题目链接:http://codeforces.com/problemset/problem/450/B
题意:

求这个的第n项。
题解:$f_{i+1} = f_i - f_{i-1} $
\begin{pmatrix} 1 & 1\\ -1 & 0 \end{pmatrix} *
\begin{pmatrix} f(n)& f(n-1) \end{pmatrix} =
\begin{pmatrix} f(n) - f(n-1) & f(n) \end{pmatrix}=
\begin{pmatrix} f(n+1) & f(n) \end{pmatrix}
代入前两项即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = ;
const ll mod = 1e9+; ll n,p; struct Matrix{
ll a[maxn][maxn];
void init(){
memset(a, , sizeof(a));
for(int i = ; i <= maxn;i++){
a[i][i] = ;
}
}
}; //矩阵乘法
Matrix mul(Matrix a, Matrix b){
Matrix ans;
for(int i = ;i <= ;++i){
for(int j = ;j <= ;++j){
ans.a[i][j] = ;
for(int k = ;k <= ;++k){
ans.a[i][j] = ans.a[i][j] % mod + a.a[i][k] * b.a[k][j] % mod;
ans.a[i][j] %= mod;
}
}
}
return ans;
} //矩阵快速幂
Matrix qpow(Matrix a,ll b){
Matrix ans;
ans.init();
while(b){
if(b & )
ans = mul(ans,a);
a = mul(a,a);
b >>= ;
}
return ans;
} void print(Matrix a){
for(int i = ; i <= n;++i){
for(int j = ;j <= n;++j){
cout << a.a[i][j]%mod<< " ";
}
cout << endl;
}
} int main(){
Matrix base;
Matrix ans;
int x,y,n;
cin>>x>>y>>n;
if(n == ){
cout<<(mod + x) % mod<<endl;
return ;
}
if( n == ){
cout<<(mod + y) % mod<<endl;
return ;
} ans.a[][] = y;ans.a[][] = x;
base.a[][] = ;base.a[][] = ;
base.a[][] = -;base.a[][] = ; ans = mul(ans,qpow(base,n-));
ll res = (mod + ans.a[][]) % mod;
cout<<res<<endl;
return ;
}
【Codeforces】450 B(div2)的更多相关文章
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【Codeforces Round #450 (Div. 2) C】Remove Extra One
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举删除第i个数字. 想想删掉这个数字后会有什么影响? 首先,如果a[i]如果是a[1..i]中最大的数字 那么record会减少1 ...
- 【Codeforces Round #450 (Div. 2) B】Position in Fraction
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找循环节就好. ->其实可以不用找出来整个循环节. 有找到c就直接输出. 找到了循环节还没找到的话,直接输出无解. [代码] ...
- 【Codeforces Round #450 (Div. 2) A】Find Extra One
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 看看Y左边或右边的点个数是否<=1 [代码] #include <bits/stdc++.h> using ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))
[题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
随机推荐
- prim算法【最小生成树1】
适用范围:要求无向图 prim算法(读者可以将其读作“普里姆算法”)用来解决最小生成树问题, 其基本思想是: ·对图G(VE)设置集合S,存放已被访问的顶点, ·然后每次从集合V-S中选择与集合S的最 ...
- Vue手把手教你撸一个 beforeEnter 钩子函数
地址 :https://www.jb51.net/article/138821.htm 地址 :https://www.jb51.net/article/108964.htm
- 初探Remoting双向通信(三)
原 初探Remoting双向通信(三) 2013年06月25日 17:51:08 喜欢特别冷的冬天下着雪 阅读数 4741 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- Linux安装配置Nginx服务器
如有需要可以加我Q群[308742428]大家一起讨论技术,有偿服务. 后面会不定时为大家更新文章,敬请期待. 喜欢的朋友可以关注下. 前言 今天搭建nginx服务器,来访问静态资源文件. Nginx ...
- 算法竞赛模板 动态规划之背包DP
① 01背包 有n件物品和一个容量为v的背包.第i件物品的价值是c[i],体积是w[i].求解将哪些物品装入背包可使价值总和最大. 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. ...
- JNI intArray
JNIDemo.java public class JNIDemo { static { /* 1. load */ System.loadLibrary("native"); / ...
- batch normlization (BN)的讲解
1. https://zhuanlan.zhihu.com/p/54073204(简单理解) 2. https://zhuanlan.zhihu.com/p/34879333 (有举例说明,但是不太理 ...
- View Triggers Function Procedure
check view total number select count(VIEW_NAME) from USER_VIEWS; check trigger total number select C ...
- python re.I compile search
import restring = "The quick brown fox jumps over the lazy dog."a_list = string.split()pat ...
- js-打印九九乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...