Counting UVA - 10198
題意:
Gustavo數數時總是把1和4搞混,他認爲4只是1的另外一種寫法。給出一個整數n,Gustavo想知道有多少個數的數字之和恰好爲n。例如,當n=2時,有5個數:11、14、41、44、2。
分析:
設dp[n]表示加和爲n的組合的個數
那麼很明顯這些組合中應該含有含1的組合,那麼去掉這個1的話就是dp[n-1]的個數加上一個1,同理含2 的話有dp[n-2]個數加上一個2,同理dp[n-3]的個數加上一個3,因爲4和1相同所以相當於2倍的dp[n-1]
因此得到遞推式
因爲數會很大又不能取模所以得用高精度
code:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in) ;
BigInteger dp[];
dp = new BigInteger[1024];
dp[1] = new BigInteger("2");
dp[2] = new BigInteger("5");
dp[3] = new BigInteger("13");
for(int i = 4; i <= 1000; i++) {
dp[i] = dp[i-1].multiply(BigInteger.valueOf(2)).add(dp[i-2]).add(dp[i-3]);
}
int n;
while(in.hasNext()) {
n = in.nextInt();
System.out.println(dp[n]);
}
}
}