51nod 1126 矩阵快速幂 水
输入3个数:A,B,N。数字之间用空格分割。(-10000 <= A, B <= 10000, 1 <= N <= 10^9)
输出f(n)的值。
3 -1 5
6
题意:f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7
题解:矩阵快速幂处理
1 1 a 1
*
0 0 b 0
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#define ll __int64
using namespace std;
ll a,b,n;
struct matrix
{
ll m[][];
}ans,exm;
struct matrix matrix_mlit(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
there.m[i][j]=;
for(int u=;u<;u++)
there.m[i][j]=(there.m[i][j]+aa.m[i][u]*bb.m[u][j]%)%;
}
}
return there;
}
ll matrix_quick(ll aa,ll bb,ll gg)
{
exm.m[][]=aa;exm.m[][]=;
exm.m[][]=bb;exm.m[][]=;
ans.m[][]=;ans.m[][]=;
ans.m[][]=;ans.m[][]=;
gg-=;
while(gg)
{
if(gg&)
ans=matrix_mlit(ans,exm);
exm=matrix_mlit(exm,exm);
gg>>=;
}
return ans.m[][];
}
int main()
{
scanf("%I64d %I64d %I64d",&a,&b,&n);
if(n==){
printf("1\n");
return ;
}
if(n==){
printf("1\n");
return ;
}
printf("%I64d\n",(matrix_quick(a,b,n)+)%);
return ;
}
51nod 1126 矩阵快速幂 水的更多相关文章
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 51nod 1113 矩阵快速幂( 矩阵快速幂经典模板 )
1113 矩阵快速幂 链接:传送门 思路:经典矩阵快速幂,模板题,经典矩阵快速幂模板. /******************************************************* ...
- Foj1683矩阵快速幂水题
Foj 1683 纪念SlingShot 题目链接:http://acm.fzu.edu.cn/problem.php?pid=1683 题目:已知 F(n)=3 * F(n-1)+2 * F(n-2 ...
- 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 矩阵快速幂 水呀水
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- HDU1757-A Simple Math Problem,矩阵快速幂,构造矩阵水过
A Simple Math Problem 一个矩阵快速幂水题,关键在于如何构造矩阵.做过一些很裸的矩阵快速幂,比如斐波那契的变形,这个题就类似那种构造.比赛的时候手残把矩阵相乘的一个j写成了i,调试 ...
- 51Nod 1126 求递推序列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...
随机推荐
- PHP文件的读取
1.PHP部分文件操作函数 ( fopen ,fread ,filesize,fwrite,fclose ) 2.unlink() rmdir() 删除函数 unlink(路径和文件名): rmdi ...
- C# Request中修改header信息
var headers = app.Context.Request.Headers; Type hdr = headers.GetType(); PropertyInfo ro = hdr.GetPr ...
- DotNetBar for Windows Forms 12.9.0.0_冰河之刃重打包版及制作Visual Studio C#项目模板文件详解
关于 DotNetBar for Windows Forms 12.9.0.0_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- oracle 数据库信息查询
/*查询当前用户表信息/ select A.column_name 字段名, A.data_type 数据类型, A.data_length 长度, A.data_precisi ...
- js倒计时显示
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...
- Android Service提高
我们从以下几个方面来了解Service IntentService的使用 Service与Thread的区别 Service生命周期 前台服务 服务资源被系统以外回收处理办法 不被销毁的服务 Inte ...
- 生成模型(Generative)和判别模型(Discriminative)
生成模型(Generative)和判别模型(Discriminative) 引言 最近看文章<A survey of appearance models in visual object ...
- aspjpeg 组件在asp中的使用
本来好的系统,你却没有做好迁移等交接工作,所以,要麻烦死自己了-------for 凌杰 首先,该系统为asp系统,需要aspjpeg 支持.... 经过使用和测试.发现有如下小结. 1. 安装时 ...
- freeCAD文档结构
一个freecad文档包含了你场景中的所有物体.它可以包含组及任何工作平台制造的物体.你可以切换工作台,但是它仍然工作在同一个文档上.当您保存您的工作时,该文件就被保存到磁盘上.你可以同时打开多个fr ...
- DOM扩展之 专有扩展
11.4.3 contains() 方法 用来确定某个节点是不是另一个节点的后代. 注:a.contains(a) 也是返回true.说明contains方法搜索是从自身开始的. DOM Level ...