poj1090,

## Problem:

Chain

 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2220 Accepted: 728

Description
Byteland had not always been a democratic country. There were also black pages in its book of history. One lovely day general Bytel − commander of the junta which had power over Byteland −− decided to finish the long−lasting time of war and released imprisoned activists of the opposition. However, he had no intention to let the leader Bytesar free. He decided to chain him to the wall with the bytish chain. It consists of joined rings and the bar fixed to the wall. Although the rings are not joined with the bar, it is hard to take them off.
'General, why have you chained me to the prison walls and did not let rejoice at freedom!' cried Bytesar.
'But Bytesar, you are not chained at all, and I am certain you are able to take off the rings from the bar by yourself.' perfidiously answered general Bytel, and he added 'But deal with that before a clock strikes the cyber hour and do not make a noise at night, otherwise I will be forced to call Civil Cyber Police.'
Help Bytesar! Number the following rings of the chain with integers 1,2,...,n. We may put on and take off these rings according to the following rules:
.only one ring can be put on or taken off from the bar in one move,
.the ring number 1 can be always put on or taken off from the bar,
.if the rings with the numbers 1,...,k−1 (for 1<= k < n) are taken off from the bar and the ring number k is put on, we can put on or take off the ring number k+1.
Write a program which:
.reads from std input the description of the bytish chain,
.computes minimal number of moves necessary to take off all rings of the bytish chain from the bar,
.writes the result to std output.

Input
In the first line of the input there is written one integer n, 1 <= n <= 1000. In the second line there are written n integers o1,o2,...,on (each of them is either 0 or 1) separated by single spaces. If oi=1, then the i−th ring is put on the bar, and if oi=0, then the i−th ring is taken off the bar.

Output
The output should contain exactly one integer equal to the minimal number of moves necessary to take off all the rings of the bytish chain from the bar.

Sample Input
```4
1 0 1 0```

Sample Output
`6`

Source
POI 2001

## Solution:

Gray code to binary code, then translate it in decimal.
(But Why?)

## Source Code:

//Sat Apr 24 20:29:55 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

//Double the number;
vector<int> Twice(vector<int> v)
{
vector<int> ret = v;
int flag = 0;
for (int i = v.size() - 1; i >= 0; i--)
{
ret[i] += v[i] + flag;
if (ret[i] > 9)
{
flag = 1;
ret[i] %= 10;
}
else
flag = 0;
}
if (flag == 1)
ret.insert(ret.begin(), 1);
return ret;
}

//Operation : number + 1;
vector<int> PlusOne(vector<int> v)
{
int flag = 1;
for(int i=(int)v.size() -1; i>=0; i--)
{
v[i] += flag;
if(v[i] > 9)
{
flag = 1;
v[i] %= 10;
}
else
return v;
}
if(flag == 1)
{
v.insert(v.begin(), 1);
}
return v;
}

vector<int> Binary2Decimal(vector<int> v)
{
vector<int> ret(1,0);
for (int i = 0; i <(int) v.size(); i++)
{
if(v[i] == 1)
{
ret = PlusOne(Twice(ret));
}
else
{
ret = Twice(ret);
}
}
return ret;
}

int main(int argc, const char* argv[])
{
//  freopen("input.in", "r", stdin);
//  freopen("output.out", "w", stdout);
int N;
while (cin >> N)
{
vector<int> v(N);
vector<int> dest(N);
for (int i = 0; i < N; i++)
cin >> v[N - i - 1];
dest = v;
for (int i = 1; i < N; i++)
{
dest[i] = dest[i - 1] ^ v[i];
//          cout << dest[i];
}
vector<int> res = Binary2Decimal(dest);
for(int i=0; i<(int)res.size(); i++)
{
cout << res[i];
}
cout << endl;
}
//  fclose(stdin);
//  fclose(stdout);
return 0;
}