码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1513 Lemon Tale

时间:2018-01-31 22:06:41      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:[]   pre   system   blank   scanner   降维   int()   mat   nbsp   

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]);
    }

}

 

URAL 1513 Lemon Tale

标签:[]   pre   system   blank   scanner   降维   int()   mat   nbsp   

原文地址:https://www.cnblogs.com/widsom/p/8394784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!