Hat's Fibonacci(大数加法+直接暴力)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250
hdu1250:
Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9442 Accepted Submission(s):
3096
previous two members the sequence, with the first two members being both
1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) +
F(n-3) + F(n-4)
Your task is to take a number as input, and print that
Fibonacci number.
file.
Note:
No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
题解,同一般的斐波那契数列来说,最先的我想到了用大数加法和快速幂矩阵的方法来做,但是由于大数乘法的模板是O(n^2)所以超时了,所以考虑只用大数加法的直接暴力方法,由于斐波那契数列增长很快,所以位数不超过2005的前提下最多n 不超过10000,然后就过了,希望看到的大神吗也可以帮我提供一个复杂度小的大数乘法的模板,万分感激
下面给出ac代码,和用矩阵快速幂tle的代码:
ac代码:
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
string ms1, ms2, ms3, ms4, ms;
int main()
{
int n;
while(~scanf("%d",&n))
{
ms1 = ms2 = ms3 = ms4 = "";
if(n <= ) {puts(""); continue;}
for(int i = ; i < n-; i++)
{
ms = add(ms1, ms2);
ms = add(ms, ms3);
ms = add(ms, ms4);
ms1 = ms2;
ms2 = ms3;
ms3 = ms4;
ms4 = ms;
}
cout << ms << endl;
}
return ;
}
矩阵快速幂tle代码:
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
string mul(string s1,string s2)
{
string ans = "";
int c = ;
for(int i = s1.length()-; i >= ; i--)
{
//计算s1[i]*s2,结果保存在tem中
string tem = "";
int x = s1[i] - '',k = ;//乘数和初始余数
for(int j = s2.length()-; j >= ; j--)
{
int y = s2[j] - '';
int d = x*y+k;
tem = char(d% + '') + tem;
k = d/;
}
if(k)
tem = char(k+'') + tem;
for(int h = ; h < c; h++)
tem = tem+'';
c++;
//tem计算完毕
ans = add(ans,tem);
}
return ans;
}
struct mlt{
string a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4;
void out (){
printf("%s %s %s %s\n", a1.c_str(), a2.c_str(), a3.c_str(), a4.c_str());
printf("%s %s %s %s\n", b1.c_str(), b2.c_str(), b3.c_str(), b4.c_str());
printf("%s %s %s %s\n", c1.c_str(), c2.c_str(), c3.c_str(), c4.c_str());
printf("%s %s %s %s\n", d1.c_str(), d2.c_str(), d3.c_str(), d4.c_str());
}
mlt operator * (const mlt m) const
{
mlt tm;
tm.a1 = add(add(add(mul(a1,m.a1),mul(a2,m.b1)),mul(a3,m.c1)),mul(a4,m.d1));
tm.a2 = add(add(add(mul(a1,m.a2),mul(a2,m.b2)),mul(a3,m.c2)),mul(a4,m.d2));
tm.a3 = add(add(add(mul(a1,m.a3),mul(a2,m.b3)),mul(a3,m.c3)),mul(a4,m.d3));
tm.a4 = add(add(add(mul(a1,m.a4),mul(a2,m.b4)),mul(a3,m.c4)),mul(a4,m.d4));
tm.b1 = add(add(add(mul(b1,m.a1),mul(b2,m.b1)),mul(b3,m.c1)),mul(b4,m.d1));
tm.b2 = add(add(add(mul(b1,m.a2),mul(b2,m.b2)),mul(b3,m.c2)),mul(b4,m.d2));
tm.b3 = add(add(add(mul(b1,m.a3),mul(b2,m.b3)),mul(b3,m.c3)),mul(b4,m.d3));
tm.b4 = add(add(add(mul(b1,m.a4),mul(b2,m.b4)),mul(b3,m.c4)),mul(b4,m.d4));
tm.c1 = add(add(add(mul(c1,m.a1),mul(c2,m.b1)),mul(c3,m.c1)),mul(c4,m.d1));
tm.c2 = add(add(add(mul(c1,m.a2),mul(c2,m.b2)),mul(c3,m.c2)),mul(c4,m.d2));
tm.c3 = add(add(add(mul(c1,m.a3),mul(c2,m.b3)),mul(c3,m.c3)),mul(c4,m.d3));
tm.c4 = add(add(add(mul(c1,m.a4),mul(c2,m.b4)),mul(c3,m.c4)),mul(c4,m.d4));
tm.d1 = add(add(add(mul(d1,m.a1),mul(d2,m.b1)),mul(d3,m.c1)),mul(d4,m.d1));
tm.d2 = add(add(add(mul(d1,m.a2),mul(d2,m.b2)),mul(d3,m.c2)),mul(d4,m.d2));
tm.d3 = add(add(add(mul(d1,m.a3),mul(d2,m.b3)),mul(d3,m.c3)),mul(d4,m.d3));
tm.d4 = add(add(add(mul(d1,m.a4),mul(d2,m.b4)),mul(d3,m.c4)),mul(d4,m.d4));
return tm;
}
};
string qkmul(int n)
{
mlt flag;
flag.a1 = flag.a2 = flag.a3 = flag.a4 = flag.b1 = flag.c2 = flag.d3 ="";
flag.b2 = flag.b3 = flag.b4 = flag.c1 = flag.c3 = flag.c4 = flag.d1 = flag.d2 = flag.d4 = "" ;
n = n-;
mlt ret; ret.a1 = ret.b2 = ret.c3 = ret.d4 = "";
ret.a2 = ret.a3 = ret.a4 = ret.b1 = ret.b3 = ret.b4 = ret.c1 = ret.c2 = ret.c4 = ret.d1 = ret.d2 = ret.d3 = "";
while(n!=)
{
if(n%==)
ret = ret*flag;
n/=;
flag = flag*flag;
}
//flag.out();
flag = ret;
string ss = "";
ss = add(add(add(flag.a1,flag.a2),flag.a3),flag.a4);
return ss;
} int main()
{
int n;
while(~scanf("%d",&n))
{
string sss = qkmul(n);
cout<<sss<<endl;
}
return ;
}
注明: 一般的斐波那契在n不是很大的情况可以直接用暴力的方法就可以解决了。
Hat's Fibonacci(大数加法+直接暴力)的更多相关文章
- HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- 【hdoj_1250】Hat's Fibonacci(大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1250 思路:本题的Fibonacci数列是扩展的四阶的Fibonacci数列,用递推关系式求解即可. 题目 ...
- HDU 1250 Hat's Fibonacci (递推、大数加法、string)
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Hat's Fibonacci(大数,好)
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1250 Hat's Fibonacci(大数相加)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1250 Hat's Fibonacci
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...
- HDUOJ----1250 Hat's Fibonacci
Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU——1715大菲波数(大数加法)
大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- UVa 495【大数加法】
UVa 495 求第n位斐波那契数列,n<=5000. 还是大数问题,这次是大数加法.仿照UVa 623的解法来做.623位数可以一位一位的增,但是这个需要预先给够位数,要是按六位存一个数组元素 ...
随机推荐
- html查看器android
1.android的API提供了访问网络的一个类HttpURLConnection 2.通过发送GET请求获取服务器返回的html代码 3.先看看布局文件,如下所示, <?xml version ...
- bzoj 3669: [Noi2014] 魔法森林 LCT版
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- whereis 命令详解
whereis 作用:whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 参数:-b 定位可 ...
- [摘抄]VC6.0移植到VS2008(vs2005)后的错误总结(未全部验证)
============================================================================================= 201405 ...
- MySQL如何找到表与表之间的关系?
如何找到两张表之间的关系? 先站在左表的角度上去找,如果可以找到左表的多个字段可以对应右表的一个字段,那么左表的一个字段foregin key右表的一个字段.一般情况下为id... 2.如果右表的多个 ...
- 最新swift4.0 图片进行尺寸大小及体积压缩
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Menlo; color: #4dbf56; background-color: #282b3 ...
- 初学Python之 布尔类型
与运算:只有两个布尔值都为 True 时,计算结果才为 True. True and True # ==> True True and False # ==> False False an ...
- eclipse导入工程时,出现Some projects cannot be imported because they already exist in the workspace
前提条件: 1.将eclipse中现有的工程javatraining删除,如下图所示, 该复选框不要勾选,点击ok steps: 1.本次打开Eclipse想要把已删除的javatraining工程再 ...
- tcp链接断开的探测
有资料说,read.write都可以探测tcp的断开,但都不是实时的. 但是实际在某些设备上测试发现,即使开一个线程每隔一小段时间发一次心跳包(write),write也不能探测连接已经断开,而且这个 ...
- MVC异步加载学习笔记
一.普通的异步请求 1.View层 <input type="text" id="Name" value="" placeholder ...