#698 (Div. 2)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,1<=d<=9。任何一个数x中只要有一位是d,则称x就是幸运数字,现在给你一个数判断他是否可以用一个或者多个幸运数字的和表示。

题目分析

先说结论:如果x的个位数等于某个d的倍数的个位,并且x大于等于这个d的倍数,那么x符合要求

下面证明一下这个结论。
假设x是一个符合要求的数, x > n ∗ d x>n*d x>nd并且x%10==n*d%10 。我们来看看x可以分解为哪些幸运数字。

x = [ ( n − 1 ) ∗ d ] + [ ( x / 10 − ( n ∗ d / 10 ) ) ∗ 10 + d ] x=[(n-1)*d]+[(x/10-(n*d/10))*10+d] x=[(n1)d]+[(x/10(nd/10))10+d]

x可以分解成n-1个d和 [ ( x / 10 − ( n ∗ d / 10 ) ) ∗ 10 + d ] [(x/10-(n*d/10))*10+d] [(x/10(nd/10))10+d] 这n个数。而不符合上述结论的数则无法进行分解。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#define LL long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=2e5+5;
bool check(int x,int d)			//判断x是否合法的函数
{
    
    
	if(x>=10*d) return true;	//nd(1<=n<=10)的个位数包含了0-9所有的个位数,因此大于等于10*d的x一定是合法的
	for(int i=1;i*d<=x;i++)				//枚举所有小于等于x的d的倍数
		if(i*d%10==x%10) return true;	//如果有符合条件的则返回true
	return false;
}
int main()
{
    
    
    int t;
	scanf("%d",&t);
	while(t--)
	{
    
    
		int n,d;
		scanf("%d%d",&n,&d);
		while(n--)
		{
    
    
			int x;
			scanf("%d",&x);
			if(check(x,d)) puts("YES");
			else puts("NO");
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/113395605