【枚举+推式子】牛客小白月赛 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;
}
相关推荐
kupeThinkPoem1 小时前
哈希表有哪些算法?
数据结构·算法
小白程序员成长日记1 小时前
2025.11.16 力扣每日一题
算法
Kuo-Teng2 小时前
LeetCode 118: Pascal‘s Triangle
java·算法·leetcode·职场和发展·动态规划
Greedy Alg2 小时前
LeetCode 32. 最长有效括号(困难)
算法
ShineWinsu2 小时前
对于数据结构:链式二叉树的超详细保姆级解析—中
数据结构·c++·算法·面试·二叉树·校招·递归
野蛮人6号3 小时前
力扣热题100道之207课程表
算法·leetcode·职场和发展
这周也會开心3 小时前
Map的遍历方式
数据结构·算法
无敌最俊朗@3 小时前
C++ 值类别与对象模型面试题(12)
算法
代码不停4 小时前
Java模拟算法题目练习
java·开发语言·算法
前端小L4 小时前
图论专题(二):“关系”的焦点——一眼找出「星型图的中心节点」
数据结构·算法·深度优先·图论·宽度优先