zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    博科SAN交换机基本配置(华为SNS系列交换机为例OEM博科)
    华为SNS交换机(OEM博科FC交换机)Fabric OS: v 8版本后通过https方式浏览器访问交换机Webtools显示没有匹配的加密算法套件的解决办法
    SNS光纤交换机怎样禁用 Virtual Fabric模式
    光纤网络的相关知识
    元音老人:怎样了生死
    戒淫偈【每天念十遍】。。。
    戒淫偈汇集
    净空老法师:你能这样念上一个星期试试看,肯定比你过去念十年还有效!
    修华严奥旨妄尽还原观 第6集
    贤公和尚,佛门榜样。-海贤老和尚往生纪实
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13716098.html
Copyright © 2011-2022 走看看