【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\). [算法]树上倍增+二维数点(树状数组 ...
随机推荐
- Linux 常用指令总结
一. 与时间有关的参数: 1.find 基本语法参数如下: find [PATH] [option] [action] -mtime n : n为数字,意思为在n天之前的“一天内”被更改过的文件: - ...
- elementUI 的el-pagination 分页功能
<div class="block1"> <el-pagination @size-change="handleSizeChange" @cu ...
- 粗糙的区别prepareStatement:(为Statement的子类)与Statement
区别: prepareStatement:(为Statement的子类) conn = DBFactory.getInstance().getImpl().getConnection(); //方式一 ...
- 四、hibernate的缓存
缓存: 就是将数据保存到内存中,需要使用时直接从内存中获取,不需要每次查询数据库或者磁盘中的文件 hibernate的缓存 一级缓存:Session级别的缓存 二级缓存:SessionFactory级 ...
- redis 发布订阅(pub/sub )
- strxfrm - 转换字符串
总览 (SYNOPSIS) #include <string.h> size_t strxfrm(char *dest, const char *src, size_t n); 描述 (D ...
- JSON.parse 解析json字符串时,遇字符串换行符,解析失败
今天遇到json字符串转对象时报错了,发现有个字符串有换行符,仔细找了原因. 结果是因为JSON.parse转json字符串时遇到一些特殊字符需要先转义,如图所示 然后尝试了各路大神介绍的办法,均不适 ...
- vue项目从0开始记录
1.安装vue-cli 2.通过脚手架进行项目的创建 4.配置第三方UI库快速开发(如ivew,element ui) 5.配置axios 库 一.安装vue-cli npm install - ...
- JavaIO流之File操作
IO流: File: File(文件/文件夹地址)构造函数: public File(String path); public File(String parentPath, String child ...
- go结构体上的函数
go结构体上的函数 我们可以将一个方法和一个结构体关联: type Saiyan struct { Name string Power int } func (s *Saiyan) Super() { ...