Fzu 2200 Cleaning (ring DP)

Source: Internet
Author: User
Tags time limit
Problem 2200 Cleaning accept:15 submit:27
Time limit:1000 mSec Memory limit:65536 KB
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 Input24 28 3Sample Output416SourceFOJ Prize Month race-October 2015
Title Link: http://acm.fzu.edu.cn/problem.php?pid=2200
Topic Analysis: Again a ring DP, this problem is simpler than the previous one, because the distance of 2 cannot be selected together, because is the ring, directly retains the first second, the penultimate and the penultimate position of the state, total space 8e6 set Dp[a][b][i][j][x][y] The state of the first person for a second person state is B, the former I personally selected J, the i-1 person's status is X, the first person's state is the number of programs for Y, or the state to enumerate the beginning, 2*2, altogether four kinds, the complexity of online DP is 4 * n^2, that is 4e6,t is 100, time is hard, So had to be offline pre-processing, recursion time four cases 1. i-1 not selected but the first choice, the number of programs is equal to I-1 and the i-2 is not selected, because I-1 and i-2 can not be selected at the same time dp[a][b][i][j][0][1] = Dp[a][b][i-1][j -1][0][0] 2. I-1 selected and I have also selected, the number of programmes equal to the i-1 selected and the first i-2 not selected there is dp[a][b][i][j][1][1] = dp[a][b][i-1][j-1][0][1]; 3. I-1 and I are not elected, the number of programmes is equal to the number of I-1 not selected and the first i-2 not selected, plus the first i-2 selected but the i-1 of the number of options not selected Dp[a][b][i][j][0][0] = dp[a][b][i-1][j] [0] [0] + dp[a][b][i-1][j][1][0] 4. I-1 selected, I was not elected, then the number of programmes is equal to the i-1 selected and the number of i-2 selected options plus the i-1 selected but i-2 the number of selected options and dp[a][b][i][ J][1][0] = dp[a][b][i-1][j][0][1] + dp[a][b][i-1][j][1][1] Last enumeration at the beginning of the end of 8 states, remove the opening end of the illegal state, the remaining accumulation can be

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
int const MAX = 1e3 + 5;
int const MOD = 1e9 + 7;
int dp[2][2][max][max][2][2];

int n;
    void up (int &x, int y) {x + = y;
if (x >= mod) x-= mod;
    } void Cal (int a, int b) {dp[a][b][2][a + b][a][b] = 1;
            for (int i = 3; I <=, i++) {for (int j = 0; J <=, J + +) {if (j)
                {Dp[a][b][i][j][0][1] = dp[a][b][i-1][j-1][0][0];
            DP[A][B][I][J][1][1] = dp[a][b][i-1][j-1][0][1];
            } up (Dp[a][b][i][j][0][0], dp[a][b][i-1][j][0][0]);
            Up (Dp[a][b][i][j][0][0], dp[a][b][i-1][j][1][0]);
            Up (Dp[a][b][i][j][1][0], dp[a][b][i-1][j][0][1]);
        Up (Dp[a][b][i][j][1][0], dp[a][b][i-1][j][1][1]);
    }}} int main () {int T;
    scanf ("%d", &t); for (int i = 0; i < 2; i + +) for (int j = 0; J < 2; j+ +) cal (I, J);
        while (T--) {int k, ans = 0;
        scanf ("%d%d", &n, &k);
                    for (int i = 0, i < 2; i++) for (int j = 0; J < 2; J + +) for (int l = 0; l < 2; l++) for (int z = 0; z < 2; z++) if (! ( (L = = 1 && i = = 1) | |
                            (z = = 1 && j = = 1)))
        Up (ans, dp[i][j][n][k][l][z]);
    printf ("%d\n", ans); }
}


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.