hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)
第一次做是看了大牛的找规律结果,如下:
//显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的
#include<stdio.h>
int main()
{
int f[],a,b,i,n;
f[]=;f[]=;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
if(a==&&b==&&n==)break; for(i=;i<;i++)
{
f[i]=(a*f[i-])%+(b*f[i-])%;
}
printf("%d\n",f[n%]%);
}
return ;
}
第二次做是学了矩阵快速幂,这是经典的矩阵快速幂简单题
//简单的矩阵快速幂
//f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
//注意origin矩阵中的第二行的a和b的位置摆放
#include<stdio.h>
#include<string.h>
int num=,mod=;
struct matrix
{
int a[][];
};
matrix multiply(matrix x,matrix y)//矩阵乘法
{
matrix temp;
memset(temp.a,,sizeof(temp.a));
for(int i=;i<num;i++)
{
for(int k=;k<num;k++)
{
for(int j=;j<num;j++)
{
temp.a[i][j]=(temp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
}
}
}
return temp;
}
matrix calc(matrix a,int n)//矩阵快速幂——a^n
{
if(n==)return a;
matrix e;
for(int i=;i<num;i++)
for(int j=;j<num;j++)
e.a[i][j]=(i==j); while(n)
{
if(n&)
e=multiply(e,a);
n>>=;
a=multiply(a,a);
}
return e;
}
int main()
{
int n,a,b;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
if(a==&&b==&&n==)break;
matrix origin= {,};
origin.a[][]=b;origin.a[][]=a;
matrix answ={,,
,};
if(n>)
answ=multiply(calc(origin,n-),answ);
printf("%d\n",answ.a[][]);
}
return ;
}
hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)的更多相关文章
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- 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 ...
- 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 ...
- 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 ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- 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 ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- 51nod-1537 1537 分解(矩阵快速幂+找规律)
题目链接: 1537 分解 问(1+sqrt(2)) ^n 能否分解成 sqrt(m) +sqrt(m-1)的形式 如果可以 输出 m%1e9+7 否则 输出no Input 一行,一个数n.( ...
随机推荐
- SQL 建表与查询 HTML计算时间差
create database xue1 go --创建数据库 use xue1 go --引用数据库 create table xinxi ( code int, name ), xuehao ), ...
- C++ Double Ended Queues(双向队列)
双向队列和向量很相似,但是它允许在容器头部快速插入和删除(就像在尾部一样). Constructors 创建一个新双向队列 Operators 比较和赋值双向队列 assign() 设置双向队列的值 ...
- C++ 11 之学习总结
感慨时间过的好快,C++ 11出来都5年了,现在才开始学习,但为时也不晚: 主要是网上及身边的朋友大肆宣扬C++ 11的某些优化,弄得别人心里痒痒的,所以就花了3天学习了点基本知识,相对于整个C++ ...
- FPGA的图像处理技术,你知道多少?
最近一段时间一直在研究基于FPGA的图像处理,乘着EEPW这个机会和大家交流一下,自己也顺便总结一下.主要是为了大家对用FPGA做图像处理有个感性的认识,如果真要研究的话就得更加深入学习了.本人水平有 ...
- sql简易的MRP资源分析
写了个简易的MRP根据传进来的数据,进行上下级的判断,父表,子表 构思: 3张变量表,第一张用来存传进来的成品,这边对表做了循环就是成品是一样一样进去的,取成品表的第一行,将数据做父表和子表关联,取出 ...
- Linux C 程序 线程(18)
线程控制 1.线程与进程的关系 线程:计算机中独立运行的最小单位. 在用户角度:多个线程是同时执行的. 操作系统角度:各个线程交替执行 以上只针对单核CPU的情况 在多核CPU主机上,多个线程可以同时 ...
- webstorm 自定义代码模板
Ctrl+Shift+A查找设置live 添加代码片段Live Template 编写代码的时候 缩写+Tab 即可输出代码片段 缩写启动对应代码片段的钥匙. 描述代码片段的名字. 模板文本代码片段的 ...
- UE4 将本地图片转成UTexture2D 在runtime显示
UFUNCTION(BlueprintCallable, Category = "TextureFromDisk") static class UTexture2D* GetTex ...
- Apache 的 Rewrite 规则详细介绍
Rewrite标志: R[=code](force redirect) 强制外部重定向 F(force URL to be forbidden) 禁用URL,返回403HTTP状态码 G(force ...
- Configuration python CGI in XAMPP in win-7
1.After install XAMPP,we need add the path of the Mysql just find the path and add it to your sys-pa ...