fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define fastt ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  7. #define sp " "
  8. #define pb push_back
  9. #define all(x) x.begin(),x.end()
  10. #define ff first
  11. #define ss second
  12. #define nl "\n"
  13.  
  14. void solve()
  15. {
  16. ll n, k, x;
  17. cin>>n>>k;
  18.  
  19. vector<ll>v(n);
  20. for(ll i=0; i<n; i++)
  21. {
  22. cin>>x;
  23. v[i] = x % k;
  24. }
  25.  
  26. map<ll,ll>freq;
  27. for(ll i=0; i<n; i++)
  28. {
  29. cin>>x;
  30. freq[x%k]++;
  31. }
  32.  
  33. /// x = 2 k = 5 then x+k=7 x - k = 3; x%k = 2, (x+k) %k = 2 [similar to x%k], (x-k)%k = 3[did abs thus val changes, similar to k - (x%k)]
  34. /// So need to Check 2 condition, i) is v[i]%k is present or ii) is k - (v[i]%k) is present
  35.  
  36. for(ll i=0; i<n; i++)
  37. {
  38. if(freq[v[i]]) freq[v[i]]--;
  39. else
  40. {
  41. x = k - v[i];
  42. if(freq[x]) freq[x]--;
  43. else
  44. {
  45. cout<<"NO"<<endl;
  46. return;
  47. }
  48. }
  49. }
  50.  
  51. for(auto &it : freq)
  52. {
  53. /// cout<<it.ff<<sp<<it.ss<<endl;
  54. if(it.ss != 0)
  55. {
  56. cout<<"NO"<<endl;
  57. return;
  58. }
  59. }
  60.  
  61. cout<<"YES"<<endl;
  62. }
  63.  
  64. int main()
  65. {
  66. ll t = 1;
  67. cin>>t;
  68. while(t--)
  69. {
  70. solve();
  71. }
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
YES