B. Curiosity Has No Limits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
When Masha came to math classes today, she saw two integer sequences of length n−1n−1 on the blackboard. Let's denote the elements of the first sequence as aiai (0≤ai≤30≤ai≤3), and the elements of the second sequence as bibi (0≤bi≤30≤bi≤3).
Masha became interested if or not there is an integer sequence of length nn, which elements we will denote as titi (0≤ti≤30≤ti≤3), so that for every ii (1≤i≤n−11≤i≤n−1) the following is true:
- ai=ti|ti+1ai=ti|ti+1 (where || denotes the bitwise OR operation) and
- bi=ti&ti+1bi=ti&ti+1 (where && denotes the bitwise AND operation).
The question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence titi of length nn exists. If it exists, find such a sequence. If there are multiple such sequences, find any of them.
Input
The first line contains a single integer nn (2≤n≤1052≤n≤105) — the length of the sequence titi.
The second line contains n−1n−1 integers a1,a2,…,an−1a1,a2,…,an−1 (0≤ai≤30≤ai≤3) — the first sequence on the blackboard.
The third line contains n−1n−1 integers b1,b2,…,bn−1b1,b2,…,bn−1 (0≤bi≤30≤bi≤3) — the second sequence on the blackboard.
Output
In the first line print "YES" (without quotes), if there is a sequence titi that satisfies the conditions from the statements, and "NO" (without quotes), if there is no such sequence.
If there is such a sequence, on the second line print nn integers t1,t2,…,tnt1,t2,…,tn (0≤ti≤30≤ti≤3) — the sequence that satisfies the statements conditions.
If there are multiple answers, print any of them.
Examples
input
Copy
4 3 3 2 1 2 0
output
Copy
YES 1 3 2 0
input
Copy
3 1 3 3 2
output
Copy
NO
Note
In the first example it's easy to see that the sequence from output satisfies the given conditions:
- t1|t2=(012)|(112)=(112)=3=a1t1|t2=(012)|(112)=(112)=3=a1 and t1&t2=(012)&(112)=(012)=1=b1t1&t2=(012)&(112)=(012)=1=b1;
- t2|t3=(112)|(102)=(112)=3=a2t2|t3=(112)|(102)=(112)=3=a2 and t2&t3=(112)&(102)=(102)=2=b2t2&t3=(112)&(102)=(102)=2=b2;
- t3|t4=(102)|(002)=(102)=2=a3t3|t4=(102)|(002)=(102)=2=a3 and t3&t4=(102)&(002)=(002)=0=b3t3&t4=(102)&(002)=(002)=0=b3.
In the second example there is no such sequence.
一、原题地址
二、大致题意
给出a[ ]和b[ ],要求输出一个序列T[ ]。满足
t1|t2=a[1] t1&t2=b[1]
t2|t3=a[2] t2&t3=b[2]
t3|t4=a[3] t3&t4=b[3]
……
若存在T [ ]则输出一种情况。反之输出NO
三、思路
大力搜索就过了,赛后想想T[ ]应该是具有“唯一性”的。因为既有a[i]\b[i]的限制,又有ti-1的限制,所以在DFS中出口是很少的(接近单出口?)
代码跑了62ms
四、代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
int n;
int a[100005], b[100005];
int ans[100005];
int pos;
bool tag;
int huo[5][5], yu[5][5];
void DFS(int np, int pre)
{
if (tag)return;
if (np == n+1)
{
tag = true;
return;
}
for (int i = 0; i <= 3; i++)
{
if (huo[pre][i] == a[np - 1] && yu[pre][i] == b[np - 1])
{
ans[np] = i;
DFS(np + 1, i);
}
if (tag)return;
}
}
int main()
{
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
huo[i][j] = i | j;
yu[i][j] = i&j;
}
}
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++)scanf("%d", &a[i]);
for (int i = 1; i <= n - 1; i++)scanf("%d", &b[i]);
tag = false;
for (int t1 = 0; t1 <= 3; t1++)
{
for (int t2 = 0; t2 <= 3; t2++)
{
if (((t1 | t2) == a[1]) && ((t1&t2) == b[1]))
{
pos = 3;
ans[1] = t1;
ans[2] = t2;
DFS(3, t2);
if (tag)break;
}
}
if (tag)break;
}
if (tag)
{
printf("YES\n");
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
}
else
printf("NO\n");
}