Given two multisets S and T of size n and a pos...
Created on: August 10, 2025
Answered using GPT-5 Thinking by Chat01
Created on: August 10, 2025
Answered using GPT-5 Thinking by Chat01
Given two multisets S
and T
of size n
and a positive integer k
, you may perform the following operations any number (including zero) of times on S
:
Select an element x
in S
, and remove one occurrence of x
in S
. Then, either insert x+k
into S
, or insert |x−k|
into S
.
Determine if it is possible to make S
equal to T
. Two multisets S
and T
are equal if every element appears the same number of times in S
and T
.
Input
Each test contains multiple test cases. The first line contains an integer t
(1≤t≤104
) — the number of test cases. The description of the test cases follows.
The first line contains two integers n
and k
(1≤n≤2⋅105
, 1≤k≤109
) — the size of S
and the constant, respectively.
The second line contains n
integers S1,S2,…,Sn
(0≤Si≤109
) — the elements in S
.
The third line contains n
integers T1,T2,…,Tn
(0≤Ti≤109
) — the elements in T
.
It is guaranteed that the sum of n
over all test cases does not exceed 2⋅105
.
Output
For each test case, output "YES" if it is possible to make S
equal to T
, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Example
InputCopy
5
1 3
1
2
1 8
4
12
3 5
6 2 9
8 4 11
2 7
2 8
2 9
3 2
0 1 0
1 0 1
OutputCopy
YES
YES
YES
NO
NO
Note
In the first test case, we can remove one occurrence of 1
from S
and insert |1−k|=|1−3|=2
into S
, making S
equal to T
.
In the second test case, we can remove one occurrence of 4
from S
and insert 4+k=4+8=12
into S
, making S
equal to T
.
In the last test case, we can show that it is impossible to make S
equal to T
.
code in java
I think it will have a easy solution
You're right — there’s a clean invariant here.
Observation: each move keeps an element’s residue modulo k
, except it can flip r
to k−r
(because |x−k|
turns residue r
into k−r
).
So each number can only move within its pair of residues {r, k−r}
. Special residues 0
(and k/2
when k
is even) are fixed to themselves.
Therefore, S
can become T
iff for every residue-pair {r, k−r}
the total count of elements of S
in that pair equals the total count in T
(and for 0
and possibly k/2
the counts match exactly).
Here’s an O(n) Java solution using that idea:
javaimport java.io.*; import java.util.*; public class Main { // Fast input static class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1 << 16]; private int ptr = 0, len = 0; FastScanner(InputStream is) { in = is; } private int read() throws IOException { if (ptr >= len) { len = in.read(buffer); ptr = 0; if (len <= 0) return -1; } return buffer[ptr++]; } long nextLong() throws IOException { int c; do { c = read(); } while (c <= ' '); // skip spaces long sgn = 1; if (c == '-') { sgn = -1; c = read(); } long val = 0; while (c > ' ') { val = val * 10 + (c - '0'); c = read(); } return val * sgn; } int nextInt() throws IOException { return (int) nextLong(); } } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(System.in); StringBuilder out = new StringBuilder(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); long k = fs.nextLong(); // k >= 1 HashMap<Long, Integer> freqS = new HashMap<>(n * 2); HashMap<Long, Integer> freqT = new HashMap<>(n * 2); for (int i = 0; i < n; i++) { long v = fs.nextLong(); long r = v % k; // 0 <= r < k freqS.merge(r, 1, Integer::sum); } for (int i = 0; i < n; i++) { long v = fs.nextLong(); long r = v % k; freqT.merge(r, 1, Integer::sum); } // Collect unique residue-pair "bases" (min(r, k - r)) HashSet<Long> bases = new HashSet<>(); for (Long r : freqS.keySet()) { long other = (k - r) % k; // for r=0 -> 0 long base = Math.min(r, other); bases.add(base); } for (Long r : freqT.keySet()) { long other = (k - r) % k; long base = Math.min(r, other); bases.add(base); } boolean ok = true; for (Long base : bases) { long r1 = base; long r2 = (k - base) % k; // partner residue (could equal r1) int cs = freqS.getOrDefault(r1, 0) + (r1 == r2 ? 0 : freqS.getOrDefault(r2, 0)); int ct = freqT.getOrDefault(r1, 0) + (r1 == r2 ? 0 : freqT.getOrDefault(r2, 0)); if (cs != ct) { ok = false; break; } } out.append(ok ? "YES\n" : "NO\n"); } System.out.print(out.toString()); } }
Why this works:
{r, k−r}
by subtracting/adding k
and doing the < k
flip via |x−k|
.r=0
or r=k/2
you can’t even flip), so matching total counts per pair is both necessary and sufficient.