【題解】Codeforces 1391C. Cyclic Permutations

【題目敘述】http://codeforces.com/contest/1391/problem/C

#include <iostream>
using namespace std;
 
long long n, ans, mod = 1e9+7;
 
int main() {
    cin >> n;
    ans = 1;
    for (long long i = 1; i <= n; i++){
        ans *= i;
        ans %= mod;
    }
    long long mul = 2, tmp = 1;
    for (int i = 0; i <= 20; i++){
        if (((n-1)>>i) & 1){
            tmp *= mul;
            tmp %= mod;
        }
        mul *= mul;
        mul %= mod;
    }
    ans += mod;
    ans -= tmp;
    ans %= mod;
    cout << ans << "\n";
}
分享本文 Share with friends