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
|
Sample Output
|
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 ...
随机推荐
- 比较JqGrid与XtraGrid
此只能比较两者的功能优劣,实现某种功能.效果的方便性和效率.首先分别粗略介绍XtraGrid和jqGrid DevExpress是目前.net下最为强大和完整的UI控件库, XtraGrid是这个控件 ...
- PHP实现页面静态化
1.通过buffer来实现 需要用file_put_contents ob_get_clean()等内置函数 ob_start (); include "filterpost.htm ...
- 两句话动态修改table数据并提交到后台
//为所有的input 添加click事件,我将对象的id放入到name属性中,行数放入到alt属性中 $("input").click(function(obj){ //获得当前 ...
- DOM中document对象的常用属性方法
每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1 document.anchors 返 ...
- 《HelloGitHub》第 18 期
<HelloGitHub>第 18 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程. ...
- 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---简介
这一章描述如何初始化一个新线程,如何停止一个执行中的线程,以及如何了解并调整线程优先权. 读过这一章之后,你将有能力回答一个 Win32 多线程程序设计的最基本问题.你一定曾经在 Usenet ...
- spring web.xml配置
<!--推荐使用此种方式--> <listener> <listener-class> org.springframework.web.context.Conte ...
- EnCase missed some usb activities in the evidence files
My friend is a developer and her colleague May was suspected of stealing the source code of an impor ...
- MySQL or MariaDB 错误解决方法之报错代码1045
phpMyAdmin登录报错:mysqli_real_connect(): (28000/1045): Access denied for user 'root'@'localhost' (using ...
- js循环生成多个easyui datagrid数据网格时,初始化表格
$.each( content, function(i, item){ var info_tpl = "";var result_tpl = "";var pr ...