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


你可能感兴趣的:(ACdream)