fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1042-C (Div. 3) (C++) Make it Equal
최초 업로드: 2025-08-10 17:14:05
최근 수정 시간: 2025-08-30 14:07:11
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 27
# C. Make it Equal [문제 링크](https://codeforces.com/contest/2131/problem/C) ## Problem Statement Given two [multisets](https://en.wikipedia.org/wiki/Multiset) $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 \le t \le 10^4$) — the number of test cases. The description of the test cases follows. The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the size of $S$ and the constant, respectively. The second line contains $n$ integers $S_1, S_2, \ldots, S_n$ ($0 \le S_i \le 10^9$) — the elements in $S$. The third line contains $n$ integers $T_1, T_2, \ldots, T_n$ ($0 \le T_i \le 10^9$) — the elements in $T$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. ## 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. ## 풀이 #### 주기 k인 수들끼리는 항상 변경할 수 있기 때문에 k로 나눈 나머지를 맵에 넣어 확인하였다. 비교에 절댓값이 붙어있기에 추가로 그것만 예외처리하면 된다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int n, k; cin >> n >> k; map<int, int> S; for(int i=0;i<n;i++) { int val; cin >> val; S[val%k]++; } bool chk=true; for(int i=0;i<n;i++) { int val; cin >> val; if(S[val%k]) { S[val%k]--; } else if(S[k-val%k]) { S[k-val%k]--; } else { chk=false; } } cout << (chk ? "YES\n" : "NO\n"); } } ```