-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA02018 - BIỂU THỨC TOÁN HỌC
156 lines (156 loc) · 3.67 KB
/
DSA02018 - BIỂU THỨC TOÁN HỌC
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//Made By Phuong Nam PROPTIT <3//
#pragma GCC Optimize("O3")
#include<bits/stdc++.h>
#define alphaa "abcdefghijklmnopqrstuvwxyz"
#define ALPHAA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define f(i,a,b) for(int i=a;i<=b;i++)
#define f1(i,n) for(int i=1;i<=n;i++)
#define f0(i,n) for(int i=0;i<n;i++)
#define ff(i,b,a) for(int i=b;i>=a;i--)
#define el cout<<'\n'
#define fi first
#define se second
#define pb push_back
#define pk pop_back
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(s) s.begin(),s.end()
using namespace std;
typedef long long ll;
const int N=1e6+3+3;
const int MOD=1e9+7;
int a[6],b[4],kt=1;
vector<vi>res;
void di()
{
vi v;
f1(i,3) v.pb(b[i]);
res.pb(v);
int i=3;
while(i&&b[i]==3) i--;
if(i<=0) return kt=0,void();
b[i]++;
f(j,i+1,3) b[j]=1;
}
int deg(char c)
{
if(c=='*') return 50;
if(c=='+'||c=='-') return 100;
return 200;
}
string hauto(string s)
{
string ans="";
stack<char>a;
f0(i,s.size())
{
if(s[i]=='(')
{
a.push('(');
continue;
}
if(s[i]==')')
{
while(a.size()&&a.top()!='(')
{
ans+=a.top();
a.pop();
}
a.pop();
continue;
}
if(s[i]=='+'||s[i]=='-'||s[i]=='*')
{
while(a.size()&°(a.top())<=deg(s[i]))
{
ans+=a.top();
a.pop();
}
a.push(s[i]);
continue;
}
int num=0,ktt=0;;
while(i<s.size()&&isdigit(s[i]))
{
ktt=1;
num=num*10+(s[i]-'0');
i++;
}
if(ktt) i--;
ans=ans+"("+to_string(num)+")";
}
while(a.size()) ans+=a.top(),a.pop();
return ans;
}
ll tinh(string s)
{
stack<ll>a;
int i=0;
while(i<s.size())
{
if(s[i]=='(')
{
ll so=0;
i++;
while(isdigit(s[i]))
{
so=so*10+(s[i]-'0');
i++;
}
i++;
a.push(so);
}
else if(a.size()>=2)
{
ll x=a.top();a.pop();
ll y=a.top();a.pop();
if(s[i]=='+') a.push(x+y);
else if(s[i]=='-') a.push(y-x);
else a.push(y*x);
i++;
}
}
return a.top();
}
void xuly()
{
f1(i,5) cin>>a[i];
for(int i=0;i<res.size();i++)
{
int x=res[i][0],y=res[i][1],z=res[i][2];
do
{
string t="";
if(x==1) t="("+to_string(a[1])+'+'+to_string(a[2])+")";
if(x==2) t="("+to_string(a[1])+'-'+to_string(a[2])+")";
if(x==3) t="("+to_string(a[1])+'*'+to_string(a[2])+")";
if(y==1) t="("+t+'+'+to_string(a[3])+")";
if(y==2) t="("+t+'-'+to_string(a[3])+")";
if(y==3) t="("+t+'*'+to_string(a[3])+")";
if(z==1) t="("+t+'+'+to_string(a[4])+")";
if(z==2) t="("+t+'-'+to_string(a[4])+")";
if(z==3) t="("+t+'*'+to_string(a[4])+")";
string t1,t2,t3;
t1=t+'+'+to_string(a[5]);
t2=t+'-'+to_string(a[5]);
t3=t+'*'+to_string(a[5]);
if(tinh(hauto(t1))==23) return cout<<"YES\n",void();
if(tinh(hauto(t2))==23) return cout<<"YES\n",void();
if(tinh(hauto(t3))==23) return cout<<"YES\n",void();
}while(next_permutation(a+1,a+5+1));
}
cout<<"NO";el;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
f1(i,3) b[i]=1;
while(kt) di();
int t=1;
cin>>t;
while(t--) xuly();
return 0;
}
//-----YEU CODE HON CRUSH-----//