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

Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】的更多相关文章

  1. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  2. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  3. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  4. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  5. Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】

    A. Flipping Game     time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces 834E The Bakery【枚举+数位dp】

    E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...

  7. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  8. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  9. Educational Codeforces Round 1 E. Chocolate Bar dp

    题目链接:http://codeforces.com/contest/598/problem/E E. Chocolate Bar time limit per test 2 seconds memo ...

随机推荐

  1. sonar安装问题记录

    1.启动时日志中提示 Caused by: java.lang.RuntimeException: can not run elasticsearch as root 错误原因:因为安全问题elast ...

  2. Delegate   Func  Action  Predicate default() 知识点

    看仓储模式,有代码写到这几个关键字,陌生,记录下来.       定义一个类型,此类型抽象化了相似结构的某一类方法,因此我们能将此类型代表的方法作为参数进行传递.      Delegate至少0个参 ...

  3. 如何轻松学习C语言编程!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  4. 2018-12-20 第一章初识JAVA-上机

    JAVA初体验之课后练习 一.请写出Java 程序执行过程与编译原理 答:①执行过程:创建.java文件——编译器(JDK环境里的javac.exe文件编译)——输出.class供计算机接受(只有cl ...

  5. mysql主从同步错误,提示The server quit without updating PID file

    在安装完lnmp后,启动mysqld失败,提示 [root@centos-6 ~]# service mysqld start Starting MySQL [确定][root@centos-6 ~] ...

  6. dotnet core2常用命令

    dotnet --info 来查看是否安装成功dotnet new -alldotnet new mvc 创建MVC项目dotnet new more 查看项目类型dotnet restore (恢复 ...

  7. 5104 I-country

    5104 I-country 在 N*M 的矩阵中,每个格子有一个权值,要求寻找一个包含 K 个格子的凸连通块(连通块中间没有空缺,并且轮廓是凸的,如书中图片所示),使这个连通块中的格子的权值和最大. ...

  8. static成员变量和static成员函数例程

    #include "pch.h" #include <iostream> using namespace std; class goods { public: good ...

  9. 【Leetcode】Binary Tree Traversal

    把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...

  10. Effective C++ 改善55个方法

    美·Scott Meyers 候捷 电子工业 2011 刚才看到个会议时间有点晚,3.25论文都提交了 谷歌去广告的插件, 最后投了这个会议,刚刚好正合适.我说金钱与时间 ACCUSTOMING YO ...