zoukankan      html  css  js  c++  java
  • 【codeforces 550C】Divisibility by Eight

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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
    YES
    344
    input
    10
    output
    YES
    0
    input
    111111
    output
    NO

    【题目链接】:http://codeforces.com/contest/550/problem/C

    【题解】

    假设一个数为x
    设x的位数大于等于4

    x=1000*a1+100*a2+10*a3+a4
    而1000能被8整除
    则x能被8整除的充要条件是x的后3位也能被8整除;
    因为位数最大为100
    所以C(100,3)->取出3个数看看能不能是8的倍数;
    当然不能漏了2位数和一位数的情况;
    都枚举出来即可;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    const int MAXN = 1E2+10;
    
    char s[MAXN];
    bool bo[MAXN];
    int n,limit;
    
    void sear_ch(int now,int pre)
    {
        if (now == limit+1)
        {
            int tt = 0;
            rep1(i,1,100)
                if (bo[i])
                {
                    int key = s[i]-'0';
                    tt = tt * 10 + key;
                }
            if (tt % 8 == 0)
            {
                puts("YES");
                printf("%d
    ",tt);
                exit(0);
            }
            return;
        }
        rep1(i,pre+1,n)
            if (!bo[i])
            {
                bo[i] = true;
                sear_ch(now+1,pre);
                bo[i] = false;
            }
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        scanf("%s",s+1);
        n = strlen(s+1);
        limit = 3;
        sear_ch(1,0);
        limit = 2;
        sear_ch(1,0);
        limit = 1;
        sear_ch(1,0);
        puts("NO");
        return 0;
    }
  • 相关阅读:
    Linux操纵体系备份工具Tar运用说明
    Linux琐屑网络设置细致分解
    若何制造Linux系统启动盘2
    RedHatAS5下整合Apache和Tomcat
    移除Linux体系下不需求的效劳
    Linux平安隐患及增强管理的方法1
    Fedora 9 布置好后启动不克不及进入GUI,表现屏表现“Out Of Range"的处理惩罚法子
    Linux和Unix最常用的收集下令
    若何彻底移除编造机Wine模仿程式的图标
    若何制造Linux系统启动盘1
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626787.html
Copyright © 2011-2022 走看看