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 ;
}
最新文章
- 【单页应用之通信机制】view之间应该如何通信
- 如何在个人博客引擎 Hexo 中添加 Swiftype 搜索组件
- MMORPG大型游戏设计与开发(客户端架构 part12 of vegine)
- NTP客户端的设置
- flex html 用flex展示html
- IE10、IE11出现“__doPostBack未定义”的解决办法。
- Spring execution 表达式
- NSString NSCFString区别
- 9.5 在 C# 中使用 F# 库
- Android 上多方式定位元素(python)
- thinkphp整合系列之短信验证码、订单通知
- sql备份(.mdf文件备份)
- 关于SpringMVC中如何把查询数据全转成String类型
- ASP.Net Core Razor 页面路由
- 使用 RxJS 实现一个简易的仿 Elm 架构应用
- Python内置函数(3)——any
- Temporal Action Detection with Structured Segment Networks (ssn)【转】
- Django集成Markdown编辑器【附源码】
- Ionic Android项目Splash设置
- HACK字体安装
热门文章
- c++中字符输入函数getline、cin.getline区分
- java中字符串编码转换
- hdoj--1754--I Hate It(线段树)
- Spring学习笔记(一) 简介
- 在eclipse环境下使用maven install 命令碰到native2ascii-utf8问题解决方案
- C# 正则表达式
- web.config or app.config 中configSections配置节点
- The Vertica Analytic Database:C-Store 7 Years Later笔记
- Java之Foreach语句
- Django中ORM之操作表记录