Codeforces Round #698 (Div. 2) B. Nezzar and Lucky Number(思维)

本文解析了如何通过编程判断一个整数是否能表示为多个幸运数字(数字d在十进制中出现至少一次)的和,涉及基础数学分析和代码实现,重点在于小于d*10范围内的判断策略。

题目链接: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. 1 * 7 = 7,大于等于 7 的个位上为 7 的数都能表示
  2. 2 * 7 = 14,大于等于 14 的个位上为 4 的数都能表示
  3. 3 * 7 = 21,大于等于 21 的个位上为 1 的数都能表示
  4. 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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值