zoukankan      html  css  js  c++  java
  • Codeforces Round #698 (Div. 2) B. Nezzar and Lucky Number

    B:http://codeforces.com/contest/1478/problem/B

    题意:

    给你一个 d q 次询问一个数字 a 是否可以由若干个数字相加得到且这些数字的数位中都含有 d  这个数字。

    解析:

    如果a%d==0,直接YES

    否则,

    举个例子:d=3,a=16

    16=d*5+1

    可见, 1 最多加5次d,1+3+3+3+3   +3 = = 13+3,为可行解

    总结一下,如果一个a为符合条件的,那么它一定可以分出来若干个d+y的形式,那么就让a%d一直加d,总能得出一个符合条件的数y来。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<map>
    using namespace std;
    const int maxn  = 2e5+50;
    const int inf=99999999;
    typedef long long ll;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n,d;
            cin>>n>>d;
            ll x;
            while(n--)
            {
                cin>>x;
                int ok  =  0 ; 
                if(d==1)
                {
                    cout<<"YES"<<endl;continue;
                }
                if(x%d==0)
                {
                    cout<<"YES"<<endl;continue;
                }
                ll cha =  x-(x/d)*d;
                ll chu = x/d;
                ll sum =cha ;
                for(int i=1;i<=chu;i++)
                {                
                    sum+=d;
                //    cout<<d<<"-"<<i<<endl;
                    ll mdsum=sum;
                    while(mdsum)
                    {
                        
                        ll md=mdsum%10;
                    //    cout<<md<<"--"<<mdsum<<endl;
                        mdsum=mdsum/10;
                        if(md==d)
                        {
                            ok=1;
                            break;
                        }
                    }
                    if(ok)
                        break;
                }
                if(ok)
                    cout<<"YES"<<endl;
                else
                    cout<<"NO"<<endl;
            }
        }
        return 0;    
    }
    //ababab
  • 相关阅读:
    c语言cgi笔记
    End of script output before headers错误解决方法
    我的树莓派3配置脚本
    Qt学习(4)
    Qt学习(3)
    Qt学习(2)
    Qt学习(1)
    C++ Primer中文版(第五版)——第六章 函数
    C++ 11 ----Lambda表达式
    Java SPI 源码解析
  • 原文地址:https://www.cnblogs.com/liyexin/p/14344933.html
Copyright © 2011-2022 走看看