Divisibility by Eight (数学)
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整除的特性:最后三位可以被8整除。
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std; const int SIZE = ;
char S[SIZE];
bool VIS[SIZE],FLAG;
vector<char> ANS; void dfs(int start,int sum,int count);
int main(void)
{
cin >> S;
dfs(,,);
if(!FLAG)
puts("NO"); return ;
} void dfs(int start,int sum,int count)
{
if(FLAG || count > )
return ;
for(int i = start;S[i];i ++)
{
if(FLAG)
return ;
if(!VIS[i])
{
VIS[i] = true;
int back = sum;
ANS.push_back(S[i]);
sum = ANS[ANS.size() - ] - '';
if(ANS.size() >= )
sum += (ANS[ANS.size() - ] - '') * ;
if(ANS.size() >= )
sum += (ANS[ANS.size() - ] - '') * ;
if(sum % == )
{
cout << "YES" << endl;
for(int i = ;i < ANS.size();i ++)
cout << ANS[i];
cout << endl;
FLAG = true;
return ;
} dfs(i + ,sum,count + );
VIS[i] = false;
sum = back;
ANS.pop_back();
}
}
}
Divisibility by Eight (数学)的更多相关文章
- cf306 C. Divisibility by Eight(数学推导)
C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- Codeforces Round #829 (Div. 2) D. Factorial Divisibility(数学)
题目链接 题目大意: \(~~\)给定n个正整数和一个数k,问这n个数的阶乘之和能不能被k的阶乘整除 既:(a\(_{1}\)!+a\(_{2}\)!+a\(_{3}\)!+....+a\(_{n}\ ...
- Codeforces 922.F Divisibility
F. Divisibility time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 数学思想:为何我们把 x²读作x平方
要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...
- 速算1/Sqrt(x)背后的数学原理
概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...
- MarkDown+LaTex 数学内容编辑样例收集
$\color{green}{MarkDown+LaTex 数学内容编辑样例收集}$ 1.大小标题的居中,大小,颜色 [例1] $\color{Blue}{一元二次方程根的分布}$ $\color{R ...
- 深度学习笔记——PCA原理与数学推倒详解
PCA目的:这里举个例子,如果假设我有m个点,{x(1),...,x(m)},那么我要将它们存在我的内存中,或者要对着m个点进行一次机器学习,但是这m个点的维度太大了,如果要进行机器学习的话参数太多, ...
- Sql Server函数全解<二>数学函数
阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...
随机推荐
- 20140102-lua binder另一只轮子的雏形
书接上一回,说到要继续丰富对类型的处理.那么如何才能做到呢,应该是要支持自定义的,所以这一回要讲的就是在前面的基础上,增加支持自定义部分,其中包含以下几个部分 函数的默认参数设置,包括有几个默认参数和 ...
- OC中控制台日志打印
OC中Debug版本常用的打印格式化操作 %@ 对象 %d,%i 整型 (%i的老写法) %hd 短整型 %ld , %lld 长整型 %u 无符整型 %f 浮点型和doubl ...
- Java程序打包成jar包
方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...
- C# 6与VB 12即将加入模式匹配
又有一种源自于函数式编程语言中的概念加入了C#和VB的阵营,这就是被称为模式匹配(Pattern Matching)的特性.初看上去,模式匹配的作用类似于一段switch/select语句块,但它的功 ...
- SRM 449 div1 (practice)
250pt: 暴力枚举所有的可能的情况就好了,求面积的时候我是用梯形的面积减去两个三角形的面积.. 550pt: 题意:给你一个蜂窝形状的特殊图形,有一些格子已经被占据了,问你将剩下的格子用1*2的砖 ...
- 在Android应用中实现Google搜索的例子
有一个很简单的方法在你的 Android 应用中实现 Google 搜索.在这个例子中,我们将接受用户的输入作为搜索词,我们将使用到 Intent.ACTION_WEB_SEARCH . Google ...
- Javascript模块化编程系列三: CommonJS & AMD 模块化规范描述
CommonJS Module 规范 CommonJS 的模块化规范描述在Modules/1.1.1 中 目前实现此规格的包有: Yabble,CouchDB,Narwhal (0.2), Wakan ...
- NUMA
- mysql主从复制 主从配置(windows系统上)
OS:Windows7 DB:MYSQL5.6.2 1.正常安装第一个mysql(安装步骤省略) 2.在控制面板里停止第一个mysql服务 3.将C:\Program Files\MySQL目录下 ...
- Javascript操纵Cookie--转
引用地址:http://www.imkevinyang.com/2009/06/javascript%E6%93%8D%E7%BA%B5cookie.html 在讲如何使用Javascript操纵Co ...