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\). ...
随机推荐
- Android 技术用于汇总
id 名词 含义 详细 1 Android CTS CTS 全称 Compatibility Test Suite 兼容性测试工具 当产品开发出来以后,并定制了自己的 Android 系统后 ...
- Oracle中是用case...when语句进行判断
使用case...when语句进行判断,其语法格式如下: case<selector> when<expression_1> then pl_sqlsentence_1; wh ...
- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS" in the equal to operation.
ErrorMessage Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" ...
- Mininet VM设置笔记
Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...
- eclipse下mysql编程
mysql -uroot -p 密码 1:如果/usr/include/mysql路径下不存在头文件请: apt-get install libmysql++安装开发包 2:程序中添加头文件<m ...
- Jira 6.0.3 安装与破解
如果你还没有使用Jira做项目跟踪与管理,那就赶紧试用一下吧.下面教你一步一步安装Jira 6.0.3,以及如何破解试用版. 一. 安装准备 1. 去Jira官方网站下载http://www.at ...
- 深入理解ThreadLocal(一)
Android里,在不同的线程(假设子线程已经创建了Looper)中创建Handler时,并不需要显式指定Looper,系统能自动找到该线程自己的Looper.不同线程的Looper相互独立,之所以能 ...
- wampserver修改mysql数据库密码后phpMyAdmin无法连接数据库
phpMyAdmin开始能打开,但是我在权限里把root的密码改了,再打开phpMyAdmin就显示:“phpMyAdmin 试图连接到 MySQL 服务器,但服务器拒绝连接.您应该检查 config ...
- Daily Scrum 11.9
摘要:本次的meeting主要是继续讨论程序的问题以及单元测试和集成测试等,本次为1.01版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Tas ...
- 团队博客作业Week1 Team Homework #3软件工程在北航
这次我们采访了一位大四的学姐,让她简单地谈了谈去年学习软件工程的经历和感受. 在完成软件工程大作业的过程中,由于计划安排与实际脱节,导致时间前松后紧,平均每周花在这门课上的时间大约有8个小时. 项目完 ...