FOJ 2200 Cleaning (ring DP)

Source: Internet
Author: User
Problem 2200 Cleaning problem Description

n people in a circle in the discussion of the big sweep of things, need to elect the K-man. But everyone has a contradiction with his distance of 2 people, so the distance of any two of the K-man cannot be 2, and they want to know how many methods there are. Input

The first line contains a number T (t<=100), which indicates the number of test data.

Next, there are two numbers n,k,n for each line, and K indicates the number of people needed (1<=n<=1000,1<=k<=n). Output

The output satisfies the test instructions scheme number, the scheme number is very large, so please output the program number mod 1,000,000,007 result. Sample Input 3 sample Output 416

First of all, consider the linear, the ring is actually a deformation of the line, Dp[i][j] means the former I personally take J personal situation, then dp[i][j]=dp[i-1][j]+dp[i-4][j-2]+dp[i-3][j-1]; DP[I-1][J] indicates that I am not selected//dp[i-4][j-2] to select I and i-1 individuals, do not select the I-2 individual//dp[i-3][j-1] to select the first person, do not select I-1 and i-2 next change line into a ring, is 1th and nth together (1 When 1,n is not selected, it is dp[n-2][k]; (2) When 1,n is selected, for Dp[n-6][k-2] (3) Elected 1 does not select N, this situation to consider the 2nd choice, the total is Dp[n-6][k-2]+dp[n-5][k-1] (4) elected n 1 O'Clock, and (3) is the same;

AC Code:

# include <stdio.h>
# include <string.h>
typedef long Long int ll;
const int mod=1000000007;
int dp[1010][1010];
int a[10][10];
int main () {
	int i, j, K, N, T;
	memset (DP, 0, sizeof (DP));
	Dp[1][0]=dp[1][1]=dp[0][0]=1;
	Dp[2][0]=1;dp[2][1]=2;dp[2][2]=1;
	dp[3][0]=1;dp[3][1]=3;dp[3][2]=2;
	for (i=4; i<=1000; i++) {
		dp[i][0]=1;
		dp[i][1]=i;
		for (j=2; j<=i; J + +) {
			dp[i][j]= ((Dp[i-1][j]+dp[i-4][j-2])%mod+dp[i-3][j-1])%mod;}
	}
    A[1][1]=1;a[2][1]=1;a[2][2]=1;
    a[3][1]=3;a[3][2]=3;a[4][1]=4;
    a[4][2]=4;a[5][1]=1;a[5][2]=5;
	scanf ("%d", &t);
	while (t--) {
		scanf ("%d%d", &n, &k);
		if (n<6) {
			printf ("%d\n", A[n][k]);
			Continue;
		}
		ll Ans=0;
		ANS=ANS+DP[N-2][K];
		ANS=ANS+DP[N-6][K-2];
		ANS=ANS+DP[N-6][K-2]+DP[N-5][K-1];
		ANS=ANS+DP[N-6][K-2]+DP[N-5][K-1];
		printf ("%d\n", (int) (ANS%MOD));
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.