UVa10541 - Stripe(动态规划,大整数加法)

You are given a rectangle 1*N, where its 1*1 squares can be painted as white or black. So, one can build a "code" of this rectangle - this will be a sequence of numbers, the number of consequent black squares from left to right.

For example, the code of this rectangle is 2 3 2 8 1. However, the number of white squares is not counted anywhere (but two groups of black squares must be separated by at least one white square). That is why, there are a few rectangles which can satisfy the same code. For example, the following rectangle satisfies the given code as well.

The problem is to calculate the number of rectangles, which can satisfy the given code.

Input
There can be multiple test cases. The first line of the input gives you the number of test cases T (1< T < 20). In the next T lines you would have the input for each of the test cases. Each test case consists of N the length of a rectangle (1 <= N <= 200). Then K - the number of numbers in a code (0 <= K <= (N+1)/2). Then K numbers, which represent the code itself.

Output
The output consists of one number - the number of rectangles, which can satisfy the given code. You can assume that the output will always fit in a 50 digit integer.

Sample InputSample Output
3
4 0
5 2 1 2
4 2 2 2
1
3
0

题意:给出一个1行N列的矩形和K个code,问有多少种方式生成这k个code

思路:动态规划算法,其状态转移方程为:f(i,j)表示在第i步中在第j个位置上放置第a[i]个code的方法的个数,f(i,j) += f(i - 1, k)其中k表示在上一步中可行的位置

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.math.BigInteger;

public class Main implements Runnable{
	private static final boolean DEBUG = false;
	private static final int MAXN = 210;
	private Scanner cin;
	private PrintWriter cout;
	private int n, k;
	private int[] a;
	private BigInteger[][] f = new BigInteger[MAXN][MAXN];
	
	private void init() 
	{
		try {
			if (DEBUG) {
				cin = new Scanner(new BufferedInputStream(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new Scanner(new BufferedInputStream(System.in));
			}

			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private boolean input() 
	{
		n = cin.nextInt();
		k = cin.nextInt();
		a = new int[k + 1];
		
		for (int i = 1; i <= k; i++) {
			a[i] = cin.nextInt();
		}
		
		return true;
	}

	private void solve() 
	{
		for (int i = 0; i <= n; i++) {
			Arrays.fill(f[i], BigInteger.ZERO);
		}
		
		f[0][0] = BigInteger.ONE;
		
		if (k > 0)
		for (int i = 1; i <= n; i++) {
			if (i + a[1] <= n) f[1][i] = BigInteger.ONE;
		}
		
		for (int i = 2; i <= k; i++) {
			for (int j = 1; j <= n; j++) {
				if (f[i - 1][j].compareTo(BigInteger.ZERO) != 0) {
					for (int k = 1; k <= n; k++) {
						if (j + a[i - 1] - 1 + k + a[i] <= n) {
							f[i][j + a[i - 1] + k] = f[i][j + a[i - 1] - 1 + k].add(f[i - 1][j]);
						}
					}
				}
			}
			
		}
		
		BigInteger ans = BigInteger.ZERO;
		for (int i = 0; i <= n; i++) {
			ans = ans.add(f[k][i]);
		}
		
		cout.println(ans.toString());
		cout.flush();
	}

	public void run()
	{
		init();
		
		int cas;
		cas = cin.nextInt();
		
		while (cas-- > 0) {
			input();
			solve();
		}
	}
	
	public static void main(String[] args) 
	{
		new Thread(new Main()).start();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值