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

0
9
999999999
1000000000
-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:

.

题目:一个矩阵快速幂就可以

代码示例:

#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e4;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; ll n;
struct mat
{
ll a[2][2];
}; mat mul(mat A, mat B){
mat r;
memset(r.a, 0, sizeof(r.a)); for(ll i = 0; i < 2; i++){
for(ll j = 0; j < 2; j++){
for(ll k = 0; k < 2; k++){
r.a[i][j] += (A.a[i][k]*B.a[k][j])%mod;
r.a[i][j] %= mod;
}
}
}
return r;
} mat qpow(mat A, ll x){
mat B;
B.a[0][0] = B.a[1][1] = 1; // 单位矩阵
B.a[0][1] = B.a[1][0] = 0; while(x){
if (x&1) B = mul(B, A);
A = mul(A, A);
x >>= 1;
}
return B;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); while(~scanf("%lld", &n)){
if (n == -1) break; mat a;
a.a[0][0] = a.a[0][1] = a.a[1][0] = 1;
a.a[1][1] = 0; if (n == 0) printf("0\n");
else if (n == 1) printf("1\n");
else {
a = qpow(a, n-1);
printf("%d\n", a.a[0][0]%mod);
} }
return 0;
}

Fibnoccia 数列简单题的更多相关文章

  1. acm.njupt 1001-1026 简单题

    点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...

  2. NYOJ 821 简单求值【简单题】

    /* 解题人:lingnichong 解题时间:2014.10.18   00:46 解题体会:简单题 */ 简单求值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 ...

  3. 【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题

    Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下:有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪 ...

  4. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  5. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  6. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  7. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  8. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  9. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

随机推荐

  1. 【t085】Sramoc问题

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] Sramoc(K,M)表示用数字0,1,2,...,K-1组成的自然数中能被M整除的最小数.给定K,M ...

  2. grep简介

    grep -ril "xxxxx" .   :查看当前目录下所有文件是否包含指定字符,只输出符合条件的文件名 -r :递归查找 -i:忽略大小写 -l :只输出文件名

  3. linux 禁止所有中断

    如果你需要禁止所有中断如何? 在 2.6 内核, 可能关闭在当前处理器上所有中断处理, 使用任一个下面 2 个函数(定义在 <asm/system.h>): void local_irq_ ...

  4. jQuery 工具类函数-字符串操作函数

    调用名为$.trim的工具函数,能删除字符串中左右两边的空格符,但该函数不能删除字符串中间的空格,调用格式为: $.trim (str); 参数str表示需要删除左右两边空格符的字符串. <bo ...

  5. 2018-2-13-win10-uwp-从Type使用构造

    title author date CreateTime categories win10 uwp 从Type使用构造 lindexi 2018-2-13 17:23:3 +0800 2018-2-1 ...

  6. Resharper 去掉注释拼写

    最近在 Resharper 的 2018.2.1 的版本,提供了单词拼写功能,如果自己写错了单词,可以在 Resharper 提示 Resharper 的拼写 在 Resharper 的 2018.2 ...

  7. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  8. 数据预处理以及探索性分析(EDA)

    1.根据某个列进行groupby,判断是否存在重复列. # Count the unique variables (if we got different weight values, # for e ...

  9. python 练习题3

    # 计算阶乘def factorial(n): if n>2: return n*factorial(n-1) else: return nprint(factorial(2))print(fa ...

  10. 将 Sidecar 容器带入新的阶段

    作者 | 徐迪.张晓宇 导读:本文根据徐迪和张晓宇在 KubeCon NA 2019 大会分享整理.分享将会从以下几个方面进行切入:首先会简单介绍一下什么是 Sidecar 容器:其次,会分享几个阿里 ...