题解 | 小红的双生排列

小红的双生排列

https://www.nowcoder.com/practice/592c127032164493b505d59540595e99

#include <iostream>
using namespace std;

long long m = 1e9 + 7;

// 递归计算阶乘并取模,取模is must, 否则容易计算出错为0
long long factorial(int n) {
    if ((n == 0) || (n == 1)) {
        return 1;
    }
    return n * factorial(n - 1) % m;
}

int main() {
    int n;
    while (cin >> n) {
        long long res;
        if ( n % 2 == 0) { // when n is even number
            res = factorial(n / 2) * factorial(n / 2) * 2 % m;
            cout << res ;
        } else if ( n % 2 == 1) { //when n is old number
            res = factorial( n / 2 + 1) * factorial(n / 2) % m;
            cout << res;
        }
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务