51nod 1383&1048 integer decomposition to power of 2 [recursive] "math"

Source: Internet
Author: User
Tags time limit

Topic Connection: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1048

————————————————————————————————————-.
Integer decomposition to a power of 2

Base time limit: 3 seconds Space limit: 131072 KB score: 1280 Difficulty: 9-level algorithm problem

Any positive integer can be decomposed into a power of 2, given an integer n, to find the number of such partitioning methods of N.
For example, n = 7 o'clock, there are 6 ways of partitioning.

7=1+1+1+1+1+1+1
=1+1+1+1+1+2
=1+1+1+2+2
=1+2+2+2
=1+1+1+4
=1+2+4

Input
Input n (1 <= n <= 10^30)

Output
Number of partitioning methods

Input example

7

Output example

6
————————————————————————————————————-.

First you can DP

Set DP[I][J] indicates that the number of components is I I, the largest number in the scheme is the scheme number of 2j 2^j.
To avoid duplication, Dp[i][j] The next number of enumerations must be greater than or equal to 2j when transferring 2^j
Then you can conclude: Dp[i][j]=∑jk=0dp[i−2j][k] dp[i][j]=\sum^{j}_{k=0}dp[i−2^j][k]

Ability to achieve O (Nlog (n)) O (N\log (n)) complexity

By playing the table to find the law, etc., you can recursively get the following rules

Dp[i]=dp[i−2]+dp[i/2],i to even dp[i]=dp[i−1],i is odd dp[i] = Dp[i-2] + DP[I/2], I is even \ \ dp[i] = Dp[i-1], I is odd

The complexity of this is O (n) o (n)

However, the complexity of 51nod 1383 is sufficient, but for 1048 of N (1030) n (10^{30}), it is completely inadequate,

What if n is very big.
That would have to dig up the nature of the decomposition scheme.

1. For a number n, it is assumed that the binary has M 1, respectively, the a1,a2...am a_1,a_2...a_m bit. For any one of n decomposition schemes, the power of all 2 is sorted in ascending order, then can be divided into M-segment, where the sum of paragraph I is 2ai 2^{a_i}
2. For a number 2i 2^i of a partition scheme, if it is not only a number of itself, it is possible to put these 2 powers in ascending order, and then divided into two segments, each paragraph and is 2i−1 2^{i−1}

Proving not difficult. With these properties, you can set a new state.

First set g[i][j] means that the first segment is done (i.e. the first I 1 bits under n binary), the largest number is 2j 2^j, and the number of programs.
Set up an auxiliary array f[i][j], representing

Related Article

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.