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. 压力测试(六)-阿里云Linux服务器压测接口实战

    1.SpringBoot 接口打包,并用jar包方式部署 简介:用jar包方式在控制台进行启动 打包 mvn package && java -jar target/gs-spring ...

  2. 从头认识js-js中的继承

    要彻底弄明白js中的继承,我们首先要弄清楚js中的一个很重要的概念那就是原型链. 1.什么是原型链? 我们知道每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型 ...

  3. 前端每日实战:42# 视频演示如何用纯 CSS 创作一个均衡器 loader 动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/oybWBy 可交互视频教程 此视频 ...

  4. 峰哥说技术:10-Spring Boot静态资源处理

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 10  峰哥说技术:Spring Boot静态资源处理 今天我们聊聊关于 Spring Boot 中关于静 ...

  5. 基于Unix Socket的可靠Node.js HTTP代理实现(支持WebSocket协议)

    实现代理服务,最常见的便是代理服务器代理相应的协议体请求源站,并将响应从源站转发给客户端.而在本文的场景中,代理服务及源服务采用相同技术栈(Node.js),源服务是由代理服务fork出的业务服务(如 ...

  6. GPS同步时钟装置应用及选择

    GPS同步时钟装置应用及选择 GPS是全球定位系统的简称,GPS具有全天时.全天候.高精度.定位和授时服务,GPS卫星授时成本低.安全可靠.覆盖范围广.GPS同步时钟装置,是指从GPS卫星上获取时间信 ...

  7. 基于SpringCloud搭建项目-Zuul篇(六)

    本文主要介绍zuul的基本原理和在sprngcloud服务下如何使用 一.简单介绍 Zuul 是 Netflix OSS 中的一员,是一个基于 JVM 路由和服务端的负载均衡器.提供路由.监控.弹性. ...

  8. Spring Boot 自动装配流程

    Spring Boot 自动装配流程 本文以 mybatis-spring-boot-starter 为例简单分析 Spring Boot 的自动装配流程. Spring Boot 发现自动配置类 这 ...

  9. Xcode调试之exc_bad_access以及 message sent to deallocated instance

    如果出现exc_bad_access错误,基本上是由于内存泄漏,错误释放,对一个已经释放的对象进行release操作.但是xcode有时候不会告诉你错误在什么地方(Visual Studio这点做得很 ...

  10. openwrt 上的 upnp wifi 音频推送 gmediarender

    首先是必须启用的模块 Libraries ---> <*> libupnp Sound ---> <*> alsa-utils<*> madplay-a ...