有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
给出A,B和N,求f(n)的值。
Input
输入3个数:A,B,N。数字之间用空格分割。(-10000 <= A, B <= 10000, 1 <= N <= 10^9)
Output
输出f(n)的值。
Input示例
3 -1 5
Output示例
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项【矩阵快速幂】的更多相关文章

  1. [51NOD1126]求递推序列的第n项(矩阵快速幂)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 存在参数a,b为负数的情况.这时候要这么处理: 根据mo ...

  2. [51nod 1126] 求递推序列的第N项 - 矩阵乘法

    #include <bits/stdc++.h> using namespace std; #define int long long const int mod = 7; struct ...

  3. 51nod 1126 求递推序列的第N项

    1126 求递推序列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f( ...

  4. 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释

    题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...

  5. 51nod 1126 - 求递推序列的第N项 - [找规律]

    题目链接:https://cn.vjudge.net/problem/51Nod-1126 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + ...

  6. 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...

  7. 51Nod 1126 求递推序列的第N项(矩阵快速幂)

    #include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...

  8. 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)的 ...

  9. 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 输 ...

随机推荐

  1. Android This Activity already has an action bar supplied by the window decor

    This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ ...

  2. 高斯滤波及高斯卷积核C++实现

    高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,在图像处理的降噪.平滑中应用较多,特别是对抑制或消除服从正态分布的噪声非常有效. 高斯滤波的过程其实就是对整幅图像进行加权平均操作的过程.滤波后图像上每 ...

  3. 3.多线程传参,以及tuple数组

    #include <Windows.h> #include <thread> #include <iostream> #include <tuple> ...

  4. POJ 2184 DP

    思路: f[j]表示当ts的和为j的时候tf的最大值. 这时候要分情况讨论: (我把状态平移了101000) 若ts[i]>=0倒序循环 否则正序 (防止ts被用了多次) f[101000]=0 ...

  5. js函数 DOM操作

    回学校了两天请了两天假,数组和方法的内容周末一定补上! 今天介绍一下JavaScript函数 Function 一.基础内容 1.定义 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. f ...

  6. svn创建分支的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1.  首先选择你要创建分支的工作目录,如下图: 2.选择要创建分支的路径.注释以及版本,选择HEADrevision ...

  7. MSSQL读取xml字符串到临时表

    DECLARE @hdoc int DECLARE @doc xml SET @doc ='<CityValueSet> <CityItem> <CityId>20 ...

  8. “.”开头,以"}"结尾,中间是任意字符的正则

    "."开头,以"}"结尾,中间是任意字符的正则 /^\..+\{$/

  9. JWT的初步了解以及session、cookie机制

    1.什么是状态保持? 想要了解JWT,首先需要知道什么是状态保持,举一个例子来说:无论是在web上还是在手机app上,我们都可以以游客的身份访问,此时都会有登录/注册字眼,当我们登录之后,就会是我们的 ...

  10. layui Layui-Select多选的使用和注意事项

    1.最近买了layadmin的后台框架,使用Layui-Select总结如下 A.配置:我采用的全局引入配置的方式 赋值(选中状态)