hdu 1715 大菲波数
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1715
大菲波数
Description
$Fibonacci$数列,定义如下:
$f(1)=f(2)=1$
$f(n)=f(n-1)+f(n-2) \ \ 3 \leq n$。
计算第$n$项$Fibonacci$数值。
Input
输入第一行为一个整数$N$,接下来$N$行为整数$Pi(1 \leq P_i \leq 1000)$。
Output
输出为$N$行,每行为对应的$f(P_i)$。
SampleInput
5
1
2
3
4
5
SampleOutput
1
1
2
3
5
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cassert> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<set> 11 using std::cin; 12 using std::max; 13 using std::cout; 14 using std::endl; 15 using std::map; 16 using std::string; 17 using std::istream; 18 using std::ostream; 19 #define sz(c) (int)(c).size() 20 #define all(c) (c).begin(), (c).end() 21 #define iter(c) decltype((c).begin()) 22 #define cls(arr,val) memset(arr,val,sizeof(arr)) 23 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 24 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 25 #define fork(i, k, n) for (int i = (int)k; i <= (int)n; i++) 26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 27 #define pb(e) push_back(e) 28 #define mp(a, b) make_pair(a, b) 29 struct BigN { 30 typedef unsigned long long ull; 31 static const int Max_N = 2010; 32 int len, data[Max_N]; 33 BigN() { memset(data, 0, sizeof(data)), len = 0; } 34 BigN(const int num) { 35 memset(data, 0, sizeof(data)); 36 *this = num; 37 } 38 BigN(const char *num) { 39 memset(data, 0, sizeof(data)); 40 *this = num; 41 } 42 void clear() { len = 0, memset(data, 0, sizeof(data)); } 43 BigN& clean(){ while (len > 1 && !data[len - 1]) len--; return *this; } 44 string str() const { 45 string res = ""; 46 for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0'); 47 if (res == "") res = "0"; 48 res.reserve(); 49 return res; 50 } 51 BigN operator = (const int num) { 52 int j = 0, i = num; 53 do data[j++] = i % 10; while (i /= 10); 54 len = j; 55 return *this; 56 } 57 BigN operator = (const char *num) { 58 len = strlen(num); 59 for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0'; 60 return *this; 61 } 62 BigN operator + (const BigN &x) const { 63 BigN res; 64 int n = max(len, x.len) + 1; 65 for (int i = 0, g = 0; i < n; i++) { 66 int c = data[i] + x.data[i] + g; 67 res.data[res.len++] = c % 10; 68 g = c / 10; 69 } 70 return res.clean(); 71 } 72 BigN operator * (const BigN &x) const { 73 BigN res; 74 int n = x.len; 75 res.len = n + len; 76 for (int i = 0; i < len; i++) { 77 for (int j = 0, g = 0; j < n; j++) { 78 res.data[i + j] += data[i] * x.data[j]; 79 } 80 } 81 for (int i = 0; i < res.len - 1; i++) { 82 res.data[i + 1] += res.data[i] / 10; 83 res.data[i] %= 10; 84 } 85 return res.clean(); 86 } 87 BigN operator * (const int num) const { 88 BigN res; 89 res.len = len + 1; 90 for (int i = 0, g = 0; i < len; i++) res.data[i] *= num; 91 for (int i = 0; i < res.len - 1; i++) { 92 res.data[i + 1] += res.data[i] / 10; 93 res.data[i] %= 10; 94 } 95 return res.clean(); 96 } 97 BigN operator - (const BigN &x) const { 98 assert(x <= *this); 99 BigN res; 100 for (int i = 0, g = 0; i < len; i++) { 101 int c = data[i] - g; 102 if (i < x.len) c -= x.data[i]; 103 if (c >= 0) g = 0; 104 else g = 1, c += 10; 105 res.data[res.len++] = c; 106 } 107 return res.clean(); 108 } 109 BigN operator / (const BigN &x) const { 110 BigN res, f = 0; 111 for (int i = len - 1; ~i; i--) { 112 f *= 10; 113 f.data[0] = data[i]; 114 while (f >= x) { 115 f -= x; 116 res.data[i]++; 117 } 118 } 119 res.len = len; 120 return res.clean(); 121 } 122 BigN operator % (const BigN &x) { 123 BigN res = *this / x; 124 res = *this - res * x; 125 return res; 126 } 127 BigN operator += (const BigN &x) { return *this = *this + x; } 128 BigN operator *= (const BigN &x) { return *this = *this * x; } 129 BigN operator -= (const BigN &x) { return *this = *this - x; } 130 BigN operator /= (const BigN &x) { return *this = *this / x; } 131 BigN operator %= (const BigN &x) { return *this = *this % x; } 132 bool operator < (const BigN &x) const { 133 if (len != x.len) return len < x.len; 134 for (int i = len - 1; ~i; i--) { 135 if (data[i] != x.data[i]) return data[i] < x.data[i]; 136 } 137 return false; 138 } 139 bool operator >(const BigN &x) const { return x < *this; } 140 bool operator<=(const BigN &x) const { return !(x < *this); } 141 bool operator>=(const BigN &x) const { return !(*this < x); } 142 bool operator!=(const BigN &x) const { return x < *this || *this < x; } 143 bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); } 144 friend istream& operator >> (istream &in, BigN &x) { 145 string src; 146 in >> src; 147 x = src.c_str(); 148 return in; 149 } 150 friend ostream& operator << (ostream &out, const BigN &x) { 151 out << x.str(); 152 return out; 153 } 154 }A[1001]; 155 inline void init() { 156 A[1] = A[2] = 1; 157 fork(i, 3, 1000) A[i] = A[i - 1] + A[i - 2]; 158 } 159 int main() { 160 #ifdef LOCAL 161 freopen("in.txt", "r", stdin); 162 freopen("out.txt", "w+", stdout); 163 #endif 164 std::ios::sync_with_stdio(false); 165 init(); 166 int t, n; 167 while (~scanf("%d", &t)) { 168 rep(i, t) { 169 scanf("%d", &n); 170 cout << A[n] << endl; 171 } 172 } 173 return 0; 174 }
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明