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. 从游戏到汽车 “明星IP”的发财轨迹

    "明星IP"的发财轨迹" title="从游戏到汽车 "明星IP"的发财轨迹"> 移动互联网时代的开启,不仅彻底重构了大众生 ...

  2. Git 程序员篇

    关于 Git Git 背后的故事 伟大的作品总是诞生于伟大的时代,正如 Git 同样诞生于一个英雄辈出.极富纷争的年代. 2005 年,Linux 内核开发社区正面临严峻的挑战:他们不能继续使用 Bi ...

  3. Samtec 5G探索之路

    序言:时代在发展,2020年5G作为元年.5G全程第五代移动通信技术(英语:5th generation mobile networks或5th generation wireless systems ...

  4. HTML简单的提示框

    由于项目中需要一个简单的提示框,就是鼠标放上去,可以提示相关信息,引用第三方的比较麻烦,所以,这里封装了一个很简单的HTML方法. <script src="http://cdn.st ...

  5. 【前端】这可能是你看过最全的css居中解决方案了~

    1.水平居中:行内元素解决方案 适用元素:文字,链接,及其其它inline或者inline-*类型元素(inline-block,inline-table,inline-flex) html部分代码: ...

  6. 简单的节流函数throttle

    在实际项目中,总会遇到一些函数频繁调用的情况,比如window.resize,mouseover,上传进度类似的触发频率比较高的函数,造成很大的性能损耗,这里可以使用节流函数来进行性能优化,主要是限制 ...

  7. NoVNC API 文档翻译

    原文地址:https://github.com/novnc/noVNC/blob/master/docs/API.md 时间:2019-05-21     noVNC API The interfac ...

  8. DBProxy快速入门

    1. DBProxy安装 1.1 安装依赖项 CentOS yum install -y Percona-Server-devel-55.x86_64 Percona-Server-client-55 ...

  9. Java多线程并发03——在Java中线程是如何调度的

    在前两篇文章中,我们已经了解了关于线程的创建与常用方法等相关知识.接下来就来了解下,当你运行线程时,线程是如何调度的.关注我的公众号「Java面典」了解更多 Java 相关知识点. 多任务系统往往需要 ...

  10. 有关EPX Studio使用DELPHI5作为基础环境版本的说明

    英巴卡迪诺北京科技有限公司,地址是北京市朝阳门外大街18号丰联广场B座813B,这家公司这家公司不拥有:delphi 1.0~delphi7.0 .delphi 2005版本的著作权,这些都还是属于B ...