P2022 Interesting number

Source: Internet
Author: User

P2022 Interesting number topic description

Let's consider a set of positive integers from 1 to n. Let's arrange the elements in the collection in dictionary order, for example, when n=11, it should be: 1,10,11,2,3,4,5,6,7,8,9.

Defines the position of K in the N number as Q (n,k), for example q (11,2) = 4. Now given the integers K and M, it is required to find the smallest N, which makes Q (n,k) =m.

Input output Format input format:

The input file has only one row, which is two integers k and M.

Output format:

The output file has only one row, which is the smallest n, and outputs 0 if no such n exists.

Input and Output Sample input example # #:
Sample 1:2 4Sample 2:100,000,001 1000000000 here Sample 1 and 2 are separate two data points.
Sample # # of output:
Sample 1:11sample 2:1.,000,000,008,888,89e,+17
Description

"Data Contract"

40% of Data,1<=k,m<=10^5;

100% of the data, 1<=k,m<=10^9.

Feel:

I was thinking, was it violence? Double? Two points? ...... The result is the Tao number problem, I C ghost can make? See for yourself.

Exercises

Because the answer can be very large, the problem is obviously not enumerated, even with two points, time complexity {o[n (LOGN) ^2]} is also very large. We can set all the dictionary order than K small number of the first M-1 is X,n is equal to K and X maximum, how to find x? [Delete] is of course an enumeration. [/delete] We divide all dictionaries into infinitely large sets of smaller numbers than K. Any two-digit j,k in the set AI satisfies I=floor (log10 (j)) =floor (log10 (k)) (Floor (a) represents the integer portion of a, log10 (a) represents the base of 10, and A is true for a value), where the maximum value is AI. We can find that the left number I bit of K is pi,qi=∑pj* (i-j+1) (1<=j<=i), when J<=log10 (k), | aj|=qj-1,aj=qj-1; when J>log10 (K), | aj|=| A (j-1) |*10 (please prove it to the reader). This allows us to find the AI in the set of X, and x=ai+[m-∑| aj| (1<=j<=i)] -1. To find the time complexity of the set of X and to find the value of x after the set of X is O[LOG10 (n)], the total time complexity is O[LOG10 (n)].

AC Code:

#include <iostream>using namespacestd;Long LongK,m,i,number,n;intMain () {CIN>>k>>m;  for(i=1; i<=k;i*=Ten) number+=k/i-i+1; number--; if(number>=m| | K (i/Ten)==0&&number<m-1) cout<<0; if(number>=m| | K (i/Ten)==0&&number<m-1)return 0;  for(I=k-(i/Ten), n=k;number<m-1; i*=Ten, number+=i,n*=Ten); cout<<max (n-number+m-2, K); return 0;}

P2022 Interesting number

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.