Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7715   Accepted: 5474

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

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:

.

Source

 
想学高斯消元,先学矩阵。
二分思想,在A^B mod m中已经体现..
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> struct node
{
int a[][];
}now,cur; void make_first()
{
now.a[][]=;
now.a[][]=;
now.a[][]=;
now.a[][]=; cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
} struct node make_cheng(node a,node b) //发现结构体忘记了。
{
struct node f;
int i,k,j;
memset(f.a,,sizeof(f.a));
for(i=;i<=;i++)
for(k=;k<=;k++)
if(a.a[i][k])
{
for(j=;j<=;j++)
if(b.a[k][j])
{
f.a[i][j]+=a.a[i][k]*b.a[k][j];
if(f.a[i][j]>=)
f.a[i][j]%=;
}
}
return f;
} void make_EF(int n)
{
make_first();
while(n)
{
if(n&)
{
now=make_cheng(now,cur);//!!!
}
n=n/;
cur=make_cheng(cur,cur);
}
printf("%d\n",now.a[][]);
} int main()
{
int n;
while(scanf("%d",&n)>)
{
if(n==-)break;
if(n==){printf("0\n");continue;}
if(n==||n==){printf("1\n");continue;}
make_EF(n-);
}
return ;
}

poj 3070 Fibonacci 矩阵相乘的更多相关文章

  1. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  2. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  3. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. POJ 3070 Fibonacci 矩阵高速求法

    就是Fibonacci的矩阵算法.只是添加一点就是由于数字非常大,所以须要取10000模,计算矩阵的时候取模就能够了. 本题数据不强,只是数值本来就限制整数,故此能够0ms秒了. 以下程序十分清晰了, ...

  5. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  6. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  7. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  8. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  9. 题解报告:poj 3070 Fibonacci

    题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...

随机推荐

  1. ie 8及以下 前端cors ajax跨域须知

    http://www.cnblogs.com/xishuai/p/jquery-ajax-ie8-cors.html

  2. java使用Redis3--完整模板类

    Redis全部指令请参考:http://www.runoob.com/redis/redis-tutorial.html 对应的java模板类 package com.d.work.redis; im ...

  3. Java中将图片保存到数据库中

    在实际的开发中,我们可能需要将图片.影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用.例如将读出的图片数据显示出来,将读出的电影文件播放出来. 二进制数据直接保存到文件和从文件中读出非 ...

  4. 负载均衡群集LB

    负载均衡群集是目前企业用得最多的群集类型,通过主节点负载调度器(Director),使用特定的分流算法,将来自客户机的访问请求分担给多个服务器节点共同处理,从而缓解整个系统的负载压力,响应更多请求:群 ...

  5. Python 魔法方法查询表 -- 总结篇

    据说,Python 的对象天生拥有一些神奇的方法,它们总被双下划线所包围,他们是面向对象的 Python 的一切. 他们是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个, ...

  6. HDU-1087-Super Jumping! Jumping! Jumping!(线性DP, 最大上升子列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. Win10将用户名修改为英文

    前言 最近重装了一次Win10系统,手贱地在引导里设置了中文的用户名.使用微软账户进行登录后,Win10以这个中文名建立了用户的文件夹,使得少数软件安装或使用过程中,保存路径不支持中文路径,从而报错, ...

  8. 42.oracle物化视图

    写在前面 先大概列一下数据库表设计的常规流程.方案.要遵循的规则 根据业务切分设计表 逻辑分层(数据库分层) 数据库结构设计与拆分:水平拆分(mysql分片)oracle分区物化视图中间表设计方案 优 ...

  9. 前端知识总结--BFC

    Block Formatting Context,中文直译为块级格式上下文. 1. BFC的定义 是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和 ...

  10. js 移动端获取当前用户的经纬度

    一.HTML5 geolocation的属性 if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(onSuccess ...