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 ...
随机推荐
- 获取父进程ID
本程序主要功能是:获取某程序的ParentProcessID 直接上代码: // parent.cpp (Windows NT/2000) // // This example will show t ...
- 7. 泛化(Generalization)
什么是泛化关系?用一个例子简单的说:假设A是B和C的父类,B.C具有公共类(父类)A,说明A是B.C的一般化(概括,也称泛化),B.C是A的特殊化. 在编程上,泛化关系(Generalization) ...
- MyEclipse 编写 ExtJS 卡死问题解决方法
MyEclipse 8.6 在 jsp 中编写 ExtJS时,会出现卡死现象,让人甚是头疼.网上找了很多方法,折腾半天,还是不管用. 什么MyEclipse 优化,Validation 取消,MyE ...
- 几种连接数据库的OLEDB驱动程序
以下是连接几种数据库的驱动,把用"<%"和"%>"括住的地方保存为文件你就可以直接调用了 连接Access数据库 <% dim conn,db ...
- Android Volley框架的使用(二)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git 使用请求队列RequestQueue Volley中的Request都需要添加到Reque ...
- C++学习笔记之由文本文件读取数据到vector模板建立的二维数组 并存储为新的文本文件
阅读本文可首先参考: C++学习笔记之输入.输出和文件 测试数据: /*读取txt文件到二维数组*/ #include <iostream> #include <fstream> ...
- grep经常使用使用方法
grep简单介绍 正如linux的man文件里所描写叙述的那样: grep searches the named input FILEs (or standard input if no files ...
- MySql 日期格式化函数date_format()
mysql> select date_format(now(),'%Y'); +-------------------------+ | date_format(now(),'%Y') | +- ...
- Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格
Testin云測公布首份国内应用质量报告:半数APP平均启动时间不合格 2014/10/23 · Testin · 实验室报告 日前,Testin云測旗下质量管家Master通过随机取样1605款国内 ...
- 提升GDI画图的效率
假设我们要画一个坐标图,里面可能还需要画网络线.XY各个单位的值.曲线或直线等,可能的函数代码如下: void OnPaint () { CPaintDC dc (this); DrawXY (&am ...