Numbers
Time Limit: 2000/1000MS (Java/Others)
Memory Limit: 128000/64000KB (Java/Others)
Problem Description
Consider numbers from 1 to n.
You have to find the smallest lexicographically number among them which is divisible by k.
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
多组数据
Source
Andrew Stankevich Contest 22
Manager
题意: 给一个数 n(n<10^18),求出字典序最小的不超过 n 的能够整除 k 的最小值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define ll long long
int main()
{
ll n,k,a[20];
char str[20][20];
string s[20];
while(cin>>n>>k)
{
if(!n) break;
int x1=0,x2=0,cnt=0;
ll m=k,q=n;
while(q)
{
x1++;
q/=10;
}
while(m)
{
x2++;
m/=10;
}
ll p=1;\
a[cnt++]=k;
for(int i=1; i<x2; i++)
p*=10;
for(int i=x2; i<x1; i++)
{
p*=10;
if(p%k==0) a[cnt++]=p;
else if(p+k-p%k<=n) a[cnt++]=p+k-p%k;
}
for(int i=0; i<cnt; i++)
{
int sum=0;
while(a[i])
{
int tmp=a[i]%10;
str[i][sum++]=tmp+'0';
a[i]/=10;
}
str[i][sum] = '\0';
}
for(int i=0; i<cnt; i++)
{
int len=strlen(str[i]);
for(int j=0; j<len/2; j++)
{
char ch=str[i][len-j-1];
str[i][len-j-1]=str[i][j];
str[i][j]=ch;
}
}
for(int i=0; i<cnt; i++)
s[i]=str[i];
sort(s, s+cnt);
cout<<s[0]<<endl;
}
return 0;
}