Poj
来源:互联网 发布:淘宝家乡版怎么切换 编辑:程序博客网 时间:2024/06/08 07:47
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
099999999991000000000-1
Sample Output
0
34
626
6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
题意
就是求Fib的第n项后四位
题解:
矩阵快速幂板子 递推构造矩阵需要学习一下
AC代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int mod = 1e4;const int N = 2;struct node { LL arr[N][N];}p;LL n, k; node mul(node x,node y){ node ans;; memset(ans.arr,0,sizeof(ans.arr)); for(int i = 0;i < 2; i++) { for(int j = 0;j < 2; j++) { for(int k = 0;k < 2; k++) { ans.arr[i][j] = (ans.arr[i][j]+(x.arr[i][k]*y.arr[k][j])%mod)%mod; } } } return ans;}node powMod(LL u, node x) { node ans; for(int i = 0;i < 2; i++) { for(int j = 0;j < 2; j++) { if(i==j) ans.arr[i][j] = 1; else ans.arr[i][j] = 0; } } while(u) { if(u&1) ans = mul(ans,x); x = mul(x,x); u >>= 1; } return ans;}int main(){ int n; while(~scanf("%d",&n),n!=-1) { p.arr[0][0] = 1; p.arr[0][1] = 1; p.arr[1][0] = 1; p.arr[1][1] = 0; node x = powMod(n,p); printf("%d\n",x.arr[0][1]); }return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Auto Ware 代码解析系列-astar_planner节点
- 凤凰视频真实视频地址解析
- R语言置换检验
- Pthreads多线程编程(2)
- 管道的原子性
- Poj
- JavaScript立即执行函数
- opnet平台中切换模块的理解——切换的建模
- 条款 49
- 获取网卡名称 linux c
- 3ds max 显示物品大纲,方便修改名称
- 12 WebGL 着色器编程语言GLSL ES的全局变量和局部变量
- CentOS 7.2 安装和配置 FTP 服务器
- 图片的上传和预览(上传到oracle数据库)