题目

第一次做是看了大牛的找规律结果,如下:

//显然我看了答案,循环节点是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(矩阵快速幂,找规律,模版更通用)的更多相关文章

  1. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  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 ...

  3. 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 ...

  4. 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 ...

  5. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  6. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  7. 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 ...

  8. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  9. 51nod-1537 1537 分解(矩阵快速幂+找规律)

    题目链接: 1537 分解  问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式  如果可以 输出 m%1e9+7 否则 输出no Input 一行,一个数n.( ...

随机推荐

  1. redis setnx 分布式锁

    private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 ...

  2. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  3. Regionals 2013 :: North America - Southeast USA

    Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...

  4. Linux下OpenCV的环境搭建

    OpenCV is the most popular and advanced code library for Computer Vision related applications today, ...

  5. Excel 窗体控件属性

    常规 AutoLoad   (Excel) 打开工作簿时是否加载控件.(如果是 ActiveX   控件,则忽略.) Enabled(表单) 控件是否可以接收焦点并响应用户生成的事件. Locked( ...

  6. Jquery操作Cookie取值错误的解决方法

    使用JQuery操作cookie时 发生取的值不正确,结果发现cookie有四个不同的属性,分享下错误的原因及解决方法. 使用JQuery操作cookie时 发生取的值不正确的问题:  结果发现coo ...

  7. 基于lnmp.org的xdebug安装

    1. 下载xdebug wget http://xdebug.org/files/xdebug-2.3.3.tgz 2. 创建一个目录: mkdir ./xdebug 3. 复制xdebug包到xde ...

  8. Global::pickSpecificClass_DNT

    /*************************************************** Created Date: 13 Jul 2013 Created By: Jimmy Xie ...

  9. Java对象校验框架之Oval

      只要有接口,就会有参数的校验,目前开源的校验框架已经非常多了,不过不得不提一下Oval.OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件.Annotation.POJO ...

  10. Python脚本控制的WebDriver 常用操作 <二十七> 文件下载

    测试用例场景 webdriver允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中. Python脚本 测试用Python代码: # coding=gbk ''' Crea ...