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。
 
 import java.util.*;
import java.math.*; public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); BigInteger N = in.nextBigInteger(); BigInteger F[][] = new BigInteger[125][125];
BigInteger G[][] = new BigInteger[125][125]; for (int i = 0; i <= 120; ++i)
for (int j = 0; j <= 120; ++j) {
F[i][j] = BigInteger.ZERO;
G[i][j] = BigInteger.ZERO;
} F[0][0] = BigInteger.ONE; for (int i = 1; i <= 120; ++i) {
F[i][i] = BigInteger.ONE; for (int j = 0; j < i; ++j)
for (int k = 0; k <= j; ++k)
F[i][j] = F[i][j].add(F[i - 1][k].multiply(F[i - k - 1][j - k]));
} int tot = 0; BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE); for (int i = 0; i <= 120; ++i) {
if (N.mod(TWO).toString().charAt(0) == '1') {
if (++tot == 1) {
for (int j = 0; j <= i; ++j)
G[tot][j] = F[i][j];
}
else {
for (int j = 0; j <= i; ++j)
for (int k = 0; k <= j; ++k)
G[tot][j] = G[tot][j].add(G[tot - 1][k].multiply(F[i - k][j - k]));
}
} N = N.divide(TWO);
} BigInteger ANS = BigInteger.ZERO; for (int i = 0; i <= 120; ++i)
ANS = ANS.add(G[tot][i]); System.out.println(ANS.toString());
}
}

@Author: YouSiki

上一篇:测试进阶之路:pywinauto助你打开pc端自动化之门!


下一篇:基于 opencv 的图像处理入门教程