博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fibnoccia 数列简单题
阅读量:6605 次
发布时间:2019-06-24

本文共 2150 字,大约阅读时间需要 7 分钟。

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
0346266875
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 longconst 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;}

 

转载于:https://www.cnblogs.com/ccut-ry/p/8973153.html

你可能感兴趣的文章
JS模拟select下拉菜单
查看>>
线性方程组迭代求解——Jacobi迭代算法(Python实现)
查看>>
vmware workstation14永久激活密钥分享
查看>>
(旧)子数涵数·PS——水杯抠图
查看>>
java的Reference学习
查看>>
远程连接服务端电脑mysql数据库
查看>>
java虚拟机知识和 内存 堆(heap)、栈(stack)和方法区(method)
查看>>
Windows 64位操作系统下安装和配置MySQL
查看>>
Java Swing3-MyDialog的基本实现
查看>>
生成器读取大文件应用
查看>>
垃圾回收监视和分析
查看>>
sublime安装sftp和ctags插件
查看>>
使用 openssl 生成证书
查看>>
如何在CI中写工具类,在哪个目录写
查看>>
SQLAlchemy的使用---增删改查
查看>>
C#和JavaScript交互(asp.net前台和后台互调)总结 (转)
查看>>
(转载)身份证验证
查看>>
Access数据库大数据量的相关测试分析
查看>>
ios中调用友盟分享时qq可以分享但是微信失败,只显示文字,网页链接没有出现...
查看>>
HDU 3954 Level up(多颗线段树+lazy操作)
查看>>