Fair Numbers

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值