URAL 1513

思路:

dp+高精度

状态:dp[i][j]表示长度为i末尾连续j个L的方案数

初始状态:dp[0][0]=1

状态转移:dp[i][j]=dp[i-1][j-1](0<=j<=k)

                dp[i][0]=∑dp[i-1][j](0<=j<=k)

目标状态:dp[n+1][0]

观察转移公式,我们发现,dp[i]只比dp[i-1]多了一个dp[i][0]其他都没变,而dp[i][0]是求和,所以我们可以用栈来模拟达到降维目的,每次求栈顶k+1个数的和放入栈,求n次

代码:

import java.math.BigInteger;
import java.util.*;
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Scanner in=new Scanner(System.in);
            BigInteger dp[]=new BigInteger[10005];
            BigInteger sum=BigInteger.ONE;
            int top=0,n,m;
            n=in.nextInt();
            m=in.nextInt();
            dp[++top]=BigInteger.ONE;
            dp[++top]=BigInteger.ONE;
            for(int i=1;i<=n;i++){
                if(top<=m+1)sum=sum.add(sum);
                else{
                    sum=sum.add(sum);
                    sum=sum.subtract(dp[top-m-1]);
                }
                dp[++top]=sum;
                //System.out.println(dp[top]);
            }
            System.out.println(dp[top]);
    }

}

 

相关文章:

  • 2022-01-19
  • 2020-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2021-12-31
  • 2022-12-23
猜你喜欢
  • 2021-07-03
  • 2021-07-25
  • 2022-12-23
  • 2021-11-30
  • 2021-10-08
  • 2022-12-23
  • 2021-12-05
相关资源
相似解决方案