Pagini recente » Cod sursa (job #1589261) | Cod sursa (job #2714856) | Cod sursa (job #2641618) | Cod sursa (job #2344933) | Cod sursa (job #3196145)
#include <fstream>
using namespace std;
ifstream fin("perle.in");
ofstream fout("perle.out");
int t,n,x[100001],i,ok;
void B();
void C();
/**
A -> 1 | 2 | 3
B -> 2B | 1A3AC
C -> 2 | 3BC | 12A
*/
void solve()
{
i=1;
ok=0;
if(n==1){
fout<<1<<"\n";
return;
}
if(x[i]==2)
B();
else if(x[i]==3)
C();
else if(x[i]==1 && n==3){
C();
}
else if(x[i]==1){
B();
}
else
{
fout<<"0"<<"\n";
return;
}
fout<<1-ok<<"\n";
}
void B()
{
if (x[i]==2)
{
i++;
if(i==n)
{
ok=1;
return;
}
B();
}
else if(x[i]==1)
{
i++;
if(i>n)
{
ok=1;
return;
}
i++;
if(x[i]!=3)
{
ok=1;
return;
}
else
i++;
if(i>n)
{
ok=1;
return;
}
i++;
if(i>n)
{
ok=1;
return;
}
C();
}
else
{
ok=1;
return;
}
}
void C()
{
if(i==n)
{
return;
}
if(x[i]==3)
{
i++;
if(i>n)
{
ok=1;
return;
}
B();
if(i>n)
{
ok=1;
return;
}
C();
}
else if(x[i]==1 && x[i+1]==2 && i+2==n)
{
return;
}
else
{
ok=1;
return;
}
}
int main()
{
fin>>t;
for(int yp=1;yp<=t;yp++)
{
fin>>n;
for(int p=1;p<=n;p++)
fin>>x[p];
solve();
}
return 0;
}