题目链接:https://codeforc.es/contest/1465/problem/B
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
4
1
282
1234567890
1000000000000000000
output
1
288
1234568040
1000000000000000000
分析
我们可以发现, fair number 之间相隔的距离并不是很远,所以可以直接暴力将答案算出来。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N = 1e18;
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
ll begin;
scanf("%lld",&begin);
for(ll i=begin;i<=N;i++)
{
ll t = i;
ll flag = 1;
while(t)
{
ll x = t % 10;
t /= 10;
if(x == 0) continue;
if(i % x != 0) flag = 0;
}
if(flag == 1)
{
printf("%lld\n",i);
break;
}
}
}
return 0;
}