当前位置: 代码迷 >> 综合 >> Fair Numbers
  详细解决方案

Fair Numbers

热度:48   发布时间:2023-11-23 07:08:26.0

Fair Numbers

题:

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

Input
The first line contains number of test cases ? (1≤?≤10^3 1≤t≤10^3). Each of the next ? lines contains an integer ? (1≤?≤10^18 1≤n≤10^18).

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

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.

题意:
找到一个数x,n<=x , x 可以被它每个位上的非零数整除,被称为公平数,找到最小的x并输出。

思路:
直接在给出的数 n 上暴力遍历得到 x ,不会超时。
(能被{0,1,2,3,4,5,6,7,8,9}的元素都整除的称为超级公平数,最小超级公平数为2520)
因为{0,1,2,3,4,5,6,7,8,9}的最小公倍数是2520,那么一个数的每一位组成的集合都会属于这个集合,所以他们每一位数组成的集合最小公倍数肯定是小于或者等于2520。
就当最坏情况来算的话,只需要找一个2520的倍数即可,最多也就遍历两千多次。

#include <bits/stdc++.h>
//万能头下,cin、cont和scanf、printf是同步的
#define ll long long
using namespace std;int main()
{
    ios::sync_with_stdio(false);//用来取消与scanf、printf的同步,大大提高运行速度int t;cin>>t;while(t--){
    ll n,m;cin>>n;m=n;int flag=0;while(!flag){
    while(n){
    int x=n%10;if(x!=0){
    if(m%x!=0){
    m++;n=m;break;}}n/=10;}if(n==0)flag++;}cout<<m<<endl;}return 0;
}