Fibnoccia 数列简单题
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 数列简单题的更多相关文章
- acm.njupt 1001-1026 简单题
点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...
- NYOJ 821 简单求值【简单题】
/* 解题人:lingnichong 解题时间:2014.10.18 00:46 解题体会:简单题 */ 简单求值 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 ...
- 【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题
Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下:有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪 ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
随机推荐
- Python--day41--thread1.join()
在 Python 的多线程编程中,在实例代码中经常有 thread1.join()这样的代码.那么今天咱们用实际代码来解释一下 join 函数的作用. join的原理就是依次检验线程池中的线程是否结束 ...
- java 泛型的上限与下限
设置泛型对象的上限使用extends,表示参数类型只能是该类型或该类型的子类: 声明对象:类名<? extends 类> 对象名 定义类:类名<泛型标签 extends 类>{ ...
- Vue.js 学习笔记 第7章 组件详解
本篇目录: 7.1 组件与复用 7.2 使用props传递数据 7.3 组件通讯 7.4 使用slot分发内容 7.5 组件高级用法 7.6 其他 7.7 实战:两个常用组件的开发 组件(Compon ...
- 【21.58%】【codeforces 746D】Green and Black Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- H3C查看CF卡内的文件
查看CF卡内的文件 <H3C>dir //查看文件及目录文件 Directory of cf:/ -------------查看的是CF卡的内容 0 ...
- Linux内核接口特定的类型
内核中一些通常使用的数据类型有它们自己的 typedef 语句, 因此阻止了任何移植性问 题. 例如, 一个进程标识符 ( pid ) 常常是 pid_t 而不是 int. 使用 pid_t 屏蔽了任 ...
- 【p082】排座椅
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 上课的时候总有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了 ...
- 【t068】智慧碑
Time Limit: 1 second Memory Limit: 128 MB [问题描述] DIABLO魔王和Mini都有三种属性,体力点,攻击点,以及集气点. 两人的攻击方式是这样的:采用回合 ...
- vue-learning:32 - component - 异步组件和工厂函数
异步组件 只有在这个组件需要使用的时候才从服务器加载这一个组件模块,用于渲染,并且会把结果缓存起来供未来复用. 实现方法: 组件定义的时候,以一个工厂函数的形式传入,在需要组件的执行这个函数,然后将组 ...
- 看到两道小学数学题,实在是解不动,用js写了一下
把一个自然数的约数(除去它本身)按照从小到大的顺序写在它的左边,可以得到一个多位数,比如6的约数是1,2,3,写成一个多位数是1236,假如这个多位数中,没有直复数字,那么我们你这个多位数是唯一的.请 ...