Many problem solutions on the Internet are not understood, and some of the "obvious" conclusions in the solutions are completely unobvious...
If we enumerate the ones digit of the first number of this sequence, then if this sequence exists, it must be constructed. (Because k<=9, the length of the sequence is <=10, and it will only be carried once at most)
1. When there is no carry in this sequence and the last digit of the first number is now, it can be expressed as: a+now, a+now+1, a+now+2,… ,a+now +k. (Among them, because there is no carry, so now+k<=9, and a means that they have the same highest order to the tenth place)
In this case, after knowing now, k and n, it can be obtained.
if(now+k<=9){
int last=n-(now*(k+1)+(1+k)*k/2);if(last<0|| last%(k+1)!=0)continue;
last/=(k+1);int num=now,bin=1;while(last){
bin*=10;if(last>=9) last-=9,num=bin*9+num;else num=bin*last+num,last=0;}
ans=min(ans,num);}
2. When there is a carry in this sequence, how to find it?
There is a solution to the problem written like this:
But I have never understood why I can be so greedy. This kind of greedy optimality is understandable, but the correctness has never been understood and will not prove. How to ensure that such filling can cover all situations?
Let us first set: the highest digit of the first number to the tens place is a, and the highest digit of the last number to the tens place is b. (a+1=b, because it is after the carry)
So, for the sum of digits, what is the relationship between a and b?
If there is no carry between a and b, then sum(a)+1=sum(b).
If there is a carry, then the last few bits of a must be consecutive 9s. When there are i consecutive 9s at the end, sum(a)-i*9+1=sum(b).
So we can enumerate how many consecutive 9s there are at the end, this will get the first number of the sequence.
Some students may ask, why do you want to enumerate how many consecutive 9s are there at the end? Isn't this also a conclusion? Isn't there no proof?
Fill in as many 9s as possible. This should be obvious, right? But we are not sure, after filling in a few 9s, whether there must be a corresponding sequence of numbers, so we enumerate the number of 9s.