UVA 10689 Yet another Number Sequence
来源:互联网 发布:windows的运行在哪里 编辑:程序博客网 时间:2024/06/11 05:32
Input: standard input
Output: standard output
Time Limit: 3 seconds
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 Output for Sample Input
4
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4
89
4296
7711
946
Problem setter: Sadrul Habib Chowdhury
Special Thanks: Derek Kisman, Member of Elite Problem Setters’ Panel
#include <cstdio>#include <cstring>const int N = 2;int MOD;struct Matrix{int ary[N][N];void init() {memset(ary, 0, sizeof(ary));}Matrix() {init();}};const Matrix operator*(const Matrix & A, const Matrix & B) {Matrix t;for (int i = 0; i < N; ++i)for (int j = 0; j < N; ++j) {for (int k = 0; k < N; ++k)t.ary[i][j] += A.ary[i][k] * B.ary[k][j];t.ary[i][j] %= MOD;}return t;}int quick_pow(int a, int b, int n) {if (n == 0) return a % MOD;if (n == 1) return b % MOD;Matrix ans, tmp;tmp.ary[0][0] = 1;ans.ary[0][0] = (a + b) % MOD;for (int i = 0; i < N; ++i) {tmp.ary[i][1 - i] = 1;ans.ary[i][1 - i] = b % MOD;}n -= 2;while (n) {if (n & 1)ans = ans * tmp;n >>= 1;tmp = tmp * tmp;}return ans.ary[0][0];}int main() {int T;scanf("%d", &T);while (T--) {int a, b, n, m;scanf("%d%d%d%d", &a, &b, &n, &m);MOD = 1;while (m--) MOD *= 10;printf("%d\n", quick_pow(a, b, n));}return 0;}
- UVa 10689 - Yet another Number Sequence
- UVA 10689 Yet another Number Sequence
- UVA 10689 Yet another Number Sequence
- Yet another Number Sequence UVA
- uva 10689 - Yet another Number Sequence(缓存,类Fibonacci)
- UVA 10689 - Yet another Number Sequence(矩阵快速幂)
- uva 10689 - Yet another Number Sequence(矩阵快速幂)
- uva 10689 Yet another Number Sequence【矩阵快速幂】
- UVA 10689 Yet another Number Sequence(Fibonacci数列)
- 【矩阵快速幂】UVA 10698 G - Yet another Number Sequence
- UVA 10698 Yet another Number Sequence 矩阵快速幂
- uva10689 Yet another Number Sequence
- UVA10689-Yet another Number Sequence
- Yet Another Number Sequence CodeForces
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- Codeforces 392C Yet Another Number Sequence
- Codeforces 392C Yet Another Number Sequence
- 数论基础:矩阵快速幂解斐波那契数列Yet another Number Sequence(UVA)
- 我的博客第一发--qt实现科学计算器
- iOS开发 -UIScrollView详解
- Android网络访问Post请求的两种写法
- android工程eclipse的alt+/提示键在编写xml文件时没有提示解决方法
- hadoop、hbase、hive、zookeeper版本对应关系
- UVA 10689 Yet another Number Sequence
- android init.rc文件语法详解
- javaScript对象
- hdu 5381 莫队...
- hdu 1181 变形课(floyed算法)
- X86模拟器加速器的使用
- 白盒测试
- ruby应用里添加zepto.min.js但是无法使用里面的方法
- POJ 4045Power Station(据说是:树形dp)