Fzu Problem 2200 Cleaning DP

Source: Internet
Author: User

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 Input2 4 2 8 3 Sample Output4 16

Well, this is the day of the month, the dog. A problem matrix fast power constant card think Day dog! At first, the problem was considered linear, so the DP equation was written: dp[i][j] = dp[i-3][j-1] + dp[i-4][j-2]+ dp[i-1][j];DP [I][j] indicates the number of individual legal schemes of the former I personal choice J Well, it's a pretty good look, right? However, the second example 16 can not run out, spent almost half an hour to find the ring, but it does not matter 233333 in front of the code may not be rewritten, found that assuming the first lattice and the end of the lattice is not put, only the first or the end, the first and the end of all put in altogether four cases, Discussion can be O (1) into a linear and then the boundary to find good trouble, simply less than 6 of all the table, so write down such an ugly code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#defineMAXN 2000#defineMOD 1000000007using namespacestd;Long LongDP[MAXN][MAXN];intMain () {intT; intn,k; scanf ("%d",&t); dp[1][1]= dp[1][0] = 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(intI=4; i<= +; i++) {dp[i][0] =1; dp[i][1] =i;  for(intj=2; j<= (i); J + +) {Dp[i][j]= ((dp[i-3][j-1] + dp[i-4][j-2])%mod + dp[i-1][J])%MOD; }    }    intan[ -][ -]={0}; an[1][0]=an[1][1]=1; an[2][0]=1; an[3][0]=1; an[3][1]=3; an[3][2]=0; an[4][0]=1; an[4][1]=4; an[4][2]=4; an[5][0]=1; an[5][1]=5; an[5][2]=5; an[6][0]=1; an[6][1]=6; an[6][2]=9;  while(t--) {scanf ("%d%d",&n,&k); Long Longans=0; if(n<=6) {printf ("%d\n", An[n][k]); Continue; } ans= ((dp[n-2][K] +2* (dp[n-5][k-1]+ (dp[n-6][k-2]))%mod)%mod+ dp[n-6][k-2])%MOD; //cout<<fac[k]<< "" <<fac[n-k]<<endl;printf"%i64d\n", ans); }    return 0;}

Fzu Problem 2200 Cleaning DP

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.