AT_arc218_f.Buckets

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Problems F and F2 are the same problem with different constraints on MM. In Problem F, 1≤M≤41 \le M \le 4.

You are given a positive integer MM. Consider the following problem.

Bucket

You are given a positive integer NN and non-negative integer sequences A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N) and B=(B1,B2,…,BN)B=(B_1,B_2,\dots,B_N) of length NN. All elements of AA and BB are between 00 and MM, inclusive.

There are NN buckets numbered 11 to NN. Each bucket can hold up to MM liters of water. Initially, bucket ii contains AiA_i liters of water.

You may perform the following operation any number of times.

  • Choose two distinct buckets ii and jj. Continue pouring water from bucket ii into bucket jj as long as both of the following conditions are satisfied:
    • Bucket ii still has water remaining.
    • The amount of water in bucket jj is less than MM liters.

Your goal is to have exactly BiB_i liters of water in bucket ii for all ii. Determine whether the goal can be achieved.

You are given a non-negative integer matrix X=(Xi,j)(0≤i,j≤M)X=(X_{i,j})(0 \le i,j \le M) of (M+1)(M+1) rows and (M+1)(M+1) columns. Process the following queries QQ times.

  • You are given non-negative integers i,j,Yi,j,Y with 0≤i,j≤M0 \le i,j \le M. Change Xi,jX_{i,j} to YY. Then, obtain non-negative integer sequences AA and BB by the following procedure.

    • Initialize non-negative integer sequences AA and BB as empty sequences.

    • For i=0,1,…,Mi = 0,1,\dots,M in this order:

    • For j=0,1,…,Mj = 0,1,\dots,M in this order:

    • Do this Xi,jX_{i,j} times: append ii to the end of AA and jj to the end of BB.

  • Solve Bucket for the non-negative integer sequences AA and BB of length N=∑i=0M∑j=0MXi,jN = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j}.

题目 F 和 F2 是同一道题,但对 MM 的约束不同。在题目 F 中,1≤M≤41 \le M \le 4。

给定一个正整数 MM。考虑如下问题:

水桶问题(Bucket)

给定一个正整数 NN,以及两个长度为 NN 的非负整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N) 和 B=(B1,B2,…,BN)B=(B_1,B_2,\dots,B_N)。AA 和 BB 的所有元素均在 00 到 MM(含端点)之间。

有 NN 个编号为 11 至 NN 的水桶。每个水桶最多可容纳 MM 升水。初始时,第 ii 个水桶中含有 AiA_i 升水。

你可以执行以下操作任意多次:

  • 选择两个不同的水桶 ii 和 jj,并持续将水从水桶 ii 倒入水桶 jj,直到满足以下任一条件为止:
    • 水桶 ii 中已无剩余水量;
    • 水桶 jj 中的水量已达 MM 升。

你的目标是使得对每个 ii,第 ii 个水桶中恰好含有 BiB_i 升水。请判断该目标是否可达。

给定一个 (M+1)×(M+1)(M+1) \times (M+1) 的非负整数矩阵 X=(Xi,j)X = (X_{i,j})(其中 0≤i,j≤M0 \le i,j \le M)。你需要处理 QQ 个查询。

  • 每次查询给出三个非负整数 i,j,Yi, j, Y,满足 0≤i,j≤M0 \le i,j \le M。将 Xi,jX_{i,j} 修改为 YY;然后按如下步骤生成非负整数序列 AA 和 BB:
*   初始化两个空的非负整数序列 $A$ 和 $B$;
*   按 $i = 0,1,\dots,M$ 的顺序遍历;
*   对每个 $i$,再按 $j = 0,1,\dots,M$ 的顺序遍历;
*   对每一对 $(i,j)$,重复 $X_{i,j}$ 次:将 $i$ 追加到序列 $A$ 末尾,同时将 $j$ 追加到序列 $B$ 末尾。
  • 针对由此生成的、长度为 N=∑i=0M∑j=0MXi,jN = \sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} 的非负整数序列 AA 和 BB,求解上述 水桶问题(Bucket)。

输入格式

The input is given from Standard Input in the following format:

MM QQ
X0,0 X0,1 … X0,MX_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X1,0 X1,1 … X1,MX_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
⋮\vdots
XM,0 XM,1 … XM,MX_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
query1\mathrm{query}_1
query2\mathrm{query}_2
⋮\vdots
queryQ\mathrm{query}_Q

Each query is given in the following format:

i j Yi\ j\ Y

输入从标准输入中按以下格式给出:

MM QQ
X0,0 X0,1 … X0,MX_{0,0}\ X_{0,1}\ \dots\ X_{0,M}
X1,0 X1,1 … X1,MX_{1,0}\ X_{1,1}\ \dots\ X_{1,M}
⋮\vdots
XM,0 XM,1 … XM,MX_{M,0}\ X_{M,1}\ \dots\ X_{M,M}
query1\mathrm{query}_1
query2\mathrm{query}_2
⋮\vdots
queryQ\mathrm{query}_Q

每个查询按以下格式给出:

i j Yi\ j\ Y

输出格式

Output QQ lines. The ii-th line should contain Yes if the goal can be achieved in queryi\mathrm{query}_i, and No otherwise.

输出 QQ 行。第 ii 行应包含 Yes(如果在 queryi\mathrm{query}_i 中可以达成目标),否则包含 No。

输入输出样例

  • 输入#1

    3 3
    0 0 0 0
    0 0 2 0
    1 0 0 0
    0 0 0 0
    0 0 0
    2 3 1
    2 1 1

    输出#1

    Yes
    No
    Yes
  • 输入#2

    4 10
    45636788580181785 16131322312654301 43477244591521823 6505049084010674 86530627327096446
    95921187347997793 55491565467039163 87684565747362311 80318628430974482 12308092878301956
    75570615154690027 96403707363045776 14150012766408204 6612197700307407 64417022692908525
    5530468643826479 41731276604630756 15675296751519388 59461896803210859 66666666666666666
    72767956047192820 18258893791516726 58852629621892634 33333333333333333 29923985408775019
    2 1 26541245644686826
    2 4 29485791833729050
    4 1 21832826336874318
    3 2 4953499115446612
    2 0 69217973349997921
    2 4 23133150029036944
    3 4 55834224798559242
    1 2 98517007615469735
    2 3 3768060024403703
    0 4 87241661746072372

    输出#2

    No
    Yes
    No
    Yes
    No
    Yes
    No
    No
    No
    No

说明/提示

Sample 1 Explanation:
The first query makes X=(0000002010000000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix}, and we obtain A=(1,1,2),B=(2,2,0)A=(1,1,2),B=(2,2,0).

In this case, the goal can be achieved by the following sequence of operations, for example.

  • Set i=1,j=2i = 1,j = 2. The amount of water in each bucket changes from (1,1,2)(1,1,2) to (0,2,2)(0,2,2).
  • Set i=3,j=1i = 3,j = 1. The amount of water in each bucket changes from (0,2,2)(0,2,2) to (2,2,0)(2,2,0).

The second query makes X=(0000002010010000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix}, and we obtain A=(1,1,2,2),B=(2,2,0,3)A=(1,1,2,2),B=(2,2,0,3).

In this case, the goal cannot be achieved no matter how operations are performed.

The third query makes X=(0000002011010000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix}, and we obtain A=(1,1,2,2,2),B=(2,2,0,1,3)A=(1,1,2,2,2),B=(2,2,0,1,3).

In this case, the goal can be achieved by the following sequence of operations, for example.

  • Set i=1,j=2i = 1,j = 2. The amount of water in each bucket changes from (1,1,2,2,2)(1,1,2,2,2) to (0,2,2,2,2)(0,2,2,2,2).
  • Set i=3,j=1i = 3,j = 1. The amount of water in each bucket changes from (0,2,2,2,2)(0,2,2,2,2) to (2,2,0,2,2)(2,2,0,2,2).
  • Set i=4,j=5i = 4,j = 5. The amount of water in each bucket changes from (2,2,0,2,2)(2,2,0,2,2) to (2,2,0,1,3)(2,2,0,1,3).

Constraints

  • 1≤M≤41 \le M \le 4
  • 1≤Q≤1061 \le Q \le 10^6
  • 0≤Xi,j,Y≤10170 \le X_{i,j},Y \le 10^{17}
  • 0≤i,j≤M0 \le i,j \le M
  • ∑i=0M∑j=0MXi,j≥1\sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1 at any time.
  • All input values are integers.

样例 1 解释:
第一次查询使得 X=(0000002010000000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix},从而得到 A=(1,1,2),B=(2,2,0)A=(1,1,2),B=(2,2,0)。

此时,目标可通过如下操作序列(例如)实现:

  • 设 i=1,j=2i = 1,j = 2。各桶中水量由 (1,1,2)(1,1,2) 变为 (0,2,2)(0,2,2)。
  • 设 i=3,j=1i = 3,j = 1。各桶中水量由 (0,2,2)(0,2,2) 变为 (2,2,0)(2,2,0)。

第二次查询使得 X=(0000002010010000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix},从而得到 A=(1,1,2,2),B=(2,2,0,3)A=(1,1,2,2),B=(2,2,0,3)。

此时,无论执行何种操作,目标均无法实现。

第三次查询使得 X=(0000002011010000)X=\begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & 0 \\ 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ \end{pmatrix},从而得到 A=(1,1,2,2,2),B=(2,2,0,1,3)A=(1,1,2,2,2),B=(2,2,0,1,3)。

此时,目标可通过如下操作序列(例如)实现:

  • 设 i=1,j=2i = 1,j = 2。各桶中水量由 (1,1,2,2,2)(1,1,2,2,2) 变为 (0,2,2,2,2)(0,2,2,2,2)。
  • 设 i=3,j=1i = 3,j = 1。各桶中水量由 (0,2,2,2,2)(0,2,2,2,2) 变为 (2,2,0,2,2)(2,2,0,2,2)。
  • 设 i=4,j=5i = 4,j = 5。各桶中水量由 (2,2,0,2,2)(2,2,0,2,2) 变为 (2,2,0,1,3)(2,2,0,1,3)。

约束条件

  • 1≤M≤41 \le M \le 4
  • 1≤Q≤1061 \le Q \le 10^6
  • 0≤Xi,j,Y≤10170 \le X_{i,j},Y \le 10^{17}
  • 0≤i,j≤M0 \le i,j \le M
  • 在任意时刻均有 ∑i=0M∑j=0MXi,j≥1\sum_{i=0}^{M} \sum_{j=0}^{M} X_{i,j} \ge 1。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页