"Algorithmic Learning Notes" 43. Dynamic programming Converse thinking SJTU OJ 1012 growth rate problem

Source: Internet
Author: User
Tags repetition

1012. Growth rate Issue Description

There is a sequence, which is made up of natural numbers and is strictly monotonically rising. The smallest number is not less than S, and the largest is not more than T. It is now known that this sequence has a property: The next number is always the percentage of the growth rate relative to the previous number (e.g. 5 is 25%,25 to 4, and 9 to 7 is not). Now ask: How long can this sequence last? What is the number of columns that meet the longest requirement?

Input Format

Enter only one row, containing S and T two numbers ( 0<S<T≤200000 ).

30% of data, 0< Span id= "mathjax-span-13" class= "Mo" ><s <t ≤100  

100% of data,0<S<T≤200000.

Output Format

The output has 2 rows. The first line contains a number representing the length, and the second row contains the number of digits.

Sample Input
2 10
Sample Output
52
Sample explanation

2 4 5 6 9 and 2 4 5 8 10

Look at the first feeling is definitely to use DP, so think from S to t problem, how to turn s to T-1 problem?

First we need to know how many of the longest sequences of s to T-1 are, who their ends are, and then compare T to their end to see if they can be connected, so as to decide whether to increase the length and whether to update the number of the longest length.

Thus, the state of our DP is D[i] indicates the length of the longest sequence ending with I and times[i] the number of the longest sequence ending with I and the number of Cnt[x] series with the length X

Algorithm process:

From S to T traverse each starting point I

There are two ways to go: forward thinking we can traverse each number from i+1 to T to compare with I to determine if the condition is met, but this will definitely time out, because the amount of data is too large.

Another idea is reverse thinking, and our final judgment is that we need to find the percentage of growth rate as the number of integers.

Assuming that the integer is J, then the determined number of TMP is the relationship to I

(tmp-i)/i = j/100 finishing can get TMP = i + i*j/100 (you can see that the TMP is an integer condition requirement i*j%100==0)

After getting this TMP (must be less than or equal to T), start the DP State transfer

The state transition relationship is as follows:

  First of all, our TMP is added after the end sequence with I, the length is d[i]+1, the longest sequence length and number of times the end of TMP may be updated

If D[TMP] and d[i]+1 are equal, the longest length of the sequence ending with TMP is consistent with the length of the sequence that was just obtained

So times[tmp] + = times[i]//In the d[i of this sequence] part of the repetition degree is times[i] (>=1)

If d[tmp]<d[i]+1, indicates that the new sequence length is longer than the original, so not only to update the longest length d[tmp] but also to update the maximum length of repetitions times[tmp]

D[TMP] = D[i] + 1

TIMES[TMP] = Times[i]

After that we want to update the total maximum length Ans:ans = max (ans,d[tmp])

Update sequence number of the current sequence's length cnt[d[i]+1] + = tims[i] * 1; (* * is to better explain times[i] is the d[i] part of the repetition of 1 indicates the degree of repetition of the TMP, and a part of 1285 is much like)

At this point the state transition ends

Finally, just output ans and cnt[ans]

PS: The point is, why J from 1 to 100. The coverage range from 1% to 100% is exactly the part I to 2i.

So why is 2i not 3i? For example, for example, the length of a sequence x,x,x,x,x,i,x,x,x,3i must not be x,x,x,x,x,i,x,,2i,x,x,3i long, so consider the optimal solution as long as I to 2i to find the next one can be.

Of course, more rigorous mathematical proof will be more troublesome, need to prove that after 2i each can meet the conditions of the TMP can be converted from i,tmp can be at least i,x,tmp structure where x is less than 2i. Have time to prove it, anyway AC.

The code is as follows:

#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespaceStd;typedefLong Longll;Const intmaxn=200007;intD[maxn];ll CNT[MAXN],TIMES[MAXN];//Cnt[i] Stores the number of sequences with a length of I//D[i] Stores the longest length of a sequence ending with IintMain () {ints,t; CIN>>s>>T; memset (CNT,0,sizeof(CNT)); inti,j,tmp,ans=1; cnt[1]=t-s+1;  for(i=s;i<=t;i++) D[i]=times[i]=1;//initialized to 1     for(i=s;i<=t;i++)//iterate through each number as a starting point         for(j=1; j<= -; j + +)if((i*j)% -==0)//Assuming a growth rate of j%        {            //The number obtained at this growth rate is TMP and it is shown that to make TMP an integer must i*j be a multiple of 100TMP = i + i*j/ -; if(tmp <= T)//If this TMP is within the specified range, we'll find a solution.            {                if(d[i]+1> D[tmp])//compare to see if you want to update the longest length that ends with TMP{d[tmp]= D[i] +1;//The length of the sequence ending with TMP is the length of the sequence ending with I, plus 1 to calculate the TMPTIMES[TMP] = times[i];//The maximum length of The update has occurred so to reset the maximum length that ends with TMP to repeat the number of I                }                Else if(d[i]+1==D[TMP])//is exactly the longest length of the repetition{times[tmp]+ = Times[i];//The longest length increases the number of repetitions with the longest length ending with I} ans= Max (ans,d[tmp]);//Update ans//d[i]+1 indicates the sequence length ending with i +tmp increase the number of 1-bit child columns of this length to be increased times[i]cnt[d[i]+1] +=Times[i]; }} cout<<ans<<Cnt[ans]; return 0;}

"Algorithmic Learning Notes" 43. Dynamic programming Converse thinking SJTU OJ 1012 growth rate problem

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.