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;
    }
  • 相关阅读:
    zstu.4189: 逻辑运算(构建 && 前缀表达式入门)
    Oulipo (kmp)
    zstu.4194: 字符串匹配(kmp入门题&& 心得)
    杭赛.Triangle(数学推导)
    cf.VK CUP 2015.C.Name Quest(贪心)
    微服务技术栈
    luogu3320 寻宝游戏 (dfs序+倍增lca+set)
    cf757F Team Rocket Rises Again (dijkstra+支配树)
    cf983E NN Country (倍增+dfs序+树状数组)
    cf860E Arkady and A Nobody-men (树剖)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626787.html
Copyright © 2011-2022 走看看