Atcoder ABC179

这期的题都可以用py写

C - A x B + C

因为N非常大,暴力是不可取的
遍历C,将N-C分解求每个数的因子个数

可以用欧拉筛的方法,我这里采用了积性函数的性质

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @file     : atcoder.py
# @software : PyCharm

import bisect
import copy
import sys
from itertools import permutations
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()
    fp = open("in.txt") if items[0] == "3.10.6" else sys.stdin
    n = int(fp.readline())
    f = [0] * (n + 1)
    f[1] = 1
    for i in range(2, n + 1):
        t = i
        for j in range(2, n):
            c = 0
            while t % j == 0:
                c += 1
                t //= j
            if c > 0:
                f[i] = f[t] * (c + 1)
                break
            if j * j > i:
                break
        if i == t:
            f[i] = 2
    ans = sum(f[1: n])
    print(ans)


if __name__ == "__main__":
    main()

D - Leaping Tak

f ( n ) = f ( n − l ) + f ( n − l + 1 ) + . . . . f ( n − r ) f(n)=f(n-l)+f(n -l+1)+....f(n-r) f(n)=f(nl)+f(nl+1)+....f(nr)
前缀数组加速

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @file     : atcoder.py
# @software : PyCharm

import bisect
import copy
import sys
from itertools import permutations
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()
    fp = open("in.txt") if items[0] == "3.10.6" else sys.stdin
    n, m = map(int, fp.readline().split())
    a = []
    for _ in range(m):
        a.append(list(map(int, fp.readline().split())))
    f = [0] * n
    s = [0] * (n + 1)
    f[0] = 1
    s[0] = 0
    s[1] = 1
    mod = 998244353
    for i in range(1, n):
        for l, r in a:
            sl, sr = i - r, i - l
            sl = max(0, sl)
            if sr >= 0:
                f[i] += s[sr + 1] - s[sl]
        f[i] %= mod
        s[i + 1] = (s[i] + f[i]) % mod
    print(f[n - 1])


if __name__ == "__main__":
    main()

E - Sequence Sum

找循环节啦。。四年级数奥题

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @file     : atcoder.py
# @software : PyCharm

import bisect
import copy
import sys
from itertools import permutations
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()
    fp = open("in.txt") if items[0] == "3.10.6" else sys.stdin
    n, x, m = map(int, fp.readline().split())
    h = {x: 0}
    seq = [x]
    rep = -1
    while True:
        x = x * x % m
        if x in h:
            rep = h[x]
            break
        h[x] = len(seq)
        seq.append(x)
    l = len(seq)
    if n <= l:
        print(sum(seq[:n]))
        return
    loop = l - rep
    ans = sum(seq[:rep])
    t, r = (n - rep) // loop, (n - rep) % loop
    ans += sum(seq[rep:]) * t + sum(seq[rep: rep + r])
    print(ans)


if __name__ == "__main__":
    main()

F - Simplified Reversi

动手画一画就知道,线段树的入门题
区间更新,单点查询最小值

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @file     : atcoder.py
# @software : PyCharm

import bisect
import copy
import sys
from itertools import permutations
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)

row, col = [], []
mark_r, mark_c = [], []


def push_down(op, idx):
    if op == 1:
        a, mark_a = row, mark_r
    else:
        a, mark_a = col, mark_c
    if mark_a[idx] == 1e9:
        return
    a[idx * 2] = min(a[idx * 2], mark_a[idx])
    a[idx * 2 + 1] = min(a[idx * 2 + 1], mark_a[idx])
    mark_a[idx * 2] = min(mark_a[idx * 2], mark_a[idx])
    mark_a[idx * 2 + 1] = min(mark_a[idx * 2 + 1], mark_a[idx])
    mark_a[idx] = 1e9


def update(op, L, R, l, r, idx, val):
    if op == 1:
        a, mark_a = row, mark_r
    else:
        a, mark_a = col, mark_c
    if L <= l and r <= R:
        a[idx] = min(a[idx], val)
        mark_a[idx] = min(a[idx], val)
        return
    push_down(op, idx)
    m = (l + r) >> 1
    if L <= m:
        update(op, L, R, l, m, idx * 2, val)
    if m < R:
        update(op, L, R, m + 1, r, idx * 2 + 1, val)


def query(op, l, r, x, idx):
    if op == 1:
        a, mark_a = row, mark_r
    else:
        a, mark_a = col, mark_c
    if l == r:
        return a[idx]
    push_down(op, idx)
    m = (l + r) >> 1
    if x <= m:
        return query(op, l, m, x, idx * 2)
    else:
        return query(op, m + 1, r, x, idx * 2 + 1)


def main():
    items = sys.version.split()
    fp = open("in.txt") if items[0] == "3.10.6" else sys.stdin
    n, q = map(int, fp.readline().split())
    global row, col, mark_r, mark_c
    row = [n] * (n << 2)
    col = [n] * (n << 2)
    mark_r = [1e9] * (n << 2)
    mark_c = [1e9] * (n << 2)
    tot_w = (n - 2) * (n - 2)
    for _ in range(q):
        op, b = map(int, fp.readline().split())
        op -= 1
        pos = query(1 - op, 1, n, b, 1)
        tot_w -= pos - 2
        update(op, 1, pos, 1, n, 1, b)
    print(tot_w)


if __name__ == "__main__":
    main()

你可能感兴趣的:(Atcoder,python)