HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 148003 Accepted Submission(s): 35976
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
1 2 10
0 0 0
5
最近学了简单一点的二维的矩阵快速幂,发现十分好用。于是又找以前做过的递推式的题目。大部分人做法应该是暴力打表发现循环节规律取前49项即可,但是这题也很适合用矩阵来加速求需要的某一项。只要会矩阵或者行列式的乘法就可以。另外为了写起来自然美观把函数改成了操作符重载。
(矩阵写法可以有很多种,F1,F2位置互换、横着写或者更离谱也行,只要能得出正确结果即可)
若求出递推式后只要注意一下指数到底应该是n还是n-1还是n-2,然后稍微特判一下,其他应该没啥问题。
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
struct mat
{
int pos[2][2];
mat(){memset(pos,0,sizeof(pos));}
};
inline mat operator*(const mat &a,const mat &b)
{
mat c;
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<2; k++)
{
c.pos[i][j]+=(a.pos[i][k]*b.pos[k][j])%7;
}
}
}
return c;
}
inline mat operator^(mat a,LL b)
{
mat r;r.pos[0][0]=r.pos[1][1]=1;
mat bas=a;
while (b!=0)
{
if(b&1)
r=r*bas;
bas=bas*bas;
b>>=1;
}
return r;
}
int main(void)
{
ios::sync_with_stdio(false);
int n,a,b;
while (cin>>a>>b>>n&&(a||b||n))
{
if(n==1)
{
cout<<1<<endl;
continue;
}
mat one,t;
one.pos[0][0]=one.pos[1][0]=1;
t.pos[0][0]=a,t.pos[0][1]=b;t.pos[1][0]=1;
t=t^(n-2);
one=t*one;
cout<<one.pos[0][0]%7<<endl;
}
return 0;
}
HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)的更多相关文章
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
- nyoj_148_fibonacci数列(二)_矩阵快速幂
fibonacci数列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the Fibonacci integer sequence, F0 = 0, F ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂
It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...
- fibonacci数列(二)_矩阵快速幂
描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- AE开发关于OnMapReplaced方法的使用原理
The OnMapReplaced event is triggered whenever the IMapControl2::Map is replaced by another map, such ...
- Python-OpenCV中的resize()函数
改变图像大小意味着改变尺寸,无论是单独的高或宽,还是两者.也可以按比例调整图像大小. 这里将介绍resize()函数的语法及实例. 语法 函数原型 cv2.resize(src, dsize[, ds ...
- 基于 Ubuntu + nextCloud 搭建自己的私人网盘
提醒一下,如果之前通过apache搭建了网站,不要用snap命令来搭建,否则,至少有一个无法正常运行(不要问我怎么知道的,都是血的教训啊). 你可以通过腾讯云的实验主机进行尝试. 1.基础设置 切换为 ...
- JavaScript onkeydown事件入门实例(键盘某个按键被按下)
JavaScript onkeydown 事件 用户按下一个键盘按键时会触发 onkeydown 事件.与 onkeypress事件不同的是,onkeydown 事件是响应任意键按下的处理(包括功能键 ...
- ios基础学习
action中调用函数方法别忘了冒号1. 各个视图之间的关系要分辨清楚 2. MVC (Model-View-Controller). In this pattern, models keep tra ...
- SC || Chapter7 健壮性和正确性
finally中语句不论有无异常都执行 若子类重写了父类方法,父类方法没有抛出异常,子类应自己处理全部异常而不再传播:子类从父类继承的方法不能增加或更改异常 判断checked和unchecked: ...
- iOS开发之蓝牙业务封装
因为公司做智能家居开发,有很多蓝牙的智能硬件.因此项目中经常需要和蓝牙打交道.为此为了提高开发效率,就把蓝牙的公共业务进行了封装. 本文将对封装的思路做一个简单的阐述. 首先我们需要一个头文件.在这个 ...
- 51nod——2489 小b和灯泡(打表/平方数)
这题打表去找因子的个数然后判奇偶也行.预处理O(n) 扫一遍判断O(n). ; i * i <= n; i++){ for(int j = i; i * j <= n; j++){ div ...
- 【Mysql】Mysql主从库搭建过程(爬完坑后整理所得)
Mysql主从数据库搭建流程 新手开始学习mysql主从库,遇到一些问题,总结后写出以下流程 下面以5.7.23版本为例介绍 第一步:去官网下载5.7.23版本的免安装压缩包形式的mysql文件,贴上 ...
- java版RSA工具类
/** * RSA算法加密/解密工具类 */ public class RSAUtils { private static final Logger LOGGER = LoggerFactory.ge ...