【枚举+推式子】牛客小白月赛 63 E

登录---专业IT笔试面试备考平台_牛客网

题意:

思路:

首先是个计数问题,考虑组合数学

组合数学就是在考虑枚举所有包含1和n的区间

这个典中典就是枚举1和n的位置然后算贡献

双指针超时,考虑推式子:

Code:

cpp 复制代码
#include <bits/stdc++.h>

#define int long long

using i64 = long long;

using namespace std;

constexpr int N = 1e6 + 10;
constexpr int M = 1e6 + 10;
constexpr int mod = 998244353;

int Fac[N];

int qpow(int a, int b) {
    int res = 1ll;
    while(b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
void solve() {
    int n;
    cin >> n;

    if (n == 1) {
        cout << 1 << "\n";
        return;
    }
    
    Fac[0] = 1;
    for (int i = 1; i <= n; i ++) Fac[i] =  (Fac[i - 1] * i) % mod;
    int ans = 0;
    int inv2 = qpow(2, mod - 2);
    for (int i = 2; i <= n; i ++) {
        ans += (i * (i - 1) % mod * inv2 % mod) % mod * (n - i + 1) % mod;
        ans %= mod;
    }
    cout << (ans * Fac[n - 2] % mod * 2 % mod) % mod << "\n";
}
signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t = 1;
    while(t --) {
        solve();
    }
    return 0;
}
相关推荐
Frostnova丶21 小时前
LeetCode 190.颠倒二进制位
java·算法·leetcode
骇城迷影1 天前
代码随想录:链表篇
数据结构·算法·链表
专注前端30年1 天前
智能物流路径规划系统:核心算法实战详解
算法
json{shen:"jing"}1 天前
字符串中的第一个唯一字符
算法·leetcode·职场和发展
追随者永远是胜利者1 天前
(LeetCode-Hot100)15. 三数之和
java·算法·leetcode·职场和发展·go
BlockWay1 天前
西甲赛程搬进平台:WEEX以竞猜开启区域合作落地
大数据·人工智能·算法·安全
im_AMBER1 天前
Leetcode 121 翻转二叉树 | 二叉树中的最大路径和
数据结构·学习·算法·leetcode
mit6.8241 天前
二分+贪心
算法
programhelp_1 天前
特斯拉 MLE 超详细面经 + 避坑
数据结构·人工智能·算法·面试·职场和发展
越甲八千1 天前
深入了解迭代器erase()之后的失效逻辑
算法