UVA - 10689 Yet another Number Sequence 矩阵快速幂
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
题意:
给你 f[0],f[1] 分别为A,B求F[n] % (10^m)
题解:
n有点大,矩阵快速幂
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N = + ;
const int mod = 1e9 + ;
const int M[] = {, , , , };
struct Matrix {
ll mat[][];
}U,F,L;
ll MOD;
Matrix multi (Matrix a, Matrix b) {
Matrix ans;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
ans.mat[i][j] = ;
for(int k = ; k < ; k++)
ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];
ans.mat[i][j] %= MOD;
}
}
return ans;
}
ll a,b,m;
Matrix powss(ll n) {
Matrix ans = L,p = U;
while(n) {
if(n&) ans = multi(p,ans);
n >>= ;
p = multi(p,p);
}
return ans;
}
int main() { int T;
scanf("%d",&T);
while(T--) {
ll n;
scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
U = {,,,};
L = {b,,a,};
MOD = M[m];
Matrix ans = powss(n);
printf("%lld\n",ans.mat[][]);
}
return ;
}
UVA - 10689 Yet another Number Sequence 矩阵快速幂的更多相关文章
- UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- 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 ...
- 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 ...
- 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\). ...
随机推荐
- SSD纠错码向LDPC码演变
作者:Stephen Bates SSD控制器芯片中採用的纠错编码(ECCs)的类型正在发生一场演变.相信很多这篇博文的读者对此都有所了解.传统上採用的纠错码是基于群变换的博斯-查德胡里-霍昆格母(B ...
- angularjs1-7,供应商
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 【POJ 2311】 Cutting Game
[题目链接] http://poj.org/problem?id=2311 [算法] 博弈论——SG函数 [代码] #include <algorithm> #include <bi ...
- 将maven项目中依赖的jar包导出到指定的目录
<plugin> <artifactId>maven-dependency-plugin</artifactId> <configuration> &l ...
- MVC中添加模块区域,并设置RedirectToAction跳转
废话少说,直接上图:
- js点击时关闭该范围下拉菜单之外的菜单
$(function(){ $(document).bind("click",function(e){ //id为menu的是菜单 if($(e.target).closest(& ...
- MFC+OpenGL可编程管线
[github链接] 网上的代码大都是固定管线渲染的,今天下午整理了下,把setPixelFormat.初始化glew.创建GL 4,2 context等操作封装到一个MFC类OpenGLWidget ...
- 人人都是产品经理?关于PM你不知道的还有很多
产品经理的职称最早出现在P&G宝洁公司,因效果非常显著,许多企业纷纷仿而效尤.硅谷知名的产品管理大师Marty Cagan在<Inspired: How To Create Produc ...
- Core Animation 负责将bitmap绑定提交到 GPU-[CALayer _display]
Core Animation 负责将bitmap绑定提交到 GPU: Core Animation一头连着CPU,一头连着GPU. ZSTest`-[ZSDTCoreTextCell drawRect ...
- day06-1 与用户交互以及格式化输出
目录 Python的与用户交互 Python2的input和raw_input(了解) 格式化输出 占位符 format函数格式化字符串 f-string格式化(方便) Python的与用户交互 in ...