Fibonacci Numbers
Fibonacci Numbers |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
| Total Submission(s): 81 Accepted Submission(s): 46 |
|
Problem Description
The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively zero and one.
What is the numerical value of the nth Fibonacci number? |
|
Input
For each test case, a line will contain an integer i between 0 and 108 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”).
There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF). |
|
Sample Input
0 |
|
Sample Output
0 |
|
Source
IPCP 2005 Northern Preliminary for Northeast North-America
|
|
Recommend
lcy
|
/*
题意:求第n个斐波那契数列的值,只需要前四位,后四位 初步思路:后四位好说,膜一下就行了重要的就是前四位.总共1e8的时间,感觉用大数爆都会超时 #补充:后四位矩阵膜10000就行了,前四位可以用通项公式取对数的方法求。 */
#include<bits/stdc++.h>
#define ll long long
#define mod 10000
using namespace std;
/********************************矩阵模板**********************************/
class Matrix {
public:
int a[][]; void init(int x) {
memset(a,,sizeof(a));
if (x)
for (int i = ; i < ; i++)
a[i][i] = ;
} Matrix operator +(Matrix b) {
Matrix c;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(int x) {
Matrix c = *this;
for (int i = ; i < ; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.init();
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k < ; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power_1(int t) {
Matrix Frist,p = *this;
Frist.init();
while (t) {
if (t & )
Frist=Frist*p;
p = p*p;
t >>= ;
}
return Frist;
} Matrix power_2(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=a*b;
}
a=a*a;
x>>=;
}
return b;
}
};
/********************************矩阵模板**********************************/
Matrix unit,init;
ll f[];
ll n;
int main(){
// freopen("in.txt","r",stdin);
f[]=;
f[]=;
for(int i=;i<;i++){
f[i]=f[i-]+f[i-];
}
while(scanf("%lld",&n)!=EOF){
if(n<){
printf("%lld\n",f[n]);
continue;
}
unit.a[][]=;
unit.a[][]=;
unit.a[][]=;
unit.a[][]=; init.a[][]=;
init.a[][]=;
init.a[][]=;
init.a[][]=; init=init.power_1(n-);//有问题
unit=unit*init;
int Last=unit.a[][]; long double Frist=-0.5 * log(5.0) / log(10.0) + ((long double)n) * log((sqrt(5.0)+1.0)/2.0) / log(10.0);
Frist-=floor(Frist);
Frist=pow(,Frist);
while(Frist<)
Frist*=;
printf("%d...%04d\n",(int) Frist,Last);
}
return ;
}
Fibonacci Numbers的更多相关文章
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- Codeforces446C - DZY Loves Fibonacci Numbers
Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- HDU 3117 Fibonacci Numbers(矩阵)
Fibonacci Numbers [题目链接]Fibonacci Numbers [题目类型]矩阵 &题解: 后4位是矩阵快速幂求,前4位是用log加Fibonacci通项公式求,详见上一篇 ...
- [CodeForces - 447E] E - DZY Loves Fibonacci Numbers
E DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...
随机推荐
- [解读REST] 4.基于网络应用的架构风格
上篇文章介绍了一组自洽的术语来描述和解释软件架构:如何利用架构属性评估一个架构风格:以及对于基于网络的应用架构来说,那些架构属性是值得我们重点关注评估的.本篇在以上的基础上,列举一下一些常见的(RES ...
- windows 下面安装gcc
0.环境说明: win7 家庭版64位 1.下载编译器 https://sourceforge.net/projects/mingw/?source=typ_redirect 如图所示: 注意,安装的 ...
- Safe Area Layout Guide
原文:Safe Area Layout Guide Apple在iOS 7中引入了topLayoutGuide和bottomLayoutGuide作为UIViewController属性.它们允许您创 ...
- BackTrack 5无线网卡混杂模式设置
用ifconfig查看网络设备 主机无线网卡名称一般为wlan0,USB网卡一般为wlan1 虚拟机中USB网卡一般无法自动识别,可以用ifconfig wlan1 up启用 用ifconfig wl ...
- css之outline实现圆角效果
对,你没有看错,outline也可以实现圆角,不过请先读完自然会知道结果: ============================================================== ...
- 关于SSH
SSH的英文全称是Secure Shell. 传统的网络服务程序,如:ftp和telnet在本质上都是不安全安全安全安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令 ...
- spring web.xml配置
<!--推荐使用此种方式--> <listener> <listener-class> org.springframework.web.context.Conte ...
- 【Spring】面向切面之AOP
前言 前面已经讲解了bean的装配技术,接着学习Spring中另外一个核心概念:切面. 面向切面 面向切面编程 切面能够帮助模块化横切关注点,横切关注点可以被描述为影响应用的功能,如为业务添加安全和事 ...
- jdk8与jdk9的共存
以前安装JDK,需要手动配置环境变量.JDK8多了自动配置环境变量,所以可以不用手动配置. 如果我已经装了JDK8,还想再装一个JDK9,安装完,自动配置的环境变量会指向JDK9版本. 解决方法 删除 ...
- mysql explain 分析sql语句
鉴于最近做的事情,需要解决慢sql的问题,现补充一点sql语句性能分析之explain的使用方式! 综合返回数据情况,分析各个参数,可以了解sql 使用方法:explain + sql语句 如 :e ...