uva 10328 - Coin Toss

f[i]代表[i,n-1]内长度不超过k的解的个数

f[i]=f[i+1...i+k]

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package pkg10328;



/**

 *

 * @author user

 */

import java.io.*;

import java.math.*;

import java.util.*;





public class Main {



    /**

     * @param args the command line arguments

     */

    public static BigInteger f[]= new BigInteger [105];

    public static BigInteger pow2[]=new BigInteger [105];

    public static void init()

    {

        pow2[0]=BigInteger.ONE;

        pow2[1]=BigInteger.valueOf(2);

        for(int k=2;k<101;k++)

            pow2[k]=pow2[k-1].multiply(pow2[1]);

    }

    public static void main(String[] args) {

        // TODO code application logic here

        init();

        Scanner cin =new Scanner (System.in);

        int i,n,k;

        while(cin.hasNext())

        {

            n=cin.nextInt();

            k=cin.nextInt();

            for(i=n;n-i<k;i--)

            f[i]=pow2[n-i];

            for(;i>=0;i--)

            {

                f[i]=BigInteger.ZERO;

                for(int j=i+1;j<=i+k;j++)

                    f[i]=f[i].add(f[j]);

            }

            System.out.println(pow2[n].subtract(f[0]));

        }

    }

}

  

你可能感兴趣的:(uva)