Codeforces1132A——Regular Bracket Sequence(水题)

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

A string is called bracket sequence if it does not contain any characters other than “(“ and “)”. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are regular bracket sequences; “))” and “)((“ are bracket sequences (but not regular ones), and “(a)” and “(1)+(1)” are not bracket sequences at all.

You have a number of strings; each string is a bracket sequence of length $2$. So, overall you have $cnt1$ strings “((“, $cnt2$ strings “()”, $cnt3$ strings “)(“ and $cnt4$ strings “))”. You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length $2(cnt1+cnt2+cnt3+cnt4)$. You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.

Input

The input consists of four lines, $i$-th of them contains one integer $cnt_i$ $(0≤cnt_i≤10^9)$.

Output

Print one integer: $1$ if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, $0$ otherwise.

Examples

input

1
2
3
4
3
1
4
3

output

1
1

input

1
2
3
4
0
0
0
0

output

1
1

input

1
2
3
4
1
2
3
4

output

1
0

Note

In the first example it is possible to construct a string “(())()(()((()()()())))”, which is a regular bracket sequence.

In the second example it is possible to construct a string “”, which is a regular bracket sequence.

Solve

你有四种括号,”((“,”()”,”)(“,”))”, 给出四种括号的数量, 问能否将这些括号以某种顺序连接起来, 使得每个左括号都有与之匹配的右括号。

观察可以发现:如果要完全匹配,()是不会对结果产生影响的,然后可以推出一个关系式:$cnt1=2\times cnt3+cnt4,cnt4=2\times cnt3+cnt1​$

即:当$cnt3\neq0,cnt1=cnt4\neq0$或$cnt3=0,cnt1=cnt2$时,才会完全匹配

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*************************************************************************

> Author: WZY
> School: HPU
> Created Time: 2019-03-15 10:34:53

************************************************************************/
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstring>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <random>
#define ll long long
#define ull unsigned long long
#define lson o<<1
#define rson o<<1|1
#define ms(a,b) memset(a,b,sizeof(a))
#define SE(N) setprecision(N)
#define PSE(N) fixed<<setprecision(N)
#define bug cerr<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
#define LEN(A) strlen(A)
const double E=exp(1);
const double eps=1e-9;
const double pi=acos(-1.0);
const int mod=1e9+7;
const int maxn=1e6+10;
const int maxm=1e3+10;
const int moha=19260817;
const int inf=1<<30;
const ll INF=1LL<<60;
using namespace std;
inline void Debug(){cerr<<'\n';}
inline void MIN(int &x,int y) {if(y<x) x=y;}
inline void MAX(int &x,int y) {if(y>x) x=y;}
inline void MIN(ll &x,ll y) {if(y<x) x=y;}
inline void MAX(ll &x,ll y) {if(y>x) x=y;}
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
cerr<<arg<<"";Debug(rest...);}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);cin.tie(0);
cout.precision(20);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
int cnt1,cnt2,cnt3,cnt4;
while(cin>>cnt1>>cnt2>>cnt3>>cnt4)
{
if(cnt1+cnt4==0)
{
if(cnt3)
cout<<0<<endl;
else
cout<<1<<endl;
continue;
}
if(cnt1==cnt4)
cout<<1<<endl;
else
cout<<0<<endl;
}
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
#endif
return 0;
}

本文标题:Codeforces1132A——Regular Bracket Sequence(水题)

文章作者:执念

发布时间:2019年03月15日 - 11:03

最后更新:2019年03月31日 - 21:03

原始链接:https://blog.wzy1999.wang/solve/cf1132A/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------
0%