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 − 1) + f(n − 2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values of a and b, you can get many different sequences. Given the values of a, b, you have to find the last m digits of f(n). Input The first line gives the number of test cases, which is less than 10001. Each test case consists of a single line containing the integers a b n m. The values of a and b range in [0, 100], value of n ranges in [0, 1000000000] and value of m ranges in [1, 4]. Output For each test case, print the last m digits of f(n). However, you should NOT print any leading zero.
Sample Input
4
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4
Sample Output
89
4296
7711
946
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 51
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
//矩阵快速幂
int a, b, n, m, mod;
struct Mat
{
int a[][];
Mat()
{
memset(a, , sizeof(a));
}
Mat operator*(const Mat& rhs)
{
Mat ret;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
for (int k = ; k < ; k++)
ret.a[i][j] = (ret.a[i][j] + a[i][k] * rhs.a[k][j]) % mod;
}
}
return ret;
}
};
Mat fpow(Mat m, int b)
{
Mat ans;
for (int i = ; i < ; i++)
ans.a[i][i] = ;
while (b != )
{
if (b & )
ans = m * ans;
m = m * m;
b = b / ;
}
return ans;
}
Mat M;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d%d", &a, &b, &n, &m);
mod = pow(, m);
M.a[][] = M.a[][] = M.a[][] = ;
M.a[][] = ;
M = fpow(M, n - );
printf("%d\n", (M.a[][] * b + M.a[][] * a) % mod);
}
}
Yet another Number Sequence 矩阵快速幂的更多相关文章
- 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 ...
- 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\). ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
随机推荐
- 【BZOJ4059】Non-boring sequences(分析时间复杂度)
题目: BZOJ4059 分析: 想了半天没什么想法,百度到一个神仙做法-- 设原数列为 \(a\),对于每一个 \(i\) 求出前一个和后一个和 \(a_i\) 相等的位置 \(pre[i]\) 和 ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
- 全面学习ORACLE Scheduler特性(10)管理Chains
5.2 管理Chains 5.2.1 修改Chains属性 基本上碰到修改CHAIN属性的机率不会太大,因此确实没啥可修改的,对于CHAIN对象来说,能够修改的属性只有两个:evaluation_ ...
- 笔试题的各种trick
%x 默认去掉前导零 #include<stdint.h> #include<stdio.h> union X { int32_t a; struct ...
- 为什么我的对象被 IntelliJ IDEA 悄悄的修改了?
背景 最近,在复习JUC的时候调试了一把ConcurrentLinkedQueue的offer方法,意外的发现Idea在debug模式下竟然会 "自动修改" 已经创建的Ja ...
- C#和Java在语法上的差异(原创,持续更新中)
1.switch C#一直支持String类型 Java直到1.7才支持 2.C#里String有Length属性 Java里是Length方法 3.C#中修饰class的sealed效果与Java ...
- leetcode126 Word Ladder II
思路: 宽搜过程中分层记录路径,递归还原.实现: class Solution { public: void getPath(string now, string beginWord, string ...
- ADPU 大全
APDU协议 APDU协议,即是智能卡与读写器间的应用层协议,在ISO7816-4[7]中定义了该协议的结构格式.APDU数据有两种结构,读写器使用的APDU结构为命令APDU,C-APDU(Comm ...
- QT开发之旅-Udp聊天室编程
一.概要设计 登录对话框(继承自QDialog类)进行用户登录查询数据库用户是否存在,注册插入数据到用户表.用户表字段: (chatid int primary key, passwd varchar ...
- python学习笔记(2)——练习小程序之 " input " 隐藏陷阱
练习小程序之 ----------" input " 隐藏陷阱 age=input('please enter your age:') if age>=18: print(' ...