C. Divisibility by Eight
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
Output
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
Examples
input
3454
output
YES 344
input
10
output
YES 0
input
111111
output
NO
题目大意
给一串数字,问是否可以删除掉一些数字(不能删完),剩下的数串起来是8的倍数。
解法
最多枚举3位数字,假如一个大于3位的数字aaaaaxyz 就可以拆分成aaaaa000 + xyz,如果它是8的倍数,因为aaaaa000是8的倍数,则xyz也是8的倍数。
#include <bits/stdc++.h> #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug_in freopen("in.txt","r",stdin) #define debug_out freopen("out.txt","w",stdout); #define pb push_back #define all(x) x.begin(),x.end() #define fs first #define sc second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int maxn = 1e6+10; const int maxM = 1e6+10; const int inf = 1e8; const ll inf2 = 1e17; template<class T>void read(T &x){ T s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); x = s*w; } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template <typename ... T> void DummyWrapper(T... t){} template <class T> T unpacker(const T& t){ cout<<' '<<t; return t; } template <typename T, typename... Args> void pt(const T& t, const Args& ... data){ cout << t; DummyWrapper(unpacker(data)...); cout << '\n'; } //-------------------------------------------- char s[maxn];int len; bool d[11][11][11]; void solve(){ for(int i = 0;i<10;i++){ for(int j = 0;j<10;j++){ for(int k = 0;k<10;k++){ int t = i*100 + j*10 + k; if(t%8 == 0) { string cur = to_string(t); int j = 1,ok = 1; for(auto c:cur){ int tag = 0; for(;j<=len;j++){ if(s[j] == c){ tag = 1; j++;break; } } if(tag == 0){ ok = 0; break; } } if(ok){ puts("YES"); cout<<cur<<'\n'; return ; } } } } } printf("NO\n"); } int main(){ // debug_in; scanf("%s",s+1);len = strlen(s+1); solve(); return 0; }