Count the number of n x n matrices A satisfying the following condition modulo m.
* Ai, j ∈ {0, 1, 2} for all 1 ≤ i, j ≤ n.
* Ai, j = Aj, i for all 1 ≤ i, j ≤ n.
* Ai, 1 + Ai, 2 + ... + Ai, n = 2 for all 1 ≤ i ≤ n.
* A1, 1 = A2, 2 = ... = An, n = 0.
输入描述:
The input consists of several test cases and is terminated by end-of-file. Each test case contains two integers n and m.
输出描述:
For each test case, print an integer which denotes the result.
备注 * 1 ≤ n ≤ 10 5 * 1 ≤ m ≤ 10 9 * The sum of n does not exceed 10 7 .
示例1:
输入
3 1000000000
100000 1000000000
输出
1
507109376
还是没有想的很清楚,明天再贴题解吧。先贴着代码
#include <map> #include <set> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define debug(a) cout << #a << " " << a << endl using namespace std; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; typedef long long ll; ll dp[maxn]; int main() { ll n, m; dp[2] = dp[3] = 1; while( cin >> n >> m ) { for( ll i = 4; i <= n; i ++ ) { dp[i] = ( ( (i-1) * (dp[i-1]+dp[i-2]) ) % m - ( (i-1)*(i-2)/2*dp[i-3]) % m + m ) % m; } cout << dp[n] << endl; } return 0; }