HDU 1041 Computer Transformation (简单大数)
2013-07-17 07:29
302 查看
Computer Transformation
http://acm.hdu.edu.cn/showproblem.php?pid=1041[align=left]Problem Description[/align]
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
How many pairs of consequitive zeroes will appear in the sequence after n steps?
[align=left]Input[/align]
Every input line contains one natural number n (0 < n ≤1000).
[align=left]Output[/align]
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
[align=left]Sample Input[/align]
2
3
[align=left]Sample Output[/align]
1
1
解题思路:找出规律,F
= F[n-2] + 2n-3, 不过不能直接一起算,因为可能会超时;我们可以这样F
= F[n-2] + G[n-3]; (其中F[]代表多少对0,G[]代表有多少个1);
G
= G[n-1] + G[n-1];所以这里就只需要大数加法就够了!
解题代码:
#include <stdio.h> #include <iostream> #include <math.h> #include <string.h> #define CLE(name) memset(name, 0, sizeof (name)) using namespace std; typedef __int64 LL; const int max_n = 1002; string F[2*max_n]; string P2[2*max_n]; int num1[max_n], num2[max_n]; string add(string a, string b) { CLE(num1); CLE(num2); int len1 = a.length(); int len2 = b.length(); int k = 0; for (int i = len1 - 1; i >= 0; i --) { num1[k++] = a[i] - '0'; } k = 0; for (int i = len2 - 1; i >= 0; i --) { num2[k++] = b[i] - '0'; } int up = 0; for (int i = 0; i < max_n/2; i ++) { num1[i] = num1[i] + num2[i] + up; up = num1[i]/10; num1[i] %= 10; } for (k = max_n - 1; k >= 0; k --) { if (num1[k] != 0) break; } a = ""; for (; k >= 0; k --) a += num1[k] + '0'; return a; } void pow2() { P2[0] = "1"; P2[1] = "2"; for (int i = 2; i <= 1000; i ++) { P2[i] = add(P2[i-1], P2[i-1]); } } void deel() { F[0] = "0"; F[1] = "0"; F[2] = "1"; for (int i = 3; i <= max_n; i ++) { F[i] = add(F[i-2], P2[i-3]); } return; } int main() { int n; pow2(); deel(); while (~scanf ("%d", &n)) { cout << F << endl; } return 0; }
View Code
相关文章推荐
- HDU 1715 大菲波数(JAVA, 简单题,大数)
- hdu 1041 Computer Transformation(规律+大数)
- HDU 1041 递推 + 大数模板
- 简单的大数运算:HDU1042,1047
- hdu 1001 Java大数简单方法
- HDU 1753 简单大数
- HDU 1212 Big Number(简单的大数取模)
- hdu 1041(递推,大数)
- hdu 1041(规律+打表+java大数)
- hdu 4523 威威猫系列故事——过生日(简单大数相加比较)
- hdu_1041(Computer Transformation) 大数加法模板+找规律
- hdu 1041 计算机二进制n步操作(大数加法)
- HDU 1041(递推+大数)
- hdu 4523 威威猫系列故事——过生日(简单大数相加比较)
- HDU-1041 Computer Transformation 大数
- HDU 1316 How Many Fibs?(java,简单题,大数)
- hdu1041 Computer Transformation 大数找规律
- Hdu 大菲波数 简单大数相加
- hdu 的一个简单的大数加法
- hdu 1041大数问题