fork download
  1. #include <bits/stdc++.h> // NeOWami
  2. using namespace std;
  3.  
  4. #define ft first
  5. #define sc second
  6. #define int long long
  7. const int N = 3e6 + 5;
  8. const int K = 1e4 + 5;
  9. int n, k;
  10. int cnt[N];
  11.  
  12. int par[N], use[N];
  13. int find(int u) {
  14. if (u == par[u]) return u;
  15. return par[u] = find(par[u]);
  16. }
  17. void joint(int u, int v) {
  18. u = find(u), v = find(v);
  19. if (u == v) return;
  20. par[u] = v;
  21. }
  22. signed main() {
  23. cin.tie(NULL)->sync_with_stdio(false);
  24. if(ifstream("EMPTY.inp")) {
  25. freopen("EMPTY.inp", "r", stdin);
  26. freopen("EMPTY.out", "w", stdout);
  27. }
  28. cin >> n >> k;
  29. int sum = 0;
  30. for (int i = 1; i <= k; i++) {
  31. long long x, y, a, b; cin >> x >> y >> a >> b;
  32. for (int j = 1; j <= y; j++) {
  33. int pos = (a * j + b) % n;
  34. cnt[pos] += x;
  35. sum += x;
  36. }
  37. }
  38. if (sum > n) exit(2);
  39. for (int i = 0; i <= n; i++) par[i] = i;
  40. for (int i = 0; i < n; i++) {
  41. for (int j = 0; j < cnt[i]; j++) {
  42. int pos = find(i);
  43. use[pos] = 1;
  44. int nxt = find((pos + 1) % n);
  45. joint(pos, nxt);
  46. }
  47. }
  48. for (int i = 0; i < n; i++) if (!use[i]) return cout << i, 0;
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5604KB
stdin
Standard input is empty
stdout
Standard output is empty