Description
给定n(N<=100),编程计算有多少个不同的n轮状病毒。
Input
第一行有1个正整数n。
Output
将编程计算出的不同的n轮状病毒数输出
Sample Input
3
Sample Output
16
很遗憾,关于递推公式的印象已经变得模糊了。总而言之上马高精度,运算符重载不敢写,直接返回速度慢但我乐意,应该直接能看代码吧。
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 1010 4 using namespace std; 5 6 struct BigInteger { 7 int a[MAXN]; 8 const static int L = 1000; 9 10 BigInteger() { 11 memset(a, 0, sizeof(a)); 12 } 13 }; 14 15 BigInteger f[1010][2]; 16 int n; 17 18 BigInteger Init(int N) { 19 BigInteger ans; 20 for (int i = 1; i <= ans.L; i++) { 21 ans.a[i] = N % 10; 22 N /= 10; 23 if (N == 0) break; 24 } 25 return ans; 26 } 27 28 BigInteger Plus(BigInteger A, BigInteger B) { 29 BigInteger ans; 30 int k = 0; 31 for (int i = 1; i <= ans.L; i++) { 32 ans.a[i] = A.a[i] + B.a[i] + k; 33 k = ans.a[i] / 10, ans.a[i] %= 10; 34 } 35 return ans; 36 } 37 38 BigInteger Minus(BigInteger A, BigInteger B) { 39 BigInteger ans; 40 for (int i = 1; i <= ans.L; i++) ans.a[i] = A.a[i]; 41 for (int i = 1; i <= ans.L; i++) { 42 ans.a[i] -= B.a[i]; 43 if (ans.a[i] < 0) { ans.a[i] += 10, ans.a[i + 1]--; } 44 } 45 return ans; 46 } 47 48 BigInteger Times(BigInteger A, int B) { 49 BigInteger ans; 50 for (int i = 1; i <= ans.L; i++) { 51 ans.a[i] += A.a[i] * B; 52 if (ans.a[i] > 9) { 53 ans.a[i + 1] += ans.a[i] / 10; 54 ans.a[i] %= 10; 55 } 56 } 57 return ans; 58 } 59 60 void Print(BigInteger A) { 61 int flag = 0; 62 for (int i = A.L; i >= 1; i--) { 63 if (flag) printf("%d", A.a[i]); 64 else if (A.a[i] > 0) { 65 printf("%d", A.a[i]); 66 flag = 1; 67 } 68 } 69 if (flag == 0) printf("0"); 70 printf(" "); 71 } 72 73 void Prepare() { 74 f[0][1] = Init(1); 75 f[1][1] = Init(1), f[1][0] = Init(1); 76 } 77 78 int main() { 79 scanf("%d", &n); 80 Prepare(); 81 for (int i = 2; i < n; i++) { 82 f[i][0] = Plus(f[i - 1][0], f[i - 1][1]); 83 f[i][1] = Plus(f[i][0], f[i - 1][1]); 84 } 85 BigInteger ans; 86 for (int i = 1; i <= n; i++) ans = Plus(ans, Times(Times(f[n - i][1], i), i)); 87 Print(ans); 88 return 0; 89 }