错位排列 \(D_n=(n-1)*(D_{n-1}+D_{n-2})\) \(D_n=n*D_{n-1}+{(-1)}^n\) 牛客54484B(\(n\leq10^9\)) B-写信_华中农业大学2023年十二届程序设计竞赛(同步赛) 打表代码代码 C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15const long long mod = 1000000007; long long ans = 0; int block = 1000000; for (int i = 0 ; i <= k ; i++) { ans = ans * i % mod; if (i & 1) { ans--; } else { ans++; } ans = (ans + mod) % mod; if (i % block == 0) { cout << ans << ", "; } } C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25#include <bits/stdc++.h> using namespace std; // 2023 OneWan const int block = 1000000; const long long mod = 1000000007; const long long res[] = {打表出来的}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k; cin >> k; long long ans = res[k / block]; for (int i = k / block + 1 ; i <= k ; i++) { ans = ans * i % mod; if (i & 1) { ans--; } else { ans++; } ans = (ans + mod) % mod; } cout << ans; return 0; }