【枚举+推式子】牛客小白月赛 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;
}
相关推荐
保持低旋律节奏25 分钟前
算法——冗余!哈希表、vector、string适配器的混合使用
数据结构·算法·散列表
weixin_457760001 小时前
OpenCV 图像处理基础算法详解(一)
图像处理·opencv·算法
做怪小疯子1 小时前
LeetCode 热题 100——链表——相交链表
算法·leetcode·链表
立志成为大牛的小牛3 小时前
数据结构——五十一、散列表的基本概念(王道408)
开发语言·数据结构·学习·程序人生·算法·散列表
Coovally AI模型快速验证3 小时前
去噪扩散模型,根本不去噪?何恺明新论文回归「去噪」本质
人工智能·深度学习·算法·机器学习·计算机视觉·数据挖掘·回归
歌_顿3 小时前
attention、transform、bert 复习总结 1
人工智能·算法
MicroTech20254 小时前
MLGO微算法科技时空卷积与双重注意机制驱动的脑信号多任务分类算法
科技·算法·分类
txp玩Linux4 小时前
rk3568上解析webrtc音频降噪算法处理流程
算法·音视频·webrtc
立志成为大牛的小牛4 小时前
数据结构——五十二、散列函数的构造(王道408)
数据结构·笔记·程序人生·考研·算法
希望有朝一日能如愿以偿5 小时前
力扣每日一题:可被三整除的最大和
数据结构·算法·leetcode