[51nod 1126] 求递推序列的第N项 - 矩阵乘法
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 7;
struct matrix {
int a[5][5]={};
int n,m;
};
matrix I(int n) {
matrix ret;
ret.n=n;
ret.m=n;
for(int i=1;i<=n;i++) ret.a[i][i]=1;
return ret;
}
matrix operator * (matrix a, matrix b) {
matrix ret;
ret.n = a.n;
ret.m = b.m;
for(int i=1;i<=ret.n;i++) {
for(int j=1;j<=ret.m;j++) {
for(int k=1;k<=a.m;k++) {
ret.a[i][j] += a.a[i][k] * b.a[k][j];
ret.a[i][j] %= mod;
ret.a[i][j] += mod;
ret.a[i][j] %= mod;
}
}
}
return ret;
}
matrix qpow(matrix p, int q) {
return ((q&1)?p:I(2)) * (q?qpow(p*p,q/2):I(2));
}
signed main() {
matrix T;
T.n=2; T.m=2;
int a,b,n;
cin>>a>>b>>n;
T.a[1][1]=a; T.a[1][2]=b;
T.a[2][1]=1;
T=qpow(T,n-2);
matrix A;
A.n=2; A.m=1;
A.a[1][1]=1; A.a[2][1]=1;
if(n==1) cout<<1<<endl;
else cout<<(T*A).a[1][1];
}
[51nod 1126] 求递推序列的第N项 - 矩阵乘法的更多相关文章
- 51nod 1126 求递推序列的第N项
1126 求递推序列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f( ...
- 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释
题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...
- 51nod 1126 - 求递推序列的第N项 - [找规律]
题目链接:https://cn.vjudge.net/problem/51Nod-1126 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + ...
- 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...
- 51Nod 1126 求递推序列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...
- 515Nod 1126 求递推序列的第n项【矩阵快速幂】
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- [51NOD1126]求递推序列的第n项(矩阵快速幂)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 存在参数a,b为负数的情况.这时候要这么处理: 根据mo ...
- 51nod1126 求递推序列的第N项
求递推序列的第N项 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的 ...
- 51nod1126 求递推序列的第N项【递推】
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
随机推荐
- Pycharm2018.3.5永久破解
声明:不做商业用途,电脑系统win10专业版,亲测有效. 首先:可以先去lookdiv.com钥匙:lookdiv.com去使用更新的激活码,免费版的有效期待考量. 或者:可以下面方式破解 注意:破解 ...
- HDU_2191_多重背包
http://acm.hdu.edu.cn/showproblem.php?pid=2191 简单多重背包题. #include<iostream> #include<cstdio& ...
- Codeforces 1092 F Tree with Maximum Cost (换根 + dfs)
题意: 给你一棵无根树,每个节点有个权值$a_i$,指定一个点u,定义$\displaystyle value = \sum^v a_i*dist(u,v)$,求value的最大值 n,ai<= ...
- Python实现IOC控制反转
思路: 用一个字典存储beanName和资源 初始化时先将beanName和资源注册到字典中 然后用一个Dscriptor类根据beanName动态请求资源,从而实现控制反转 # -*- coding ...
- Spring Boot 2.1.7 启动项目失败,报错: "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."
一开始按照网上的很多解决办法是: 启动类头部声明@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),但是这样会排除 ...
- Shell脚本 硬盘监控
用shell 写了一个硬盘监控的小程序 #!/bin/bash MAX=95 EMAIL=zonghua@iyunshu.com PART=sda1 IP=`ifconfig |grep -w ...
- 注销Apache
到D:\phpTools\Apache24\bin下运行cmd 输入httpd.exe -k uninstall -n apache24 回车后提示注销完成 接着把Apache的文件删了即可
- 幻读在 InnoDB 中是被如何解决的?
在MySQL事务初识中,我们了解到不同的事务隔离级别会引发不同的问题,如在 RR 级别下会出现幻读.但如果将存储引擎选为 InnoDB ,在 RR 级别下,幻读的问题就会被解决.在这篇文章中,会先介绍 ...
- Quartz.NET - 课程 6: Cron 触发器
译者注: 目录在这 [译]Quartz.NET 3.x 教程 原文在这 Lesson 6: CronTrigger 如果你需要一个类似日历概念而不是像 SimpleTrigger 那样指定间隔来调度作 ...
- SpringCloud之Ribbon负载均衡的入门操作
使用Ribbon进行负载均衡 在使用Ribbon之前,我们先想一个之前的问题,之前我们将服务提供者注册进了eureka注册中心,但是在消费者端,我们还是使用的restTemplate调用的时候,其中写 ...