zoukankan      html  css  js  c++  java
  • soj1166. Computer Transformat(dp + 大数相加)

    1166. Computer Transformat

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

     

    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?

     

    Input

    Every input line contains one natural number n (0 < n ≤1000).

    Output

    For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.

    Sample Input

    2
    3
    

    Sample Output

    1
    1

    本来还以为是一道基本的动态规划题目,结果做了一遍WA,然后仔细观察,到了1000时数太大了,long long都放不下,所以尝试大数相加,一次就过了。。

    思路:

    dp[i][0] —— 第i轮后出现多少个01

    dp[i][1] —— 第i轮后出现多少个1

    dp[0][0] = 0; dp[0][1] = 1;

    dp[1][0] = dp[1][1] = 1;

    dp[i][0] = dp[i-2][0] + dp[i-1][1];

    dp[i][1] = 2*dp[i-1][1];

    n = dp[n-1][0];

    #include <iostream>
    #include <string>
    using namespace std;
    
    string dp[1001][2];
    
    string add(string a,string b)
    {
    	string result;
    	string rr;
    	int i;
    	int l1,l2,len1,len2;
    	l1 = len1 = a.size();
    	l2 = len2 = b.size();
    	int aa,bb,cc,dd;
    	dd = 0;
    	while(l1 > 0 && l2 > 0)
    	{
    		aa = a[l1-1] - '0';
    		bb = b[l2-1] - '0';
    		cc = (aa + bb+dd) % 10;
    		dd = (aa + bb+dd) / 10;
    		result += cc+'0';
    		l1--;
    		l2--;
    	}
    	while(l1 > 0)
    	{
    		aa = a[l1-1] - '0';
    		cc = (aa + dd) % 10;
    		dd = (aa + dd) / 10;
    		result += cc + '0';
    		l1--;
    	}
    	while(l2 > 0)
    	{
    		bb = b[l2-1] - '0';
    		cc = (bb + dd) % 10;
    		dd = (bb + dd) / 10;
    		result += cc + '0';
    		l2--;
    	}
    	if(dd == 1)
    		result += '1';
    	for(i = result.size() - 1;i >= 0 ;i--)
    		rr += result[i];
    	return rr;
    }
    
    void init()
    {
    	int i;
    	dp[0][0] = "0";
    	dp[0][1] = "1";
    	dp[1][0] = dp[1][1] = "1";
    	for(i = 2;i <= 1000;i++)
    	{
    		dp[i][0] = add(dp[i-2][0],dp[i-1][1]);
    		dp[i][1] = add(dp[i-1][1],dp[i-1][1]);
    	}
    }
    
    int main()
    {
    	int n;
    	init();
    	while(cin >> n)
    	{
    		cout << dp[n-1][0] << endl;
    	}
    	return 0;
    }
    
    
    		
    
    
  • 相关阅读:
    A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
    Fliptile 开关问题 poj 3279
    Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
    Aggressive cows 二分不仅仅是查找
    Cable master(二分题 注意精度)
    B. Pasha and String
    Intervals poj 1201 差分约束系统
    UITextField的快速基本使用代码块
    将UIImage转换成圆形图片image
    color转成image对象
  • 原文地址:https://www.cnblogs.com/sysu-blackbear/p/3292450.html
Copyright © 2011-2022 走看看