ACdream 1417 Numbers

Description

      Consider numbers from 1 to n. 
      You have to find the smallest  lexicographically number among them which is divisible by k.

Input

      Input file contains several test cases. Each test case consists of two integer numbers n and k on a line(1 ≤ n ≤ 1018, 1 ≤ k ≤ n).

      The last test case is followed by a line that contains two zeroes. This line must not be processed.

Output

      For each test case output one integer number — the smallest lexicographically number not exceeding n which is divisible by k.

Sample Input

2000 17
2000 20
2000 22
0 0

Sample Output

1003
100
1012

Hint

多组数据


#include<stdio.h>
#include<string.h>
long long n,k,i,j,f[50],a[50],u,kk;

int main()
{
	for (i=f[1]=1;i<20;i++) f[i+1]=f[i]*10;
	while (~scanf("%lld%lld",&n,&k),n+k)
	{
		for (i=1;f[i]<k;i++);
		for (j=1,a[1]=k;a[j]<=n&&a[j]>0;i++)
		{
			a[++j]=f[i] % k ?k-f[i] % k+f[i]:f[i];
		}

		for (i=2,kk=a[1],u=1;i<j;i++)
		{
			kk*=10;
			if (kk>a[i]) {kk=a[i]; u=i;}
		}

		printf("%lld\n",a[u]);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值