Lucky number 4&7: Given a positive integer n, the number of numbers consisting of only 4 and 7 that are not greater than n __java

Source: Internet
Author: User

Topic Description: We call a decimal positive integer a lucky number when and only if it is composed only of numbers 4 and 7. Now given a positive integer n, you need to calculate how many lucky numbers are not greater than n, because the answer can be very large, you only need to output the answer divided by 1000000007 after the remainder.

Input: The first line contains an integer n,1<=n<=10^100000

Output: Output corresponds to the answer

Test Sample:

125

6


21857711

254


The simple idea: one by one the poor lift

Result: timeout, because the number is too large

Train of thought by someone who does not know the name of the great God, I would like to express my thanks to O (∩_∩) o Thank you, train of thought description:

Symbol Description: The following symbol, 7xx means X is 4, or 7, that is, 744,777,747,774, a total of 2^2 possibility to meet the meaning.

Train of thought: Since the numbers consist only of 7 and 4, the number satisfying the requirements is considered to be a "binary" number (7->1,4->0) composed of 7 and 4, so that when the input number is n, there is at least a case.

For example: 441,,444,777,123,731,491,594,982, the length is 3, the minimum number is 6, respectively, 4,7,44,47,74,77. So, you need to scan from the top to the right, you judge the situation:

CASE1: Current position >7, indicating that this position is 7 or 4 can satisfy the question (for example, 982, then the 7xx,4xx type are all satisfied with the answer;), so the answer is increased 2^ (n-i), I is subscript for the current position (such as 491,i=1, 9>7, then 47x, 44x all meet the requirements, so the answer to increase 2^ (3-1) = 4), and do not have to be judged backwards. (because from the example above, the following results have been included because of the 491>4xx)

CASE2: Current position ==7, this position is 4 to meet the question (for example, 777, then 4xx must meet the answer; 731, then 4xx must satisfy the answer) but for 7xx is satisfied, still need to continue to scan the next judgment; if the last one is 7, then the last 7 or 4 will satisfy the answer. +2 (such as 777,i=0,4xx satisfied; i=1,*4x satisfied; i=3, because is the last one, so can judge 777,774 meet the condition, answer +2)

case3:7> Current position >4, indicating that this location is 4 to meet the order (for example, 594, then 4xx must be satisfied, and 594>4xx so the follow-up does not have to be judged, already included).

CASE4: Current position ==4, similar to ==7 situation, but not continue to scan backwards, cannot be accurately judged (due to the existence of 441, etc.), but when you can scan to the last one and the last one is 4 o'clock (for example, 444), prove 44 ... 4 The situation can exist, the answer +1 (for example, 444, in the case of XX plus 444 in this case)

CASE5: Non 4 non-7, not meet the requirements, stop scanning, the most scan results before return to (such as 441, only xx satisfied; 123, only xx satisfied;)




Program Description: Because 10^100000 is probably equal to 4^160000 (preferably logarithmic estimate), so pre-allocate 160000 memory



Import Java.util.Scanner; public class Lucknum_4and7 {public static void main (string[] args) {//input n<=10^100000 is approximately equal to 4^160000, you can take the logarithm to calculate in
		t[] Bit = new int[160000];
		int[] Sum = new int[160000]; Bit[0] = 1;//2^0 = 1 Sum[0] = 0;//sum[0] = bit[0] for (int i = 1;i<100010;i++) {//Initialize, calculate the power of 2 and sum in advance to facilitate the use of bit[
			I] = bit[i-1]*2% (1000000007);
		Sum[i] = (sum[i-1] +bit[i])% (1000000007);
		} Scanner sc = new Scanner (system.in);
			while (Sc.hasnext ()) {String N = Sc.nextline ();
			int lenth = N.length (); int ans = sum[lenth-1];//shortest length, for example: 143, length three bits, so the length of the two digits must meet the for (int i=0;i<lenth;i++) {//from the highest bit start to judge, Judge 712, 741,474,444,447,132, such as the case (N.charat (i) > ' 7 ') {//current position is greater than the head, to three digits, such as 712, then 4xx of the case (x=4, 7) are satisfied, then the two-bit xx situation ans has been obtained, the third place adds 7
					, 42 kinds of circumstances ans + + sum[lenth-i]; break;//after all satisfied, so do not have to check back}else if (N.charat (i) < ' 7 ' &&n.charat (i) > ' 4 ') {//current bit ==7, then the current bit of 4 must pass,
					The current probability of 7 is still to be judged downward, so the result is added to the situation of 4xx ans = (ans+bit[lenth-i-1])% (1000000007);; break;//because it's 4xx., and then all satisfied, so do not have to check back again}else if (N.charat (i) = = ' 7 ') {ans = (ans+ bit[lenth-i-1))% (1000000007);;/
				/Because for the last one, the addition is bit[0]=1, but the last one should contain two cases xx7 and xx4, so add 1//if (i==lenth-1) ans++;
				}else if (N.charat (i) = = ' 4 ') {if (i==lenth-1) ans= (ans+1)% (1000000007);;
			
		}else{break;//blocked} System.out.println (ans); }
	}
}




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.