Codeforces Round #306 (Div. 2)

来源:互联网 发布:5g网络基站建设 编辑:程序博客网 时间:2024/05/17 21:11

C. 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
YES344
input
10
output
YES0
input
111111
output
NO

好水的题啊!!!

然而自己根本就不会啊。数学真菜啊

求这些数字任意删除,最终结果如果能整除8则输出最后的数,否则NO

自己当时敲了个pow(2,str.size())的暴力,当时看错以为范围是10,然而题目是100,直接WA

其实对于8来说,只要找到1位数,2位数,或者3位数能整除8即可。四位数及以上可以不用找了,为什么呢?因为1000是最小的四位数,它是能整除8的,所以n*1000+m(m表示一个三位数或者二位数或者一位数),(n*1000+m)%8,即m%8。到这里就很水了。

#include<cstdio>#include<cstdlib>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<vector>#include<map>#include<set>#define eps 1e-8#define zero(x) (((x>0?(x):-(x))-eps)#define mem(a,b) memset(a,b,sizeof(a))#define memmax(a) memset(a,0x3f,sizeof(a))#define pfn printf("\n")#define ll __int64#define ull unsigned long long#define sf(a) scanf("%d",&a)#define sf64(a) scanf("%I64d",&a)#define sf264(a,b) scanf("%I64d%I64d",&a,&b)#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)#define sf464(a,b,c,d) scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)#define sf564(a,b,c,d,ee) scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&ee)#define sf2(a,b) scanf("%d%d",&a,&b)#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sf5(a,b,c,d,ee) scanf("%d%d%d%d%d",&a,&b,&c,&d,&ee)#define sff(a) scanf("%f",&a)#define sfs(a) scanf("%s",a)#define sfs2(a,b) scanf("%s%s",a,b)#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)#define sfd(a) scanf("%lf",&a)#define sfd2(a,b) scanf("%lf%lf",&a,&b)#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)#define sfc(a) scanf("%c",&a)#define ull unsigned long long#define pp pair<int,int>#define debug printf("***\n")#define pi 3.1415927#define mod 1000000007#define rep(i,a,b) for(int i=a;i<b;i++)const double PI = acos(-1.0);const double e = exp(1.0);const int INF = 0x7fffffff;;template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a, int b){ return a>b; }bool cmpsmall(int a, int b){ return a<b; }using namespace std;int main(){  //  freopen("data.in","r",stdin);    //freopen("data.out" ,"w",stdout);    string str;    while(cin>>str)    {        rep(i,0,str.size())        {            int num=str[i]-'0';            if(num%8==0)            {                cout<<"YES"<<endl<<str[i]<<endl;                return 0;            }        }        rep(i,0,str.size())        rep(j,i+1,str.size())        {            int num=(str[i]-'0')*10+(str[j]-'0');            if(num%8==0)            {                cout<<"YES"<<endl<<str[i]<<str[j]<<endl;                return 0;            }        }        rep(i,0,str.size())        rep(j,i+1,str.size())        rep(k,j+1,str.size())        {            int num=(str[i]-'0')*100+(str[j]-'0')*10+(str[k]-'0');            if(num%8==0)            {                cout<<"YES"<<endl<<str[i]<<str[j]<<str[k]<<endl;                return 0;            }        }        cout<<"NO"<<endl;    }    return 0;}







0 0
原创粉丝点击