1. 程式人生 > >CF1091D New Year and the Permutation Concatenation

CF1091D New Year and the Permutation Concatenation

思路:

找規律。

實現:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 const ll MOD = 998244353;
 6 const int N = 1000000;
 7 
 8 ll a[N + 5], f[N + 5];
 9 
10 ll pow(ll x, ll n)
11 {
12     ll res = 1;
13     while (n)
14     {
15         if (n & 1) res = res * x % MOD;
16 x = x * x % MOD; 17 n >>= 1; 18 } 19 return res; 20 } 21 22 ll inv(ll x) 23 { 24 return pow(x, MOD - 2); 25 } 26 27 int main() 28 { 29 ll n; 30 f[0] = 1; 31 for (int i = 1; i <= N; i++) f[i] = f[i - 1] * (ll)i % MOD; 32 a[N] = inv(f[N]); 33 for
(int i = N - 1; i >= 0; i--) a[i] = a[i + 1] * (i + 1) % MOD; 34 while (cin >> n) 35 { 36 if (n <= 2) { cout << n << endl; continue; } 37 ll ans = (f[n] * n - f[n] * inv(2) * 3) % MOD; 38 for (int i = 3; i <= n - 1; i++) ans = (ans - f[n] * a[i] % MOD) % MOD;
39 cout << (ans + MOD) % MOD << endl; 40 } 41 return 0; 42 }