Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
2 seconds
256 megabytes
standard input
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.
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.
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.
3454
YES
344
10
YES
0
111111
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】的更多相关文章
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- codeforces 629C Famil Door and Brackets (dp + 枚举)
题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...
- 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 ...
- Codeforces 834E The Bakery【枚举+数位dp】
E. Ever-Hungry Krakozyabra time limit per test:1 second memory limit per test:256 megabytes input:st ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- 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 ...
随机推荐
- [linux] 查看SATA速度和具体设备
查看SATA速度和具体设备 SATA 速度确认 方法一 dmesg |grep SATA 输出 [ 2.977661] ahci 0000:00:17.0: AHCI 0001.0301 32 slo ...
- solr7.3集群搭建
solr集群搭建 原博客:https://blog.csdn.net/gdsgdh308227363/article/details/81004706 注意,在搭建solr集群前,建议最好有一个sol ...
- redis安装 卸载 启动 关闭
一 redis安装 第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机) 第二步:在Linux下安装gcc环境 [root@hadoop ~]#yum install gcc- ...
- Entity Framework 6 暂停重试执行策略
EF6引入一个弹性连接的功能,也就是允许重新尝试执行失败的数据库操作.某些复杂的场景中,可能需要启用或停用重试执行的策略,但是EF框架暂时尚未提供直接的设置开关,将来可能会加入这种配置.幸运的是,很容 ...
- SystemID
A:BJQUANYONG-B:CCC332322987612323008002DDD A:JHDUJIA-B:CCC1365323754641263423809708001DDD A:GUANGZHO ...
- System.Data.OracleClient.dll方式操作oracle数据库
System.Data.OracleClient.dll方式操作oracle数据库 一.查询语句: using (OracleConnection conn = new OracleConnectio ...
- day10学python socket用户交互+MD5加密
socket用户交互+MD5加密 利用socket从client传输文件指令于server 再返还字节大小与内容 socketserver的使用(重要) 注意: ##client.recv(1024) ...
- [agc007f] Shik and Copying String 模拟神题
Description "全"在十分愉快打工,第0天,给了他一个仅有小写字母构成的长度为N的字符串S0,在之后的第i天里,"全"的工作是将Si−1复制一份到 ...
- Squid代理服务器(三)——ACL访问控制
一.ACL概念 Squid提供了强大的代理控制机制,通过合理设置ACL(Access Control List,访问控制列表)并进行限制,可以针对源地址.目标地址.访问的URL路径.访问的时间等各种条 ...
- 图论 竞赛图(tournament)学习笔记
竞赛图(tournament)学习笔记 现在只是知道几个简单的性质... 竞赛图也叫有向完全图. 其实就是无向完全图的边有了方向. 有一个很有趣的性质就是:一个tournament要么没有环,如果 ...