您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: