LightOj 1065 - Number Sequence (矩阵快速幂,简单)
和 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 (矩阵快速幂,简单)的更多相关文章
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
- 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 ...
- 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 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- 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 ...
- 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 ...
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
- 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\). ...
随机推荐
- System Generator入门
System generator 安装之后会在Simulin模块库中添加一些Xilinx FPGA专用的模块库,包括Basic Element,Communication,Control Logi ...
- webBrowser执行js的方法,并返回值,c#后台取值
private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate(Application.StartupPath + ...
- HTML:form表单总结,input,select,option,textarea,label
<form>标签是块级元素. form标签的标准属性有id,class,style,title,lang,xml:lang. 表单能够包含input元素(包含button,checkbox ...
- 命令行参数的处理函数getopt
命令参数 在linux下, shell命令的参数分两种情况: a.参数需要附加信息, 如"wget http://www.abc.com/1.zip -o 1.zip" b.参数不 ...
- linux终端io笔记
简介 终端的两种工作模式:以行为单位的工作模式,以字符数或时间为单位自定义模式 终端判断函数: int isatty(int fd) 终端属性的获取与设置: int tcgetattr(int fd, ...
- Java 包(package)详解
为了更好地组织类,Java提供了包机制,用于区别类名的命名空间. 包的作用 1 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2 如同文件夹一样,包也采用了树形目录的存储方式.同一 ...
- 【HTML5】websocket 初识
什么是WebSocket API? WebSocket API是下一代客户端-服务器的异步通信方法.该通信取代了单个的TCP套接字,使用ws或wss协议,可用于任意的客户端和服务器程序.WebSock ...
- C# 实现Oracle中的数据与Excel之间的转换
最近项目要求实现数据库之间数据在各个数据库之间导入导出,在此做个笔记 1. 将Oracle中的表导入到Excel中,反之亦然 private static readonly string conne ...
- Unity 优化
1. 尽量避免每帧处理比如: function Update() { DoSomeThing(); } 可改为每5帧处理一次: function Update() { == ) { DoSomeThi ...
- Most People Aren’t.
Most people want to be fit, most people aren't. Most people want to build a successful business, mos ...