HDU - 1005

Number Sequence

Problem Description
A number sequence is defined as follows:
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).

Input

The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output

For each test case, print the value of f(n) on a single line.
Sample Input

1 1 3

1 2 10

0 0 0

Sample Output

2

5

网上的一些解法很多是关于找规律的,其实找规律也是有一些道理的,根据鸽巢原理总会出现一些重复项,所以找到规律以后开始mod就行

但是这种解法毕竟还是有bug,虽然能够AC掉,但也有人提出了Hack数据   HDU数据有点水

其实Hack挺容易,就是针对一个程序,设计一组n,k让它很难找出规律就行

所以这个时候矩阵快速幂就来了~

mod的问题很好解决,我们先来看一下如何构建矩阵

    

我们可以假定有一个矩阵K,使得{f(n-1) f(n-2)}与之相乘之后可以得到{f(n) f(n-1)}

由f(n) = (A * f(n - 1) + B * f(n - 2)):

  相乘之后的矩阵可化为{A * f(n - 1) + B * f(n - 2)  f(n-1) }

不难得出矩阵K

所以初始化矩阵ans为

{f(2) f(1)} 即  {1 1} 竖着写也可以我懒得开二维所以直接写了横着的一维数组

构建另一个矩阵K为

{A  1}

{B  0}

如果n的值为1或2,直接返回  注意一定要返回!!!不然n=1,n-=2,n=-1,然后while(-1)  呵呵呵~~~~

否则求A*Kn-2 输出ans[1]的值即可。 为什么是n-2?显然啊,可以自己举个例子,求n=3,要乘1次即可

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int mat[][],ans[];
void Mul(){
int temp[];
for(int i=;i<=;i++){
temp[i]=;
for(int k=;k<=;k++)
temp[i]+=(ans[k]*mat[k][i]%);
temp[i]%=;
}
memcpy(ans,temp,sizeof(temp));
}
void Mulself(){
int temp[][];
for(int i=;i<=;i++){
for(int j=;j<=;j++){
temp[i][j]=;
for(int k=;k<=;k++)
temp[i][j]+=(mat[i][k]*mat[k][j]%);
temp[i][j]%=;
}
}
memcpy(mat,temp,sizeof(temp));
}
int main(){
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c)){
if(!b&&!a&&!c)break;
if(c<= ){
printf("1\n");
continue;
}
mat[][]=a,mat[][]=;
mat[][]=b;mat[][]=;
ans[]=ans[]=;
c-=;
while(c){
if(c&)Mul();
Mulself();
c>>=;
}
printf("%d\n",ans[]%);
}
}

HDU - 1005 Number Sequence 矩阵快速幂的更多相关文章

  1. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  2. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  3. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  4. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  5. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  6. Yet another Number Sequence 矩阵快速幂

    Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...

  7. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  8. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  9. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

随机推荐

  1. webpack的基本配置(初识)

    webpack能根据模块的依赖关系递归地构建一个依赖关系图,当中包含了应用程序所需要的所有模块,最后打包成一个或多个bundle.它有四个核心概念entry.output .loader.plugin ...

  2. Python——3条件判断和循环

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. python爬虫-提取网页数据的三种武器

    常用的提取网页数据的工具有三种xpath.css选择器.正则表达式 1.xpath 1.1在python中使用xpath必须要下载lxml模块: lxml官方文档 :https://lxml.de/i ...

  4. [面试专题]Web缓存详解

    Web缓存详解 标签(空格分隔): 缓存 缓存之于性能优化 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影响网站交互的前提下可以大大加快网站加载速度. 降低服务器压力: ...

  5. Yuchuan_Linux_C编程之五gdb调试

    一.整体大纲 二.gdb调试 1. 启动gdb start -- 只执行一步    n -- next    s -- step(单步) -- 可以进入到函数体内部    c - continue - ...

  6. VUE一 基础语法

    一.模板语法 二.Class和Style绑定 三.条件渲染 四.vue事件处理器 五.vue组件

  7. vue的子组件不能进行router的切换

    在用vue开发过程中,偶然一次使用在子组件中进行router的切换,发现不起作用,后来才反应过来,子组件只是一个组件,vue的路由的切换只能是在父组件(也就是真正的页面)里面进行跳转!

  8. 看逐浪CMS技术小哥做SVG动画(附使用Bodymovin和Lottie将Adobe After Effects(AE)程式转为 HTML5/Android/iOS原生的动画全过程-即AE转svg\canvas\html5动画)

      名词解解释 adobe After Effects AE:adobe After Effects,adobe公司的专业视频制作软件. Bodymovin插件预览 Bodymovin:是一个AE的插 ...

  9. 回想笔记 瞎比比 域名注册 解析绑定ip 下载证书 设置证书 重定向http到https请求

    2019.7.27 回想笔记 拥有腾讯云服务器一台 阿里云注册5元域名,进行备案 完成之后 使用解析 绑定服务器ip地址 ,使用域名可以访问到web服务器而不是通过直接暴露ip地址进行访问 证书购买 ...

  10. props watch 接口抖动

    readType (val) { this.innerReadType = '-' this.$nextTick(() => { this.innerReadType = val }) },