题目

和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int num,mod;
struct matrix
{
int a[][];
}origin,answ; matrix multiply(matrix x,matrix y)//矩阵乘法
{
matrix temp;
// memset(temp.a,0,sizeof(temp.a));
for(int i=;i<=num;i++)
{
for(int j=;j<=num;j++)
{
int ans=;
for(int k=;k<=num;k++)
{
ans+=((x.a[i][k]*y.a[k][j])%mod);
}
temp.a[i][j]=ans%mod;
}
} return temp;
} matrix calc(int n)//n次矩阵快速幂
{
while(n)
{
if(n%==)
answ=multiply(origin,answ);
origin=multiply(origin,origin);
n/=;
}
return answ;
} int main()
{
int t,id,a,b,n,m;
scanf("%d",&t);
for(id=;id<=t;id++)
{
scanf("%d%d%d%d",&a,&b,&n,&m);
num=;
memset(answ.a,,sizeof(answ.a));
memset(origin.a,,sizeof(origin.a));
mod=;
while(m--)
mod=mod*;
answ.a[][]=a;
answ.a[][]=b;
origin.a[][]=;
origin.a[][]=;
origin.a[][]=;
origin.a[][]=;
printf("Case %d: ",id);
if(n==)printf("%d\n",a%mod);
else if(n==)printf("%d\n",b%mod);
else
{
calc(n-);
printf("%d\n",(answ.a[][]+answ.a[][])%mod);
}
}
return ; }

LightOj 1065 - Number Sequence (矩阵快速幂,简单)的更多相关文章

  1. LightOJ 1065 - Number Sequence 矩阵快速幂水题

    http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...

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

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

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

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

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

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

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

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

  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. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  10. CodeForces 392C Yet Another Number Sequence 矩阵快速幂

    题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...

随机推荐

  1. linux之Vim使用

    Vim同Emac是Linux世界下最为流行的两个文本编辑工具,集中精力学习一个就好了,暂定以Vim为学习对象.在本文中,一些基本的操作将不再介绍,只会介绍最为常用的命令以及设置,操作系统为Ubuntu ...

  2. 【风马一族_xml】xmlp之dtd1

    什么是XML约束? 在xml技术里,可以编写一个文档来约束一个xml文档的写法,这称之为xml约束 2. 为什么要使用xml约束? 参看提示栏 3. xml约束的作用? 约束xml的写法 对xml进行 ...

  3. 【转】HttpServletRequest.getParameter() &HttpServletRequest.getAttribute() 区别

    Ref: HttpServletRequest的getParameter和getAttribute方法有什么区别 具体如下几点: (1)HttpServletRequest类有setAttribute ...

  4. 【原】Infragistics.Win.UltraWinGrid.UltraGrid 增加行号

    private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayo ...

  5. Js操作Select大全(取值、设置选中)

    Js操作Select是很常见的,也是比较实用的. jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<s ...

  6. Oracle 表的访问方式(2)-----索引扫描

    索引扫描(Index scan) 我们先通过index查找到数据对应的rowid值(对于非唯一索引可能返回多个rowid值),然后根据rowid直接从表中得到具体的数据,这种查找方式称为索引扫描或索引 ...

  7. perl中shift 和unshift 操作

    ##################################################################### unshift 和shift 对一个数组的开头进行操作(数组 ...

  8. Python默认模块 os和shutil 实用函数

    os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 ' ...

  9. backbone collection add 事件回调参数

    this.listenTo(this.collection, 'add', this.renderBook); renderBook: function (item) { var bookView = ...

  10. ORA-1653: unable to extend table SYS.AUD$

    今早运维组的同事反映有个系统功能很多地方都报错,怀疑是不是数据库有什么问题.于是登录数据库检查,通过crsctl status res -t检查,发现所有集群资源都是OK的,没有哪个资源挂掉了.于是到 ...