PTA reciprocity (recursive)

7-1 Reciprocity (10 points)

Jige is still that Jige, the Jige who is called "Chijige". Whenever the holiday comes, Jiji, who has many girlfriends, can always receive various gifts from girlfriends all over the country. Receiving a gift is certainly worthy of joy, but returning a gift is indeed a hassle! No matter how troublesome it is, I'm always embarrassed to accept the gift and not return it. That's not the style of Chi Chi brother. Now, Jiji, who is both face-loving and stingy, has come up with a wonderful way: he is ready to distribute the gifts from his girlfriends reasonably, and then send them back to different girlfriends, so that he doesn't have to spend money on gifts! Suppose that Jiji’s n girlfriends each give him a gift (each person gives him a gift), and now he needs to make reasonable arrangements, and then send each girlfriend a gift. The point is that the gift returned cannot be this girlfriend The gift that I gave him before, otherwise, Jiji will be a big deal... Now, Jiji wants to know how many kinds of gift-giving schemes that meet the conditions in total?

Input format:

The first line of the input data is a positive integer T, which means there are a total of T groups of test data (T <= 100); each group of data contains a positive integer n, which means that the number of girlfriends of Jiji is n (1 <= n <= 100 ).

Output format:

Please output the number of possible solutions, because the number of solutions may be relatively large, please output the result after taking the modulo 1000000007. (Hint: In the process of recursion, keep finding the remainder to prevent the data from overflowing due to too much data.) Each group of output occupies one line.

Input sample:

3
1
2
4

Sample output:

0
1
9

Problem solving

  1. Don't try to solve this question blindly. I was like this at the beginning. The result was too complicated and I couldn't write a general term. Later, I reanalyzed it and found it to be a recursive question.
  2. Let dp[n] represent the plan of n people
  3. As shown in the figure below, there are a total of n people. If the first position (A) is given to one of the (n-1) people except A, there can be (n-1) choices. Among the remaining n-1 people, there are n -2 people have repetitions. Now suppose that all n-1 people have repetitions, that is, if F = A, then there are dp[n-1] schemes for n-1 people. Obviously, if this is the case directly Calculating, we have less counted the number of times A is at position F, now put A directly at position F, leaving n-2 positions, all of which are repeated n-2, namely dp[n-2], in summary:
  4. dp[n] = (n-1) * (dp[n-1] + dp[n-2]); The result is just taking the remainder of p.
    Insert picture description here

Code

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
const int p = 1000000007;
const int maxn = 102;
ll dp[maxn];

int main() {
    
    
    int T, n;
    dp[1] = 0;
    dp[2] = 1;
    for (int i = 3; i < maxn; i++) {
    
    
        dp[i] = (i - 1LL) * (dp[i - 1] + dp[i - 2]) % p;
    }
    cin >> T;
    // for (int i = 1; i <= T; i++) {
    
    
    //     cout << i << " " << dp[i] << endl;
    // }
    while (T--) {
    
    
        cin >> n;
        cout << dp[n] << endl;
    }

    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45349225/article/details/109434418