2024.7.13刷题记录-牛客小白月赛98(未完)

目录

一、A-骰子魔术_牛客小白月赛98 (nowcoder.com)

1.模拟

2.简洁写法

二、B-最少剩几个?_牛客小白月赛98 (nowcoder.com)

贪心-数学

三、C-两个函数_牛客小白月赛98 (nowcoder.com)

1.模拟

2.逆元


一、A-骰子魔术_牛客小白月赛98 (nowcoder.com)

1.模拟

python 复制代码
R = lambda: map(int, input().split())
n, tar = R()
nums = list(R())
flag = 0
for x in nums:
    if x == tar:
        flag = 1
print('YES' if flag else 'NO')

2.简洁写法

参考他人代码(代码查看 (nowcoder.com)

python 复制代码
print(input().split()[1] in input().split() and 'YES' or 'NO')

二、B-最少剩几个?_牛客小白月赛98 (nowcoder.com)

贪心-数学

偶数加奇数为奇数,奇数乘奇数为奇数,优先选择加法,取决于奇数的个数。分为奇数多和偶数多两种情况。奇数多,则选择完加法后剩余 2 * cnt - n 个奇数,两两配对(乘法)则取余;当偶数多时,使用加法消耗完所有奇数偶数对即可,剩余 n - 2 * cnt。而两者一正一负(或0),取最大值即可。

python 复制代码
n = int(input())
nums = list(map(int, input().split()))
odd_cnt = 0
for x in nums:
    if x & 1: odd_cnt += 1
print(max(n - 2 * odd_cnt, (2 * odd_cnt - n) % 2))

三、C-两个函数_牛客小白月赛98 (nowcoder.com)

1.模拟

python无精度影响

python 复制代码
mod = 998244353
def f(a, x):
    if x == 1:
        return a * x % mod
    else:
        return a * a * x * (x - 1) // 2 % mod
t = int(input())
for _ in range(t):
    a, x = map(int, input().split())
    print(f(a, x))

2.逆元

python 复制代码
mod = 998244353
def f(a, x):
    if x == 1:
        return a % mod * x % mod
    else:
        return a % mod * a % mod * x % mod * (x - 1) % mod * qmi(2, mod - 2) % mod
    
def qmi(n, k):
    ans = 1
    while k > 0:
        if k & 1:
            ans = ans * n % mod
        n = n * n % mod
        k >>= 1
    return ans

t = int(input())
for _ in range(t):
    a, x = map(int, input().split())
    print(f(a, x))

(未完待续)

相关推荐
TF男孩11 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在16 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
NAGNIP18 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
站大爷IP18 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
美团技术团队19 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
c8i1 天前
python中类的基本结构、特殊属性于MRO理解
python
茉莉玫瑰花茶1 天前
算法 --- 字符串
算法