fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. using namespace std;
  5. const int N = 1e3 + 9;
  6. ll mod = 1e9L + 9, BASE = 4e6 + 1, mod2 = 2000000011ll, powr[N], pow2[N];
  7.  
  8. pair<ll, ll> hash_it(vector<ll> &v) {
  9. ll c1 = 0, c2 = 0;
  10. for (int i = 0; i < v.size(); i++) {
  11. c1 = (c1 * BASE + v[i]) % mod;
  12. c2 = (c2 * BASE + v[i]) % mod2;
  13. }
  14. return {c1, c2};
  15. }
  16.  
  17. void test() {
  18. int n;
  19. scanf("%d", &n);
  20. ll a[4][n], sum = 0;
  21. for (int i = 0; i < 4; i++) for (int j = 0; j < n; j++) scanf("%lld", &a[i][j]), sum += a[i][j];
  22.  
  23. if (sum % n != 0) {
  24. printf("No\n");
  25. return;
  26. }
  27. sum /= n;
  28.  
  29. set<pair<ll, ll>> s;
  30. //save rotations of first 2 arrays
  31. for (int st = 0; st < n; st++) {//start idx of second array
  32. vector<ll> v(n);
  33. for (int count = 0; count < n; count++) v[count] = a[0][count] + a[1][(count + st) % n];
  34. s.insert(hash_it(v));
  35. }
  36.  
  37. //search for matching rotation using the last 2 arrays
  38. bool flag = false;
  39. for (int st = 0; st < n; st++) {//start idx of second array
  40. if (flag)break;//if found answer break
  41. vector<ll> v(n);
  42. for (int count = 0; count < n; count++) v[count] = sum - a[2][count] - a[3][(count + st) % n];
  43.  
  44. auto c = hash_it(v);
  45. ll c1 = c.first, c2 = c.second;
  46.  
  47. for (int i = 0; i <= n; i++) {
  48. //search for the current rotation
  49. c1 = (((c1 - v[i] * powr[n - 1]) % mod) + mod) % mod;
  50. c2 = (((c2 - v[i] * pow2[n - 1]) % mod2) + mod2) % mod2;
  51. c1 = (c1 * BASE + v[i]) % mod;
  52. c2 = (c2 * BASE + v[i]) % mod2;
  53. if (s.count({c1, c2}))flag = true;
  54. }
  55. }
  56. if (flag)printf("Yes\n");
  57. else printf("No\n");
  58. }
  59.  
  60. int main() {
  61. // freopen("input.in", "r", stdin);
  62. int t;
  63. scanf("%d", &t);
  64. powr[0] = pow2[0] = 1;
  65. for (int i = 1; i < N; i++) {
  66. powr[i] = (powr[i - 1] * BASE) % mod;
  67. pow2[i] = (pow2[i - 1] * BASE) % mod2;
  68. }
  69.  
  70. for (int cas = 1; cas <= t; cas++) {
  71. printf("Case %d: ", cas);
  72. test();
  73. }
  74. }
  75.  
Runtime error #stdin #stdout 0s 4264KB
stdin
Standard input is empty
stdout
Standard output is empty