Acdream 1417 Numbers (Violent enumeration)

Source: Internet
Author: User
Tags integer numbers

Transmission Door
Numbers
Time limit:2000/1000ms (java/others) Memory limit:128000/64000kb (java/others)
Submit Statistic Next problem
Problem 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

Multiple sets of data
Source

Andrew Stankevich Contest 22

Main topic:
give you a number n (n<10^18), which allows you to ask for the smallest of the least N of the dictionary order to be divisible by K
Problem Solving Ideas:
Need to note here is the dictionary order that is 1001:123 small, in fact, we can enumerate 10^p (p<=18), then up to 18 bits, we put 10^p after the first to K modulo equals 0 of the number of digital groups recorded (note that the enumeration does not exceed N), The final answer must be in here, and we'll just have to convert it to a string to do the dictionary sequence.

My Code:

#include <iostream>#include <cstring>#include <algorithm>Using namespace Std;typedefLong LongLL;Char Str[ -][ -];string s[ -]; LL a[ -];intMain () {LL n, K; while(cin>>n>>k) {if(!n &&!k) Break;intp =0; LL TP = n; while(TP)            {p++; TP/=Ten; } TP =1; for(intI=0; i<p-1; i++) TP *=Ten;intCNT =0; LL tmp = tp%k;if(TMP) TMP = TP+K-TMP;ElseTMP = TP;if(TMP <= N) a[cnt++] = tmp; TP/=Ten; while(TP) {tmp = Tp%k;if(TMP) TMP = TP+K-TMP;ElseTMP = TP;if(TMP < tp*Ten) a[cnt++] = tmp; TP/=Ten; } for(intI=0; i<cnt; i++) {int sum=0; while(A[i]) {intTP = a[i]%Ten;StrI [sum+ +] = tp+' 0 '; A[i]/=Ten; }StrI [sum] =' + '; } for(intI=0; i<cnt; i++) {intLen = strlen (Str[i]);///cout<<str[i]<<endl;             for(intj=0; j<len/2; J + +) {CharCH =StrI [len-j-1];StrI [len-j-1] =StrI [j];StrI            [j] = ch; }        } for(intI=0; i<cnt; i++) S[i] =Str[i];        Sort (s, s+cnt); cout<<s[0]<<endl; }return 0;}

Acdream 1417 Numbers (Violent enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.