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. LeetCode~报数(简单)

    报数(简单) 题目描述: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ( ...

  2. 浅谈了解JDBC

    目录 前言 作用 JDBC的架构 步骤 JDBC常见的关键字解释 前言 Java数据库连接,是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法.J ...

  3. Spring AOP使用方式

    AOP:全称是Aspect Oriented Programming,面向切面编程 Spring AOP的作用和优势: 作用:在程序运行期间,不修改源码对已有方法进行增强 优势:减少重复代码:提高开发 ...

  4. XML的相关基础知识分享(二)

    前面我们讲了一下XML相关的基础知识(一),下面我们在加深一下,看一下XML高级方面. 一.命名空间 1.命名冲突 XML命名空间提供避免元素冲突的方法. 命名冲突:在XML中,元素名称是由开发者定义 ...

  5. YiGo环境搭建

    软件环境 操作系统:Windows 2000+,Mac OS,AIX,RedHat linux,HP-UX等 JDK/JRE:Oracle JDK/JRE 1.8+,IBM J9 VM 1.8+,Op ...

  6. nginx设置目录浏览及中文乱码问题解决

    在Nginx下默认是不允许列出整个目录的.如需此功能, 先打开nginx.conf文件,在location server 或 http段中加入 autoindex on;另外两个参数最好也加上去: a ...

  7. 《javascript高级程序设计》笔记:文档模式

    文档模式是用于指定浏览器使用什么样的标准来正确的显示网页,各个标准的解析存在着差异 文档类型的分类 文档模式大致分为三种类型: 混杂模式(quirks mode) 标准模式(standards mod ...

  8. Safari配置WebApp----添加启动图和桌面图标让你的WebApp在ios设备上体验如原生一样

    现在很多开发者的Web应用程序的设计样式和交互类似本机应用程序,例如,它的缩放比例适合iOS上的整个屏幕.当用户将其添加到主屏幕时,您可以通过使其看起来像本机应用程序一样,在iOS上为您的Web应用程 ...

  9. 使用veloticy-ui生成文字动画

    前言 最近要实现一个类似文字波浪线的效果,使用了velocity-ui这个动画库,第一个感觉就是使用简单,代码量少,性能优异,在此简单介绍一下使用方法,并实现一个看上去不错的动画.具体使用方法可以点击 ...

  10. 实验一 Linux系统与应用准备(嵌入式Linux工程师的“修真之路”)

    作业格式 项目 内容 这个作业属于哪个课程 这里是链接[https://edu.cnblogs.com/campus/nchu/2020SpringSystemAndApplication] 这个作业 ...