B. Nezzar and Lucky Number
原题链接:
题意:
Nezzar’s favorite digit among 1,…,9 is d. He calls a positive integer lucky if d occurs at least once in its decimal representation.
Given q integers a1,a2,…,aq, for each 1≤i≤q Nezzar would like to know if ai can be equal to a sum of several (one or more) lucky numbers.
Input
The first line contains a single integer t (1≤t≤9) — the number of test cases.
The first line of each test case contains two integers q and d (1≤q≤104, 1≤d≤9).
The second line of each test case contains q integers a1,a2,…,aq (1≤ai≤109).
Output
For each integer in each test case, print “YES” in a single line if ai can be equal to a sum of lucky numbers. Otherwise, print “NO”.
You can print letters in any case (upper or lower).
Example
input
2
3 7
24 25 27
10 7
51 52 53 54 55 56 57 58 59 60
output
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
NO
Note
In the first test case, 24=17+7, 27 itself is a lucky number, 25 cannot be equal to a sum of lucky numbers.
题解:
题意中给出一个幸运字d,要求后来给出的数都能由含有幸运字的数相加得来,比如当d为8时,86=48+38;当然还有其它的表示方法比如86=28+58,
其实86本身也是一个幸运数字,再举一个例子,200=182+18;可以看出当给出的数大于幸运数字的十倍时总是可以由幸运数字相加得来的,当其小于幸运数字的十倍时每次判断个位是否为幸运数字即可。
代码:
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int q,d;
cin>>q>>d;
while(q--)
{
int a;
cin>>a;
if(a>=10*d)
{
cout<<"YES"<<endl;
}
else
{
int f=1;
for(int i=1;i<=9;i++)
{
if((a - i * d )>= 0 && (a - i * d) % 10 == 0)
{
f=0;
break;
}
}
if(f==1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
}
}