逆序对
题解:递推,dp[ n ] = n * dp[ n - 1 ] + (n - 1)! * (n - 1) * n / 2。
①注意炸 int, 我全开的 long long !!!!! ②1e6的阶乘取模,可以递推算,以为要用lucas什么之类(数学不会),最后才写,被自己愚蠢到了。
#include<bits/stdc++.h> #define ll long long #define P pair<int,int> #define pb push_back #define lson root << 1 #define INF (int)2e9 + 7 #define maxn (int)1e6 + 7 #define rson root << 1 | 1 #define LINF (unsigned long long int)1e18 #define mem(arry, in) memset(arry, in, sizeof(arry)) using namespace std; ll T, n, mod; ll dp[maxn], co[maxn]; void Inite() { mod = 1000000007; co[1] = 1; for(ll i = 2; i < (ll)maxn; i++){ co[i] = (co[i - 1] * i) % mod; } dp[2] = 1; for(ll i = 3; i < (ll)maxn; i++){ ll tp = i * (i - 1) / 2 % mod; dp[i] = (i * dp[i - 1] % mod + co[i - 1] * tp % mod) % mod; } } int main() { Inite(); cin >> T; while(T--){ cin >> n; cout << dp[n] << endl; } return 0; }