Title Description:
An integer can be split into the sum of 2 powers, for example:
7 = 1+ 2 +
4
7 = 1 + 2
+ 2 + 2
7 = 1 + 1
+ 1 + 4
7 = 1 + 1
+ 1 + 2 + 2
7 = 1 + 1
+ 1 + 1 + 1 + 2
7 = 1 + 1
+ 1 + 1 + 1 + 1 + 1
There are six different ways to split
Another example: 4 can be split into:
4 = 4, 4 = 1+1+1+1, 4 = 1+1+2, 4 =.
Use f (n) to denote the number of different splits of N, e.g. F (7)
= 6.
Requirements:
Required to read n (not more than 1000000), output f (n)
% 1000000000.
Thinking Analysis:
- This topic has no direct formula to use, consider recursion
- Using the idea of dynamic programming
- When that 2k +1,f (2k+1) certainly contains a 1,f (2k+1) =f (2k)
- n is even when the split is divided into 1 and does not contain 12 cases, f (2k) = f (2k-2) +f (k), containing 1 of the case, must contain at least two 1, not including 1 is divided by 2, is f (k)
- Also note the output to% 1000000000, note the remainder of the nature: (a+b)%m = (a%m+b%m)%m, so as long as each intermediate result also take the remainder, there will be no overflow problem, and will not change the final output results
-
Code:
Import Java.util.Scanner; Public classMain {Static int[] num = {1,2,5,Ten, -, -, -}; Public Static void Main(string[] args) {Scanner scan =NewScanner (System.inch); while(Scan.hasnext ()) {intinput = Scan.nextint ();if(Input <1){return; } System. out. println (GetNumber (input)); } } Public Static int GetNumber(intN) {if(n = =1){return 1; }if(n = =2){return 2; }if(n%2!=0){returnGetNumber (n1)%1000000000; }Else{returnGetNumber (n2)%1000000000+getnumber (n/2)%1000000000; } }}
My QR code is as follows, welcome to exchange discussion
You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:
Huawei Machine Question "9"-integer divided into 2 power times