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.( ...
随机推荐
- [GeekBand] C++ 高级编程技术 (1)
一.类型转换 class Fraction { public: explicit Fraction(int num, int den=1) : m_numerator(num), m_denomina ...
- win10任务视图
之所以用到win10任务视图源自于一个需求,就是我想在上班操作电脑的同时想将某个游戏在后台挂机,这样工作归工作,挂机归挂机,互不干扰.win10任务视图就能轻松的解决这个问题. 任务视图 新建任务视图 ...
- C#操作Excel基本操作
/// using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System.IO; using System ...
- 玄机宝盒v1.6.1.1
最新版本:玄机宝盒v1.6.1.1 玄机宝盒v1.6.1.1 04-14/2016 给你的将是无与伦比的体验http://bbs.msdn5.com/thread-15-1-1.html(出处: 玄机 ...
- HTML5 Shiv – 让该死的IE系列支持HTML5吧
HTML5能为我们做的事儿很多,最为可口的就是语义化标签的应用,如果你已经在Chrome或者其他支持HTML5的浏览器上用过它的牛x,那这篇文章对你一定有用,因为现在你也可以在IE上用到HTML5. ...
- PHP类的自动载入机制
php的自动加载: 在php5以前,我们要用某个类或类的方法,那必须include或者require,之后才能使用,每次用一个类,都需要写一条include,麻烦 php作者想简单点,最好能引用一个类 ...
- IIFE-js中(function(){…})()立即执行函数写法理解
介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...
- Android emulator warning----Emulator window was out of view and was recentred
最近在打开Android emulator时,总会提示“Emulator window was out of view and was recentred ”,然后无法打开模拟器,但是可以使用Win7 ...
- openerp模块收藏 移除下拉选择列表中的“创建并编辑”链接(转载)
移除下拉选择列表中的“创建并编辑”链接 原文:http://shine-it.net/index.php/topic,5990.0.html 有时希望下拉列表中列出的项是与主表某个字段关联的,用户只能 ...
- 3. opencv进行SIFT特征提取
opencv中sift特征提取的步骤 使用SiftFeatureDetector的detect方法检测特征存入一个向量里,并使用drawKeypoints在图中标识出来 SiftDescriptorE ...