• CF550C Divisibility by Eight


    CF550C Divisibility by Eight

    洛谷传送门

    题目描述

    You are given a non-negative integer nn , its decimal representation consists of at most 100100 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 nn . The representation of number nn doesn't contain any leading zeroes and its length doesn't exceed 100100 digits.

    输出格式

    Print "NO" (without quotes), if there is no such way to remove some digits from number nn .

    Otherwise, print "YES" in the first line and the resulting number after removing digits from number nn in the second line. The printed number must be divisible by 88 .

    If there are multiple possible answers, you may print any of them.

    题意翻译

    题目描述

    给你一个位数不超过 100 的非负整数 N(不含前导 0)。你的任务是判断这个数字能否通过去掉其中的一些位上的数(当然不能去掉全部),使其成为一个能被 8 整除的正整数(不含前导 0)。特别注意:你不能重新排列数字的顺序。

    输入输出格式

    输入

    一行,表示正整数 N。保证 N 不超过 100 位。

    输出

    如果不能达成条件,则输出 “NO”(不含引号)。

    如果可以,输出 “YES”,并在第二行输出最终能被 8 整除的那个结果。如果有多解,则输出任意解。


    题解:

    有一个听说是小学奥数但是我还是不会的定理:如果一个数的后三位可被8整除,那么这个数就可被8整除。

    所有题解都给出了这种定理,但是没有题解给证明。

    那么我来证一下:

    因为1000是8的倍数,所以1000的整数倍仍然是8的倍数。也就是说,无论有多少位,除了后三位之外,都可以被8整除。那么后3位如果也可以被8整除,即这个数可以被8整除。

    所以可以有AC代码:

    #include<iostream>
    #include<string>
    using namespace std;
    int sum[105],begin[10],e[10];
    int main()
    {
    	int n=0;
        string str;
    	cin>>str;
    	for(int k=0;k<str.size();k++)
    	{
    		int i=++n,now=str[k]-48;
            sum[i]=now;
    		begin[now]=(begin[now]?begin[now]:i),e[now]=i;
    	}
    	if(begin[0])	
        {
            cout<<"YES
    0";
            return 0;
        }	
        if(begin[8])	
        {
            cout<<"YES
    8"; 
            return 0;
        }
    	for(int i=8;i<=992;i+=8)
    	{
    		int a=i/100,b=i/10%10,c=i%10;
    		if((!begin[a] && a) || (!begin[b] && a && b) || !begin[c])	
                continue;
    		for(int j=(a?begin[a]+1:1);j<e[c];j++)
    			if(sum[j]==b || (a==0 && b==0))	
                {
                    cout<<"YES
    ";
                    if(a)
                        cout<<a;
                    if(b)
                        cout<<b;
                    if(c)
                        cout<<c;
                    return 0;
                }
    	}
    	cout<<"NO";
    	return 0;
    }
    
  • 相关阅读:
    HTML/网站一键打包APK工具(html网页打包安卓APP应用)
    四款最好用的html转苹果APP免上架免签名打包在线生成工具
    ios11,弹出层内的input框光标错位
    前端进阶小知识
    html数据交互小知识
    H5特殊情况解决方案
    H5个人不常用但感觉很重要的东西
    H5小知识
    Java SE 8 Programmer复习小结
    JSP页面中onSubmit方法不执行
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13716098.html
Copyright © 2020-2023  润新知