【枚举+推式子】牛客小白月赛 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;
}
相关推荐
mit6.8241 小时前
bfs|栈
算法
CoderYanger2 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz2 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
稚辉君.MCA_P8_Java3 小时前
DeepSeek 插入排序
linux·后端·算法·架构·排序算法
多多*3 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
.YM.Z4 小时前
【数据结构】:排序(一)
数据结构·算法·排序算法
Chat_zhanggong3454 小时前
K4A8G165WC-BITD产品推荐
人工智能·嵌入式硬件·算法
百***48074 小时前
【Golang】slice切片
开发语言·算法·golang
墨染点香4 小时前
LeetCode 刷题【172. 阶乘后的零】
算法·leetcode·职场和发展
做怪小疯子4 小时前
LeetCode 热题 100——链表——反转链表
算法·leetcode·链表