51Nod 1048 1383 整数分解为2的幂
任何正整数都能分解成2的幂,给定整数N,求N的此类划分方法的数量!
比如N = 7时,共有6种划分方法。
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
傻逼的高精度DP,我只选Java BigInteger。
1 import java.util.*; 2 import java.math.*; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 8 BigInteger N = in.nextBigInteger(); 9 10 BigInteger F[][] = new BigInteger[125][125]; 11 BigInteger G[][] = new BigInteger[125][125]; 12 13 for (int i = 0; i <= 120; ++i) 14 for (int j = 0; j <= 120; ++j) { 15 F[i][j] = BigInteger.ZERO; 16 G[i][j] = BigInteger.ZERO; 17 } 18 19 F[0][0] = BigInteger.ONE; 20 21 for (int i = 1; i <= 120; ++i) { 22 F[i][i] = BigInteger.ONE; 23 24 for (int j = 0; j < i; ++j) 25 for (int k = 0; k <= j; ++k) 26 F[i][j] = F[i][j].add(F[i - 1][k].multiply(F[i - k - 1][j - k])); 27 } 28 29 int tot = 0; 30 31 BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE); 32 33 for (int i = 0; i <= 120; ++i) { 34 if (N.mod(TWO).toString().charAt(0) == '1') { 35 if (++tot == 1) { 36 for (int j = 0; j <= i; ++j) 37 G[tot][j] = F[i][j]; 38 } 39 else { 40 for (int j = 0; j <= i; ++j) 41 for (int k = 0; k <= j; ++k) 42 G[tot][j] = G[tot][j].add(G[tot - 1][k].multiply(F[i - k][j - k])); 43 } 44 } 45 46 N = N.divide(TWO); 47 } 48 49 BigInteger ANS = BigInteger.ZERO; 50 51 for (int i = 0; i <= 120; ++i) 52 ANS = ANS.add(G[tot][i]); 53 54 System.out.println(ANS.toString()); 55 } 56 }
@Author: YouSiki