B. Fair Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

We call a positive integer number fair if it is divisible by each of its nonzero digits. For example, 102 is fair (because it is divisible by 1 and 2), but 282 is not, because it isn't divisible by 8. Given a positive integer n. Find the minimum integer x, such that n≤x and x is fair.

Input

The first line contains number of test cases t (1≤t≤103). Each of the next t lines contains an integer n (1≤n≤1018).

Output

For each of t test cases print a single integer --- the least fair number, which is not less than n.

Example

Input

Copy

复制代码
4
1
282
1234567890
1000000000000000000

Output

Copy

复制代码
1
288
1234568040
1000000000000000000

Note

Explanations for some test cases:

  • In the first test case number 1 is fair itself.
  • In the second test case number 288 is fair (it's divisible by both 2 and 8). None of the numbers from 282,287 is fair, because, for example, none of them is divisible by 8.

解题说明:此题是一道数学题,直接从 n开始逐个递增检查,直到找到第一个满足条件的数即可。针对每个数字,判断其中的每一位,如果为0就跳过,否则进行整除判断。

cpp 复制代码
#include<stdio.h>
int main()
{
	int t;
	long long int a, k;
	scanf("%d", &t);
	while (t) 
	{
		scanf("%lld", &a);
		k = a;
		while (k != 0)
		{
			if (k % 10 == 0)
			{
				k = k / 10;
			}
			else
			{
				if (a % (k % 10) == 0)
				{
					k = k / 10;
				}
				else 
				{
					a++; 
					k = a;
				}
			}
		}
		printf("%lld\n", a);
		t--;
	}
	return 0;
}
相关推荐
人道领域2 小时前
【LeetCode刷题日记】93.复原IP地址
java·开发语言·算法·leetcode
jarreyer2 小时前
【算法记录1】模型训练问题
算法
Felven2 小时前
D. Friends and the Restaurant
算法
想吃火锅10052 小时前
【leetcode】165.比较版本号js
javascript·算法·leetcode
San813_LDD2 小时前
[量化]《浮点数比较的艺术:从内存布局到极致性能优化》
网络·算法
ysu_03142 小时前
leetcode数据结构与算法1~4
c语言·数据结构·学习·算法·leetcode
小欣加油2 小时前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
PH = 72 小时前
动态规划-求最优解-自底向上
算法·动态规划
用户497863050732 小时前
前缀和与差分
算法