Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】

 Divisibility by Eight
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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整除。如果存在,输出“YES”并且把该数输出。否则,输出“NO”。

解题思路:我们可以知道,10^3的倍数都可以被8整除。所以我们只要我们枚举判断最多3位数时能否被8整除即可。所以就是O(len^3)。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1e5 + 300;
const int INF = 0x3f3f3f3f;
typedef long long  LL;
typedef unsigned long long ULL;
char s[200];
int main(){
    while(scanf("%s",s+1)!=EOF){
        int len = strlen(s+1);
        int num , num1, num2, ans;
        int flag = 0;
        for(int i = 1; i <= len; i++){
            if(flag) break;
            num = s[i] - '0';
            if(num % 8 == 0){
                ans = num;
                flag = 1; break;
            }
            for(int j = i+1; j <= len; j++){
                if(flag) break;
                num1 = num * 10;
                num1 = num1 + s[j] - '0';
                if(num1 % 8 == 0){
                    ans = num1;
                    flag = 1; break;
                }
                for(int k = j+1; k <= len; k++){
                    num2 = num1 * 10;
                    num2 = num2 + s[k] - '0';
                    if(num2 % 8 == 0){
                        ans = num2;
                        flag = 1;
                        break;
                    }
                }
            }
        }
        if(flag){
            puts("YES"); printf("%d\n",ans);
        }else{
            puts("NO");
        }
    }
    return 0;
}

  

题解中还有一种更好的复杂度。但是所给的dp转移方程不太明白,有机会再看看。

http://codeforces.com/blog/entry/18329

posted @ 2016-05-30 18:44  tcgoshawk  阅读(523)  评论(0编辑  收藏  举报