AtCoder ABC132

陷入了"会做,但不完全会做"的状况

C

水题,排序找中间数两边的差值

D

组合数学

求把n个相同的球分到m个相同的盒子,1每个盒子至少一个球2每个盒子球不限的组合数

空挡插隔板法,高中数学

python 复制代码
# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @author   : yhdu@tongwoo.cn
# @desc     :
# @file     : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)

mod = 10 ** 9 + 7


def pw(a, x):
    if x == 0:
        return 1
    temp = pw(a, x >> 1)
    if x & 1:
        return temp * temp * a % mod
    else:
        return temp * temp % mod


def inv(x):
    return pw(x, mod - 2)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    n, k = map(int, fp.readline().split())
    R, B = n - k, k

    def cmb(x, y):
        if x < y:
            return 0
        return fac[x] * inv(fac[y]) * inv(fac[x - y]) % mod

    fac = [1] * 5005
    for i in range(2, 5005):
        fac[i] = fac[i - 1] * i % mod

    for i in range(1, B + 1):
        ans_r = cmb(R + 1, i)
        ans = cmb(B - 1, i - 1) * ans_r % mod
        print(ans)


if __name__ == "__main__":
    main()

E

一开始用dfs去求(3步能访问到的点对),明显TLE

分层图求最短路

原图分为3层,每个点拆成 V 0 , V 1 , V 2 V_0,V_1,V_2 V0,V1,V2,

假如 ( u , v ) (u,v) (u,v)间有一条有向边

把边拆成 ( u 0 , v 1 ) , ( u 1 , v 2 ) , ( u 2 , v 3 ) (u_0,v_1),(u_1,v_2),(u_2,v_3) (u0,v1),(u1,v2),(u2,v3)

走三的倍数步能回到0层,这样只要能走到 T 0 T_0 T0点就有解

python 复制代码
# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @author   : yhdu@tongwoo.cn
# @desc     :
# @file     : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    n, m = map(int, fp.readline().split())
    g = [[] for _ in range(n * 3)]
    for i in range(m):
        u, v = map(int, fp.readline().split())
        u, v = u - 1, v - 1
        u0, u1, u2 = u, u + n, u + 2 * n
        v0, v1, v2 = v, v + n, v + 2 * n
        g[u0].append(v1)
        g[u1].append(v2)
        g[u2].append(v0)
    S, T = map(int, fp.readline().split())
    S, T = S - 1, T - 1

    vis = [-1] * (3 * n)
    qu = deque()
    qu.append(S)
    vis[S] = 0
    while qu:
        cur = qu.popleft()
        if cur == T:
            break
        for v in g[cur]:
            if vis[v] == -1:
                vis[v] = vis[cur] + 1
                qu.append(v)
    if vis[T] == -1:
        print(-1)
    else:
        print(vis[T] // 3)


if __name__ == "__main__":
    main()

F

分块

重要的是想清楚每一块代表的数字

相关推荐
顾林海11 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱13 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽18 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码18 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱1 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵1 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio1 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663671 天前
使用 Python 从零创建 Word 文档
python
Csvn2 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽2 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate