-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path235-divisibility-by-eight.cpp
More file actions
208 lines (205 loc) · 4.75 KB
/
Copy path235-divisibility-by-eight.cpp
File metadata and controls
208 lines (205 loc) · 4.75 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Divisibility by Eight [550C]
* Problem: https://codeforces.com/problemset/problem/550/C
* Verdict: ACCEPTED Solved: 2021-05-17
* Language: C++17 (GCC 7-32)
* Runtime: 31 ms Memory: 0 KB
* Tags: brute force, dp, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/550/submission/116506946
*/
/// isA AC
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
#pragma GCC optimize("-Ofast")
using namespace std;
template<
typename Key, // Key type
typename Mapped, // Mapped-policy
typename Cmp_Fn = std::less<Key>, // Key comparison functor
typename Tag = __gnu_pbds::rb_tree_tag, // Specifies which underlying data structure to use
template<
typename Const_Node_Iterator,
typename Node_Iterator,
typename Cmp_Fn_,
typename Allocator_>
class Node_Update = __gnu_pbds::null_node_update, // A policy for updating node invariants
typename Allocator = std::allocator<char> > // An allocator type
class tree;
typedef tree<
int,
__gnu_pbds::null_type,
less<int>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
ordered_set;
// Important functions and defines
/// BidoTeima legacy functions
using ll = long long;
ll mod = (ll)1e9+7;
ll MOD = mod;
int64_t POW(int64_t a, int64_t b)
{
if(b==0) return 1;
a%=mod;
int64_t res = POW(a,b/2);
if(b&1)
return (res%mod*res%mod*a%mod)%mod;
return (res%mod*res%mod)%mod;
}
ll sum_range2d(ll i, ll j, ll k, ll l, vector<vector<ll>>& sum)
{
return sum[k][l] - sum[k][j - 1] - sum[i - 1][l] + sum[i - 1][j - 1];
}
ll GCDAC(ll a, ll b)
{
if(b==0)
return a;
return GCDAC(b, a%b);
}
ll gcd(ll a, ll b)
{
if(a<b)swap(a,b);
return GCDAC(a, b);
}
ll lcm(ll a,ll b)
{
return (a/gcd(a,b))*b;
}
ll dist(ll X1, ll Y1, ll X2, ll Y2)
{
return sqrt(POW(X1 - X2, 2) + POW(Y1 - Y2, 2));
}
bool intersect(pair<ll, ll> p1, pair<ll, ll> p2)
{
ll x1 = p1.first, x2 = p1.second, y1 = p2.first, y2 = p2.second;
return (x1 >= y1 && x1 <= y2) ||
(x2 >= y1 && x2 <= y2) ||
(y1 >= x1 && y1 <= x2) ||
(y2 >= x1 && y2 <= x2);
}
bool isPrime(ll n)
{
for (ll i = 2; i * i <= n; i++) if (n % i == 0) return false;
return true;
}
ll inverse(ll n)
{
return POW(n%mod,mod-2);
}
bool mul_overflow(ll x, ll y) {
return (log2(x)+log2(y)) >= 64.0;
}
ll ceili(ll x,ll y){
return (x + y - 1) / y;
}
string operator*(const string& a, size_t n){
string ret;
for(int i = 0; i < n; i++)ret+=a;
return ret;
}
void operator*=(string& a, size_t n){
string cpy=a;
for(int i = 1; i < n; i++)a+=cpy;
if(!n)a="";
}
void ACPLS(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
}
/*const ll maxn = 30;
ll C[maxn + 1][maxn + 1];
void bin_coeff()
{
C[0][0] = 1;
for (ll n = 1; n <= maxn; ++n) {
C[n][0] = C[n][n] = 1;
for (ll k = 1; k < n; ++k)
C[n][k] = ((C[n - 1][k - 1]) + (C[n - 1][k]));
}
}*/
/*vector<bool> prime(10000005, true);
void sievePrime(ll n)
{
prime[1]=false;
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=n; i += p)
prime[i] = false;
}
}
}*/
/// End of BidoTeima legacy functions
#define ALL(x) (x).begin(),(x).end()
#define all(cont) (cont).begin(), (cont).end()
#define rall(cont) (cont).rbegin(), (cont).rend()
#define tc int tt;cin>>tt;while(tt--)
string s;
int a[3],n,mx=-1;
bool vis[105][3];
void recurse(int idx, int num){
if(idx==-1||vis[idx][num])
return;
vis[idx][num]=1;
recurse(idx-1,num);
if(s[idx]==a[num]){
if(num==0){
mx=max(mx,idx);
return;
}
else recurse(idx-1,num-1);
}
}
int main() {
ACPLS();
cin>>s;
n = (int)s.size();
int freq[10]={0},pos[10];for(int i = 0; i < 10; i++)pos[i]=INT_MAX;
for(auto&i:s)i-='0',freq[i]++;
for(int i = 0; i < n; i++){
pos[s[i]]=min(pos[s[i]], i);
}
string ans;
for(int i = 0; i < 1000; i+=8){
a[2]=i%10,a[1]=(i/10)%10,a[0]=(i/100)%10;
int mxbef=mx;
recurse(n-1,2);
if(mx>mxbef){
ans="";
for(int i = 0; i < mx; i++){
ans.push_back(s[i]+'0');
}
ans.push_back(a[0]+'0');
ans.push_back(a[1]+'0');
ans.push_back(a[2]+'0');
}
memset(vis,0,sizeof(vis));
}
if(ans.empty()){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
bool yes=(i==j?freq[i]>=2:freq[i]>0&&freq[j]>0&&pos[i]<pos[j])&&((i*10+j)%8==0);
if(yes){
cout<<"YES\n"<<i<<j;
return 0;
}
}
}
if(freq[8]>0){
cout<<"YES\n8";
}
else if(freq[0]>0){
cout<<"YES\n0";
}
else cout<<"NO";
} else{
cout<<"YES\n"<<ans;
}
}