515Nod 1126 求递推序列的第n项【矩阵快速幂】
输入3个数:A,B,N。数字之间用空格分割。(-10000 <= A, B <= 10000, 1 <= N <= 10^9)
输出f(n)的值。
3 -1 5
6

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
ll A,B,n;
struct matrix
{
ll a[][];
};
matrix mutiply(matrix u,matrix v)
{
matrix res;
memset(res.a,,sizeof(res.a));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
res.a[i][j]=(res.a[i][j]+u.a[i][k]*v.a[k][j])%;
return res;
}
matrix quick_pow(ll n)
{
matrix ans,res;
memset(res.a,,sizeof(res.a));
for(int i=;i<;i++)
res.a[i][i]=;
ans.a[][]=A;ans.a[][]=B;
ans.a[][]=;ans.a[][]=;
while(n)
{
if(n&) res=mutiply(res,ans);
n>>=;
ans=mutiply(ans,ans);
}
return res;
}
int main()
{
scanf("%lld%lld%lld",&A,&B,&n);
if(n==) printf("1\n");
else if(n==) printf("1\n");
else
{
while(A<) A+=;
while(B<) B+=;
n-=;
matrix ans=quick_pow(n);
matrix res;
res.a[][]=res.a[][]=;
ans=mutiply(ans,res);
printf("%lld\n",ans.a[][]);
}
return ;
}
515Nod 1126 求递推序列的第n项【矩阵快速幂】的更多相关文章
- [51NOD1126]求递推序列的第n项(矩阵快速幂)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 存在参数a,b为负数的情况.这时候要这么处理: 根据mo ...
- [51nod 1126] 求递推序列的第N项 - 矩阵乘法
#include <bits/stdc++.h> using namespace std; #define int long long const int mod = 7; struct ...
- 51nod 1126 求递推序列的第N项
1126 求递推序列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f( ...
- 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释
题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...
- 51nod 1126 - 求递推序列的第N项 - [找规律]
题目链接:https://cn.vjudge.net/problem/51Nod-1126 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + ...
- 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...
- 51Nod 1126 求递推序列的第N项(矩阵快速幂)
#include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...
- 51nod1126 求递推序列的第N项
求递推序列的第N项 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的 ...
- 51nod1126 求递推序列的第N项【递推】
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
随机推荐
- Found conflicts between different versions of the same dependent assembly that could not be resolved
https://stackoverflow.com/questions/24772053/found-conflicts-between-different-versions-of-the-same- ...
- nyoj--84--阶乘的0(数学技巧)
阶乘的0 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示测试数据的组数(1<=N<=100 ...
- quartz.net定时任务框架详解
C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...
- sql server Delete误操作后如何恢复数据
声明:本文是根据别人的经验https://blog.csdn.net/dba_huangzj/article/details/8491327写的总结 说明:update和delete时没有加where ...
- 洛谷P1067 多项式输出(模拟)
题目描述 一元 n 次多项式可用如下的表达式表示: 其中,aixi称为 i 次项,ai 称为 i 次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 1. 多项式中 ...
- jQuery学习(三)——选择器总结
1.基本选择器 id选择器:$(“#id名称”); 元素选择器:$(“元素名称”); 类选择器:$(“.类名”); 通配符:* 多个选择器共用(并集) 案例代码: <!DOCTYPE html& ...
- 读取XML字符串到临时表
DECLARE @hdoc int DECLARE @doc xml SET @doc = '<CityValueSet> <CityItem> <CityId>2 ...
- pthread_cleanup_push vs Autorelease VS 异常处理
黑幕背后的Autorelease http://www.cnblogs.com/feng9exe/p/7239552.html objc_autoreleasePoolPush的返回值正是这个哨兵对象 ...
- ActiveMQ学习笔记(12)----ActiveMQ的集群
1. Queue consumer cluster ActiveMQ支持Consumer对消息的高可靠性的负载均衡消费,如果一个Consumer死掉,该消息会转发到其他的Consumer消费的Queu ...
- EFcore笔记之创建模型
排除属性:NotMapped NotMapped:排除属性在CodeFirst的时候在数据库里不创建该属性 public class Blog { public int BlogId { get; ...