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 ...
随机推荐
- 修改IIS7中ASP的上传文件大小限制
最近在处理一个ASP的项目,用的全新的Windows Server 2008服务器. 今天客户反映图片文件上传不上去,设置服务器端文件夹权限之后文件可以上传了. 但是不久客户就反映有几个文件传不上去, ...
- java(2014)实现对mysql数据库分页的代码
package util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultS ...
- cocos2d-x android java调用C++
转自:http://www.cnblogs.com/mokey/archive/2013/04/10/3012961.html java调用C++ 1.在jniHelper.java文件中定义一个方法 ...
- C语言中结构体 自引用 和 相互引用
http://blog.163.com/modingfa_002/blog/static/11092546620133193264579 结构体的自引用(self reference),就是在结构体内 ...
- SpringAOP 基础具体解释
Spring AOP对于刚開始学习spring的同学来说有点难以理解.我刚工作的时候都没怎么理解,如今略微理解了一点,所以在这里我将用嘴简单的样例,最通俗易懂的话语来说出我的理解,可能因为我对Spri ...
- xmf 翻译
避免在详细信息视图的确认对话框显示? https://documentation.devexpress.com/#Xaf/CustomDocument3160 我如何获得从登录窗口应用程序的数据库? ...
- POJ 3624 Charm Bracelet
DP 一直是心中痛,不多说了,这个暑假就坑在这上了. 这暑假第一道DP题,01背包问题. 题意是说物品有 重量和价值 ,但你能承受的重量有限,问你能带的最大价值. 这题数组开大点,尽管不知道有啥坑点, ...
- cocos2dx-jsb 跨语言调用及第三方集成 - 过程记录
1:C++中调用js方法: 问题:ios中当用户通过home键将游戏转入后台时,调用js中的暂停游戏方法: AppDelegate::applicationDidEnterBackground() 和 ...
- Adding DTrace Probes to PHP Extensions
By cj on Dec 06, 2012 The powerful DTrace tracing facility has some PHP-specific probes that can b ...
- C#对Windows服务的操作
一.安装服务: private void InstallService(IDictionary stateSaver, string filepath) { try { System.ServiceP ...