結果

問題 No.3397 Max Weighted Floor of Linear
コンテスト
ユーザー 👑 Mizar
提出日時 2026-02-16 12:11:20
言語 PyPy3
(7.3.17)
結果
AC  
実行時間 1,119 ms / 2,000 ms
コード長 2,702 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 291 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 88,040 KB
最終ジャッジ日時 2026-02-16 12:11:43
合計ジャッジ時間 21,216 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# -*- coding: utf-8 -*-

from __future__ import annotations
from dataclasses import dataclass

LL_MIN = -(1 << 63)


@dataclass(frozen=True)
class MwfData:
    """
    sum : total sum (for concatenation)
    best: maximum prefix value (or LL_MIN meaning "no valid prefix")
    dx  : length / shift amount (used to compute arg)
    arg : argmax (smallest arg if tie)
    """
    sum: int
    best: int = LL_MIN

    def __mul__(self, other: "MwfData") -> "MwfData":
        ssum = self.sum + other.sum
        cand_l = self.best
        cand_r = LL_MIN if other.best == LL_MIN else (self.sum + other.best)
        if cand_l > cand_r:
            return MwfData(ssum, cand_l)
        if cand_l < cand_r:
            return MwfData(ssum, cand_r)
        if cand_l == LL_MIN:
            return MwfData(ssum, LL_MIN)
        return MwfData(ssum, cand_l)

    def __pow__(self, k: int) -> "MwfData":
        # support: x ** k  (k >= 0)
        if not isinstance(k, int):
            return NotImplemented
        assert k >= 0
        if k == 0:
            return MwfData(0)
        ssum = self.sum * k
        if self.best == LL_MIN:
            return MwfData(ssum, LL_MIN)
        if self.sum > 0:
            sbest = (k - 1) * self.sum + self.best
            return MwfData(ssum, sbest)
        return MwfData(ssum, self.best)


def floor_prod(n: int, m: int, a: int, b: int, e, x, y):
    """
    Requires:
      - integers n,m,a,b with 0<=n, 0<m, 0<=a, 0<=b
      - x,y,e are elements of a monoid-like structure:
          * multiplication:  t1 * t2
          * power:           t ** k for k>=0
    """
    assert 0 <= n and 0 < m and 0 <= a and 0 <= b
    c = (a * n + b) // m
    pre = e
    suf = e
    while True:
        p, a = divmod(a, m)
        x *= (y ** p)
        q, b = divmod(b, m)
        pre *= (y ** q)
        c -= (p * n + q)
        if c == 0:
            return pre * (x ** n) * suf
        d = (m * c - b - 1) // a + 1
        suf = y * (x ** (n - d)) * suf
        n, m, a, b, c, x, y = c - 1, a, m, m - b - 1 + a, d, y, x


def max_weighted_floor_of_linear(n: int, m: int, a: int, b: int, c: int, d: int) -> tuple[int, int]:
    """
    argmwf(n,m,a,b,c,d) = (
      max_{0<=i<n} (a*i+b*floor((c*i+d)/m)),
      argmax (smallest i if tie)
    )
    """
    assert n > 0 and m > 0
    res: MwfData = floor_prod(
        n, m, c, d,
        MwfData(0),
        MwfData(a, 0),
        MwfData(b),
    )
    return res.best


if __name__ == '__main__':
    import sys
    T = int(sys.stdin.readline())
    for _ in range(T):
        N, M, A, B, C, D = map(int, sys.stdin.readline().split())
        max_val = max_weighted_floor_of_linear(N, M, A, B, C, D)
        print(max_val)
0