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 ...
随机推荐
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) H Heroes Of Might And Magic (隐含dp)
问题是求一个方案,实际隐含一个dp.法力是递减的,所以状态是DAG,对于一个确定的状态,我们贪心地希望英雄的血量尽量大. 分析:定义状态dp[i][p][h]表示是已经用了i的法力值,怪兽的位置在p, ...
- codeforce Gym 100500A Poetry Challenge(博弈,暴搜)
题解:状态压缩之后,暴力dfs,如果有一个选择,能让对手必败,那么就是必胜态,能转移到的状态都是对手的必胜态,或者无法转移,就是必败态. 总算是过了,TLE是因为状态没判重. #include< ...
- ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi ...
- 如何从Ubuntu 16.04 LTS升级到Ubuntu 18.04 LTS
可以说非常简单(假设过程顺利!!) 您只需打开Software&Update,进入"Updates"选项卡,然后从“有新版本时通知我”下拉菜单中选择“适用长期支持版”选项. ...
- Python SciPy Sparse模块学习笔记
1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix的存储 ...
- Bootstrap历练实例:下拉菜单插件方法的使用
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 【贪心 计数】bzoj2006: [NOI2010]超级钢琴
这么经典的贪心我怎么现在才做啊…… Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐. 这架超级钢琴可以弹奏出n个 ...
- 洛谷 2387/BZOJ 3669 魔法森林
3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3765 Solved: 2402[Submit][Statu ...
- 【Java_Spring】RestTemplate发HTTP请求详解
Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解) Spring RestTemplate提交时设置http header请求头 Spring之RestTempla ...
- 跟踪路由 tracert
由于最近遇到网络出现故障的问题,便使用到Tracert来确定了下出现故障的网络节点 记录下tracert命令相关内容 1. 简介 2. Tracert工作原理... 3. 常用参数 4. 使用示例与输 ...