【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\). [算法]树上倍增+二维数点(树状数组 ...
随机推荐
- 关于char(M)和varchar(N)的区别
1.int(10)的10表示显示的数据的长度,不是存储数据的大小:chart(10)和varchar(10)的10表示存储数据的大小,即表示存储多少个字符. int(10) 10位的数据长度 9999 ...
- Linux部分常用命令详解(一)
echo 命令详解 格式: echo string 显示普通字符: echo "it is a test" 或者 echo it is a test 显示转义字符: echo &q ...
- CF322F
CF322F 拉格朗日插值 #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- java虚拟机规范(se8)——java虚拟机的编译(一)
本文翻译自:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html 第三章 java虚拟机的编译 java虚拟机是设计用来支持ja ...
- sublime推荐插件
SyncedSidebarBg:侧边栏底色统一 Emmet:集合多种功能,大名鼎鼎的 Zen coding ==> 不过对于嵌入式的我没多大用啊 Sublime CodeIntel:代码提示 A ...
- sudo 出现unable to resolve host hostname 解决方法
Linux 环境,我的电脑叫枝桠(机器的hostname), 每次执行sudo 就出现这个警告讯息: sudo: unable to resolve host 枝桠 直接修改 /etc/hosts 的 ...
- C语言交换两个数的值
#include<stdio.h> int main() { //交换两个数的值 // 方法一 可读性最好 ; ; int temp ; temp = a; a = b; b = temp ...
- aiohttp上报405: Method Not Allowed
请求方式不对,修改为“POST”或者“GET” 可参考:https://blog.csdn.net/yiifaa/article/details/80928487
- myeclipse中出现The method xxx of type must override or implement a supertype
出现问题提示:The method xxx of type must override or implement a supertype? annotation:@Override的原因 查阅了一下资 ...
- hbuilder 配置app为沉浸式状态栏