Codeforces 550C divisibility by Eight (enumeration)

Source: Internet
Author: User

" Topic link " click here~~

"The main topic "

Give a number not exceeding 100 digits, ask whether to delete several numbers, the remaining number can be divisible by 8

" problem-solving ideas ": There is a nature: if a number after three can be divisible by 8, then this number can be divisible by 8

Proof: Give an example of a 5-digit number,
For example
_____  _____  __              __  __                __  ___
Abcde=ab000+cde=1000xab+cde=8x125xab+cde
Obviously, 8x125xab must be a multiple of 8 or 125, so when the CDE can be divisible by 8 or 125, the five-digit ABCDE can be divisible by 8 or 125. The number of digits is the same, mainly 1000=125*8

So as long as you enumerate the three bits

Code:

#include <bits/stdc++.h>using namespace Std;int main () {char str[110];        while (CIN&GT;&GT;STR) {bool Ok=false;        int Len=strlen (str);                for (int i=0; i<len; ++i) {if ((str[i]-' 0 ')%8==0) {puts ("yes");                cout<< (str[i]-' 0 ') <<endl;            return 0;                }} for (int i=0, i<len; ++i) {for (int j=i+1; j<len; ++j) {                    if ((str[i]-' 0 ') *10+ (str[j]-' 0 ')) (%8==0) {puts ("yes");                    cout<< ((str[i]-' 0 ') *10+ (str[j]-' 0 ')) <<endl;                return 0;                }}} for (int i=0, i<len; ++i) {for (int j=i+1; j<len; ++j) { for (int k=j+1; k<len; ++k) {if (((str[i]-' 0 ') *100+ (str[j]-' 0 ') *10+str[                       k]-' 0 ')%8==0) { Puts ("YES");                        cout<< ((str[i]-' 0 ') *100+ (str[j]-' 0 ') *10+str[k]-' 0 ') <<endl;                    return 0;    }}}} puts ("NO"); }}
The official problem is to use DP, some trouble.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 550C divisibility by Eight (enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.