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.( ...
随机推荐
- springmvc 精华
Spring Mvc简介: Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求 ...
- Win7中隐藏的上帝模式——GodMode
Win7中隐藏的上帝模式——GodMode ~ Windows7中的隐藏模式 ~ 随意新建一个文件夹吧,然后重命名为: GodMode.{ED7BA470-8E54-465E-825C-997 ...
- how to debug thread cpu 100%
when we write a program, cpu and memory usages are very important to indicate the stability of the p ...
- 6.ipv6地址配置
1. "nmcli connection modify 网卡名 ipv4.addresses "ipv6地址" ipv6.method manual ". 2. ...
- UILabel滚动字幕的实现
经常需要在应用中显示一段很长的文字,比如天气或者广告等,这时候使用滚动字幕的方式比较方便. 参考文献: [1] YouXianMing, 使用UILabel实现滚动字幕移动效果, 博客园 [2] ht ...
- 8款实用的Jquery瀑布流插件
1.网友Null分享Jquery响应式瀑布流布局插件 首先非常感谢网友Null的无私分享,此作品是一款响应式瀑布流布局Jquery插件,网友Null增加了一个屏幕自适应和响应式,响应式就是支持智能手机 ...
- 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式
Helper类代码 public class Helper { #region 单列循环 private Helper() { } private static Helper instance = n ...
- Asp.net MVC知识积累
一.知识积累 http://yuangang.cnblogs.com/ 跟蓝狐学mvc教程专题目录:http://www.lanhusoft.com/Article/169.html 依赖注入:htt ...
- 自改xss小平台上线
原先一直用xss.hk结果不知怎么被关的,正好手上有代码于是自己搭了一个,网上的类似的xss平台大多一样,原先的xss的chrome插件,不适合 "manifest_version" ...
- keyboard
一 按键驱动程序的简单实现 下面是基于中断和消息的按键驱动程序,其工作原理是:当应用程序读取键值时,会调用按键驱动程序的read函数,而我们实现的read函数检测完读取长度后没有直接读取键值而是等待按 ...