http://codeforces.com/contest/550/problem/C
给定一个数字(最多100位),可以任意去掉其中的数(每一位数的相对关系不变), 使得剩下的数能够被8整除.最后至少要有一位数.不能满足的话就没有存在的答案.
根据能被8整除的数的特征:只要最低的三位能被整除,这个数便能被整除.
abcdef=abc∗1000+def
8|1000, 只要8|def,那么就有 8|abcdef.
这题是SPJ的,只要找出一个满足的答案就可以了.那么其实只要来一个 O(n3) 的方法就可以过了.就是枚举最后三位,再注意一下前导零.
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
#define pb push_back
#define lb lower_bound
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1005;
char s[N];
int best[3];
void input()
{
scanf("%s", s+3);
s[0] = s[1] = s[2] = '0';
int len = strlen(s);
int i, j, k, cnt = 0;
bool flag = 0;
for(i=len-1; i>=3; i--){
for(j=i-1; j>=0; j--){
for(k=j-1; k>=0; k--){
int num = s[i]-'0' + (s[j]-'0')*10 + (s[k]-'0')*100;
if(num % 8 == 0){
best[0] = k;
best[1] = j;
best[2] = i;
flag = 1;
break;
}
}
if(flag) break;
}
if(flag) break;
}
if(!flag){
puts("NO");
return ;
}
puts("YES");
bool isz = 0;
for(int i=0; i<3; i++){
if(!isz && s[best[i]] == '0' && i<2) continue;
isz = 1;
putchar(s[best[i]]);
}
}
int main(void)
{
input();
return 0;
}