题目链接:http://codeforces.com/contest/1478/problem/B
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
分析
我们可以想到,如果一个数大于等于 d * 10 ,那么肯定是可以组成的,如 d 为 7 时:80 可以由 73 和 7 组成,81可以由 74 和 7 组成…
在那个范围内一下的,我们可以这样判断,例如 d 为 7 时:
- 1 * 7 = 7,大于等于 7 的个位上为 7 的数都能表示
- 2 * 7 = 14,大于等于 14 的个位上为 4 的数都能表示
- 3 * 7 = 21,大于等于 21 的个位上为 1 的数都能表示
- 4 * 7 = 28,大于等于 28 的个位上为 8 的数都能表示
- …
代码
#include<bits/stdc++.h>
using namespace std;
int t,q,d;
int a[10007];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&q,&d);
for(int i=1;i<=q;i++)
{
scanf("%d",&a[i]);
if(a[i] % d == 0 || a[i] >= d * 10)
{
printf("YES\n");
continue;
}
int flag = 0;
int tmp = a[i];
while(tmp)
{
if(tmp % 10 == d)
{
flag = 1;
break;
}
tmp /= 10;
}
if(flag == 1)
{
printf("YES\n");
continue;
}
flag = 0;
tmp = 0;
for(int j=1;j<=2000;j++)
{
tmp += d;
if(tmp > a[i]) break;
if((tmp % 10) == (a[i] % 10) && a[i] >= tmp)
{
flag = 1;
printf("YES\n");
break;
}
}
if(flag == 0) printf("NO\n");
}
}
return 0;
}
本文解析了如何通过编程判断一个整数是否能表示为多个幸运数字(数字d在十进制中出现至少一次)的和,涉及基础数学分析和代码实现,重点在于小于d*10范围内的判断策略。
1809

被折叠的 条评论
为什么被折叠?



