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 输 ...
随机推荐
- ThinkPHP5.0框架开发--第7章 TP5.0数据库操作
ThinkPHP5.0框架开发--第7章 TP5.0数据库操作 第7章 TP5.0数据库操作 ===================================================== ...
- sql server 去掉某字段左边的0
比如:将以下Code字段 Code 000 001 002 ... 009 010 依次去掉左边的0,000替换为0,001替换为1,010替换为10,以此类推 Code 0 1 2 ... 9 10 ...
- BZOJ 4289: PA2012 Tax(最短路)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 240[Submit][Status][Discuss] Descriptio ...
- MyBatis数据持久化(五)数据源配置优化
在前面的教程中,我们把数据库的驱动.用户名.密码等配置项全部写在 SqlMapConfig.xml中: <dataSource type="POOLED"> <p ...
- Android之通过HttpURLConnection.getResponseCode状态码抛出异常的问题以及解决方法
1.最近,在学习解析json数据的时候遇到一个错误信息,错误信息如下图所示: 发现解析出来的数据为空,错误信息如上图所示,发现程序中的HttpUtils工具类的22行出现了错误和MainActiv ...
- c# 02-18 值类型 引用类型 字符串的不可变性 字符串的处理方法
1值类型 直接把值存在栈中 栈的特点是后进先出 int double decimal char struct enum bool 2 引用类型 把值存在堆中,把地址存在栈中: string 自定义的类 ...
- 不实例化一个 class 的时候使用它的property
class A: @property def name(self): " print(A.name) # <property object at 0x10d54cf98> cla ...
- CodeForces-722C Destroying Array 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/CodeForces-722C 题意 给个数组,每次删除一个元素,删除的元素作为一个隔断,问每次删除后该元素左右两边最大连续和 思 ...
- BZOJ 2865 字符串识别(后缀数组+线段树)
很容易想到只考虑后缀长度必须为\(max(height[rk[i]],height[rk[i]+1])+1\)(即\([i,i+x-1]\)代表的串只出现过一次)然后我正着做一遍反着做一遍,再取一个\ ...
- CF451E Devu and Flowers (组合数学+容斥)
题目大意:给你$n$个箱子,每个箱子里有$a_{i}$个花,你最多取$s$个花,求所有取花的方案,$n<=20$,$s<=1e14$,$a_{i}<=1e12$ 容斥入门题目 把取花 ...