题目

和 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. 南阳理工oj88--汉诺塔(一)

    题目链接.http://acm.nyist.net/JudgeOnline/problem.php?pid=88 #include <stdio.h> /* //测试一下49999和500 ...

  2. Static的使用

    (一) 当它用于函数定义时,或用于代码块之外的变量声明时,static关键字用于修饰修改标识符的连接属性,从external改为internal,但是标识符的存储类型和作用域不受影响.用这种方式声明的 ...

  3. ubuntu 14.04 下 安装samba 及SSH 服务端的方法

    之前说了 FTP服务器的搭建:http://www.cnblogs.com/bcsflilong/p/4200139.html 今天说一下samba 和ssh服务器的搭建. 先说samba 吧. 在u ...

  4. javascript 数字字符串转为数字

    var a="111"; comsole.log(a);     //打印出来是个字符串类型 comsole.log(+a);   //打印出来是个数字类型 comsole.log ...

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

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

  6. [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)

    4.3 抽样(Sampling) 用基于MapReduce的程序来处理TB级的数据集,要花费的时间可能是数以小时计.仅仅是优化代码是很难达到良好的效果. 在开发和调试代码的时候,没有必要处理整个数据集 ...

  7. jquery文字上下滚动的实现方法

    jquery实现文字上下滚动的方法. 代码: //上下滚动var textRoll=function(){$('#notice p:last').css({'height':'0px','opacit ...

  8. URL锚点HTML定位技术机制

    一.锚点是什么 锚点就等同于火影中的“飞雷神之术”,我们先看百科中锚点的解释: 使用命名锚记可以在文档中设置标记,这些标记通常放在文档的特定主题处或顶部.然后可以创建到这些命名锚记的链接,这些链接可快 ...

  9. python之else总结

    python中除了if...elif...else..还有while...else, for...else..., try...except...else...finally... 不管哪种else, ...

  10. 51nod 1257 背包问题 V3

    1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...