550C - Divisibility by Eight - CodeForces Solution


brute force dp math *1500

Please click on ads to support us..

Python Code:

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")
read_int = lambda: int(input())
read_ints = lambda: list(map(int, input().split()))
read_str = lambda: input().strip()
read_strs = lambda: read_str().split(' ')



def solve():
    n = read_int()
    l = list(map(int, str(n)))
    n = len(l)
        if 0 in l or 8 in l:
        print("YES")
        print(0 if 0 in l else 8)
        return
        for i in range(n):
        for j in range(i+1, n):
            if (l[i] * 10 + l[j]) % 8 == 0:
                print("YES")
                print(l[i] * 10 + l[j])
                return
        for i in range(n):
        for j in range(i+1, n):
            for k in range(j+1, n):
                if (l[i] * 100 + l[j] * 10 + l[k]) % 8 == 0:
                    print("YES")
                    print(l[i] * 100 + l[j] * 10 + l[k])
                    return
    print("NO")


solve()

C++ Code:

/********************************
** Author  : Aryan Rajput      **
**     __InnocentStranger__    **
********************************/
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MOD 1000000007
#define MOD1 998244353
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,s,e) for(int i=s;i<e;i++)

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// *s.find_by_order(index) -> give index element
// s.order_of_key(input) -> (no of ele less than given input)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 16;
/*-------------------------------------------------------------------------------------------------*/
vector<int> primes; vector<bool> sieve;
void getSieve() { sieve.resize(maxn, 1); sieve[0] = sieve[1] = 0; for (ll i = 2; i <= maxn; i++) { if (sieve[i]) { for (ll j = i * i; j <= maxn; j += i) sieve[j] = 0; } } }
ll exp(ll n, ll m, ll mod) { if (m == 0) return 1; ll e = exp(n, m / 2, mod); e = (e * e) % mod; if (m & 1) e = (e * n) % mod; return e; }
/*-------------------------------------------------------------------------------------------------*/

void solve() {
    string s; cin >> s;
    int num = 0;
    for(int i = 0; i < s.length(); i++){
        num = s[i]-48; if(num % 8 == 0) {cout << "YES" << endl << num << endl; return;}
        for(int j = i+1; j < s.length(); j++){
            num = (s[i] - 48) *10 + s[j] - 48;
            if(num%8 == 0) { cout << "YES" << endl << num << endl; return;}
            for(int k = j+1; k < s.length(); k++){
                num = ((s[i] - 48) *10 + s[j] - 48) * 10 + s[k] - 48;
                if(num % 8 == 0){cout << "YES" << endl << num << endl; return;}
            }
        }
        num = 0;
    }
    cout << "NO" << endl;
}

int32_t main() {
    fastIO
#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
    int t = 1;
    //cin >> t;

    auto start1 = high_resolution_clock::now();
    while (t--) solve();
    auto stop1 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop1 - start1);
    cerr << "Time: " << duration.count() / 1000 << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III