大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了。。。。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std; /*
* 完全大数模板
* 输出cin>>a
* 输出a.print();
* 注意这个输入不能自动去掉前导0的,可以先读入到char数组,去掉前导0,再用构造函数。
多组数据输入输出时在循环里面定义变量BigNum,在循坏外定义有时会出现WA
大数的位数可以根据题目的要求进行更改
*/
#define MAXN 9999
#define MAXSIZE 1010
#define DLEN 4
class BigNum
{
private:
int a[]; //可以控制大数的位数
int len; public:
BigNum()
{
len = ; //构造函数
memset(a, , sizeof(a));
}
BigNum(const int); //将一个int类型的变量转化成大数
BigNum(const char *); //将一个字符串类型的变量转化为大数
BigNum(const BigNum &); //拷贝构造函数
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
friend istream &operator>>(istream &, BigNum &); //重载输入运算符
friend ostream &operator<<(ostream &, BigNum &); //重载输出运算符
BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
BigNum operator*(const BigNum &)const; //重载乘法运算符,两个大数之间的相乘运算
BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算
BigNum operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigNum &T) const; //大数和另一个大数的大小比较
bool operator>(const int &t) const; //大数和一个int类型的变量的大小比较
void print(); //输出大数
};
BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
{
int c, d = b;
len = ;
memset(a, , sizeof(a));
while (d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char *s) //将一个字符串类型的变量转化为大数
{
int t, k, index, L, i;
memset(a, , sizeof(a));
L = strlen(s);
len = L / DLEN;
if (L % DLEN)
len++;
index = ;
for (i = L - ; i >= ; i -= DLEN)
{
t = ;
k = i - DLEN + ;
if (k < )
k = ;
for (int j = k; j <= i; j++)
t = t * + s[j] - '';
a[index++] = t;
}
}
BigNum::BigNum(const BigNum &T) : len(T.len) //拷贝构造函数
{
int i;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = T.a[i];
}
BigNum &BigNum::operator=(const BigNum &n) //重载赋值运算符,大数之间赋值运算
{
int i;
len = n.len;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = n.a[i];
return *this;
}
istream &operator>>(istream &in, BigNum &b)
{
char ch[MAXSIZE * ];
int i = -;
in >> ch;
int L = strlen(ch);
int count = , sum = ;
for (i = L - ; i >= ;)
{
sum = ;
int t = ;
for (int j = ; j < && i >= ; j++, i--, t *= )
{
sum += (ch[i] - '') * t;
}
b.a[count] = sum;
count++;
}
b.len = count++;
return in;
}
ostream &operator<<(ostream &out, BigNum &b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for (i = b.len - ; i >= ; i--)
{
printf("%04d", b.a[i]);
}
return out;
}
BigNum BigNum::operator+(const BigNum &T) const //两个大数之间的相加运算
{
BigNum t(*this);
int i, big;
big = T.len > len ? T.len : len;
for (i = ; i < big; i++)
{
t.a[i] += T.a[i];
if (t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -= MAXN + ;
}
}
if (t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum &T) const //两个大数之间的相减运算
{
int i, j, big;
bool flag;
BigNum t1, t2;
if (*this > T)
{
t1 = *this;
t2 = T;
flag = ;
}
else
{
t1 = T;
t2 = *this;
flag = ;
}
big = t1.len;
for (i = ; i < big; i++)
{
if (t1.a[i] < t2.a[i])
{
j = i + ;
while (t1.a[j] == )
j++;
t1.a[j--]--;
while (j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while (t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if (flag)
t1.a[big - ] = - t1.a[big - ];
return t1;
}
BigNum BigNum::operator*(const BigNum &T) const //两个大数之间的相乘
{
BigNum ret;
int i, j, up;
int temp, temp1;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < T.len; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if (temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if (up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int &b) const //大数对一个整数进行相除运算
{
BigNum ret;
int i, down = ;
for (i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
int BigNum::operator%(const int &b) const //大数对一个 int类型的变量进行取模
{
int i, d = ;
for (i = len - ; i >= ; i--)
d = ((d * (MAXN + )) % b + a[i]) % b;
return d;
}
BigNum BigNum::operator^(const int &n) const //大数的n次方运算
{
BigNum t, ret();
int i;
if (n < )
exit(-);
if (n == )
return ;
if (n == )
return *this;
int m = n;
while (m > )
{
t = *this;
for (i = ; (i << ) <= m; i <<= )
t = t * t;
m -= i;
ret = ret * t;
if (m == )
ret = ret * (*this);
}
return ret;
}
bool BigNum::operator>(const BigNum &T) const //大数和另一个大数的大小比较
{
int ln;
if (len > T.len)
return true;
else if (len == T.len)
{
ln = len - ;
while (a[ln] == T.a[ln] && ln >= )
ln--;
if (ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator>(const int &t) const //大数和一个int类型的变量的大小比较
{
BigNum b(t);
return *this > b;
}
void BigNum::print() //输出大数
{
int i;
printf("%d", a[len - ]);
for (i = len - ; i >= ; i--)
printf("%04d", a[i]);
} BigNum f[ + ];
int main()
{
BigNum tmp();
f[] = tmp;
f[] = tmp;
f[] = tmp;
f[] = tmp;
for (int i = ; i < ; i++)
{
f[i] = f[i - ] + f[i - ] + f[i - ] + f[i - ];
}
int x;
while (scanf("%d", &x) != EOF)
{
f[x].print();
printf("\n");
} return ;
}

hdu Hat's Fibonacci(用了kuangbin模板)的更多相关文章

  1. hdu Hat's Fibonacci

    import java.math.BigInteger; import java.util.*; public class Main { public static void main(String ...

  2. hdu 1250 Hat's Fibonacci

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...

  3. HDU 1250 Hat's Fibonacci(大数相加)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Ot ...

  4. HDU 1250 Hat's Fibonacci (递推、大数加法、string)

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. Hat's Fibonacci(大数加法+直接暴力)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 hdu1250: Hat's Fibonacci Time Limit: 2000/1000 M ...

  6. HDUOJ----1250 Hat's Fibonacci

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. Hat's Fibonacci(大数,好)

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. hdu 5895 广义Fibonacci数列

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. (二维数组 亿进制 或 滚动数组) Hat's Fibonacci hdu1250

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. Android网络通信库Volley简介(转)

    以前反编译过android market,发现里面有用到volley,起这么个名字不知道啥用的,现在才知道主讲者Ficus Kirkpatrick 就是负责开发Google play 的. 看完视频, ...

  2. 个体商户POS机遭遇禁刷 职业养卡人称自有对策

    “套现猛于虎也”,这对于信用卡业而言无异于一大命门,信用卡套现金额的规模如同滚雪球般愈演愈烈.记者昨日采访银行业内了解到,虽然为防套现将根据规定关闭个体商户POS机刷信用卡的功能,但职业“养卡人”不以 ...

  3. Azkaban调度器

    Azkaban介绍 Azkaban 是由 Linkedin 公司推出的一个批量工作流任务调度器,用于在一个工作流内以一个特定的顺序运行一组工作和流程.Azkaban 使用 job 配置文件建立任务之间 ...

  4. linux下网络编程学习——入门实例ZZ

    http://www.cppblog.com/cuijixin/archive/2008/03/14/44480.html 是不是还对用c怎么实现网络编程感到神秘莫测阿,我们这里就要撕开它神秘的面纱, ...

  5. 页面引入(include)方式的研究及性能比较

    1. 应用Html中的框架(iframe) 目前大多数门户网站都应用iframe来进行页面上广告的投放,就是将不同的广告分别应用iframe投放到主页面上,优点是效率高,互不影响,缺点是不符合网页标准 ...

  6. request.getRequestDispatcher().forward(request.response)

    request.getRequestDispatcher().forward(request.response)中的那两个参数是哪里来的? 2010-11-09 23:13 QQ357169111 | ...

  7. Surrounded Regions [未完成]

    思路完全一样 AC的代码: class Solution { private: struct Point { int x, y; Point(int _x, int _y):x(_x), y(_y) ...

  8. AD诊断命令

    dcdiag repadmin /showrepl "启动从dc02到dc01的复制"cmd /c "repadmin /replicate dc02 dc01 dc=u ...

  9. 阿里云OSS 上传文件SDK

    Aliyun OSS SDK for C# 上传文件 另外:查找的其他实现C#上传文件功能例子: 1.WPF用流的方式上传/显示/下载图片文件(保存在数据库) (文末有案例下载链接) 2.WPF中利用 ...

  10. Hadoop学习---Hadoop的HBase的学习

    Hbase Hbase的特点: Hbase是bigtable的开源的仿制版本 建立在HDFS之上 可靠性,靠性能 大:一个表可以有上亿行,上百万列 面向列:面向列(族)的存储和权限控制,列(族)独立检 ...