Timus: 1225 (Flags)



// http://acm.timus.ru/problem.aspx?space=1&num=1225
// Runtime: 0.031s
// Tag: Dp
// How many ways u can make a binary string (0/1) of length n, where two '1' can't be adjacent 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Pr(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;
#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

int n;
LL dp [45 + 3] [2];

LL recur (int at, int pre)
{
	if ( at == n ) {
		if ( pre == 1 ) return 0;
		return 1;
	}

	if ( dp [at] [pre] != -1 ) return dp [at] [pre];

	LL c = 0;

	if ( pre != 1 ) {
		c += recur (at + 1, 1);
		c += recur (at + 1, 0);
	} else {
		c += recur (at + 1, 0);
	}

	return dp [at] [pre] = c;
}


int main ()
{
	scanf ("%d", &n);

	Set (dp, -1);
	LL p = recur (1, 0);
	printf ("%lld\n", p * 2);

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

One thought on “Timus: 1225 (Flags)

  1. I have a much easier solution… If u observe carefully, this problem is similar to fibonacci problem.
    Here is my solution…Its runtime was 0.001s.

    #include
    using namespace std;
    int64_t a[45];
    int64_t fib(int n)
    {
    if (n==1) return n+1;
    else if (n==0) return n;
    else if (a[n]!=-1) return a[n];
    else a[n]=fib(n-1)+fib(n-2);
    return a[n];
    }

    int main()
    {
    for (int i=0; i> inum;
    cout << fib(inum);
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s