Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/550/problem/C
Description
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
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.
Sample Input
3454
Sample Output
YES
344
HINT
题意
给你一个最多100位的数,让你去掉一些数,然后让这个数是8的倍数
题解:
首先,成为8的倍数,只要末尾3位数是8的倍数就好(8×125=1000)
于是我们只用判定三位数就好,然后特判一位数和两位数的情况
暴力枚举三位数的情况就好啦~
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** string s; ll ans; ll solve(char A,char B,char C) { ll ans=(A-'0')*100+(B-'0')*10+(C-'0'); return ans; } int main() { //test; char O='0'; cin>>s; for(int i=0;i<s.size();i++) { if(s[i]=='8'||s[i]=='0') { cout<<"YES"<<endl; cout<<s[i]<<endl; return 0; } } if(s.size()==1) { ans=s[0]-'0'; if(ans%8==0) { printf("YES\n"); cout<<ans<<endl; return 0; } printf("NO\n"); return 0; } if(s.size()==2) { if(solve(O,O,s[0])%8==0) { cout<<"YES"<<endl; cout<<solve(O,O,s[0])<<endl; return 0; } if(solve(O,s[1],s[0])%8==0) { cout<<"YES"<<endl; cout<<solve(O,s[1],s[0])<<endl; return 0; } if(solve(O,O,s[1])%8==0) { cout<<"YES"<<endl; cout<<solve(O,O,s[1])<<endl; return 0; } printf("NO\n"); return 0; } for(int j=0;j<s.size();j++) { for(int k=j+1;k<s.size();k++) { if(solve(O,s[j],s[k])%8==0) { cout<<"YES"<<endl; cout<<solve(O,s[j],s[k])<<endl; return 0; } } } for(int i=0;i<s.size();i++) { for(int j=i+1;j<s.size();j++) { for(int k=j+1;k<s.size();k++) { if(solve(s[i],s[j],s[k])%8==0) { cout<<"YES"<<endl; cout<<solve(s[i],s[j],s[k])<<endl; return 0; } } } } cout<<"NO"<<endl; return 0; }