【CodeForces 550C 】Divisibility by Eight DFS 數學

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

題意:問一個數去掉若干數位後能否被8整除,有則輸出一種情況

思路:

數學條件:能被8整除的數,其末三位肯定能被8整除(n>=100)。
這樣,我們就可以DFS對每個數位進行選或者不選的操作,選了就計入sum,然後看選了1、 2 、 3位的時候能否被8整除即可。時間複雜度O(n3)。具體操作見代碼註釋

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

string s;
bool flag = false;
void dfs(ll pos, ll sum, ll num)
{
    if(flag) return;        //找到了就不找了
    if(num==3)      //滿員,看能否被8整除
    {
        if(sum%8==0) flag = true, cout<<"YES"<<'\n'<<sum<<endl;
        return;
    }
    if((num==1||num==2)&&sum%8==0)      //過程中有結果可以直接處理,注意這一步要放到下面那個遞歸出口的前面,因爲可能是最後一個位置有滿足條件的
    {
        flag = true;
        cout<<"YES"<<'\n'<<sum<<endl;       //找到輸出
        return;
    }
    if(pos==s.size()) return;       //邊界放最後
    dfs(pos+1,sum,num);     //取或者不取
    if(flag) return;        
    dfs(pos+1,sum*10+s[pos]-'0',num+1);
}

int main()
{
    while(cin>>s)
    {
        flag = false;
        dfs(0LL,0LL,0LL);
        if(!flag) cout<<"NO"<<endl;     //沒有的情況
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章