We are given two arrays a and b of length n (1 <= n <= 2e5). For a real number d (not necessarily integer), define c_i = d * a_i + b_i. We want to maximize the number of i such that c_i = 0.
Goal: find maximum number of indices i for which we can satisfy d * a_i + b_i = 0, i.e., d = -b_i / a_i, provided a_i != 0. If a_i = 0, then c_i = b_i, which is zero if b_i = 0, regardless of d; otherwise, if b_i != 0, then c_i cannot be zero for any d because a_i=0 leads to constant b_i (non-zero). Actually if a_i = 0 and b_i = 0, then c_i = 0 always (for any d). So indices with a_i=0 and b_i=0 are always zero; they contribute to count irrespective of d. Those with a_i=0 and b_i != 0 can never be zero. For a_i != 0, we can choose d to satisfy c_i=0 exactly at d = -b_i / a_i.
Thus the problem reduces to: Among indices with a_i != 0, we need to pick d (a real number) that maximizes the count of indices i for which d equals -b_i / a_i, plus always count indices with a_i=0 and b_i=0. Also note: if we pick d arbitrarily, indices with a_i != 0 but not matching d will not be zero. However, we could also consider d being infinite? But d is real finite number; no infinities. So d must be a real number.
Thus maximum zero count = (number of indices with a_i = 0 and b_i = 0) + max frequency among the values -b_i / a_i for indices with a_i != 0. But careful: We must treat the case where a_i != 0, but we can have multiple fractions that reduce to the same rational number? Since a_i and b_i are integers, -b_i/a_i is a rational number. However, due to floating point representation, we need to handle with care. The fraction -b_i / a_i can be simplified to a canonical representation (reduced fraction) with sign and using pairs (numerator, denominator) after reducing by gcd. But also note that there may be zeros: if b_i = 0 and a_i != 0, then d = 0 works. So that's a valid fraction 0/anything? Actually -b_i/a_i = 0. So d=0 yields zero for all those indices with b_i=0 and a_i!=0. So they all have d=0, which is the same value.
Thus the maximum number of zeroes is: constant_always = count of i such that a_i == 0 and b_i == 0; plus the maximum frequency of a rational value among the indices with a_i != 0. But note: indices with a_i = 0 and b_i != 0 never contribute, so ignore.
Thus answer = constant_always + max_count where max_count is the maximum number of indices i (with a_i != 0) that share the same ratio -b_i / a_i (as rational number).
But is that always optimal? Yes, because we can choose d equal to that common rational, making all those indices zero.
Thus the problem reduces to counting frequencies of the rational numbers (or equivalently pairs of (numerator, denominator) after reduction) from each index with a_i != 0.
However, we must be careful about sign: For two indices, say a1=2, b1=4 gives d = -2; a2=-1, b2=2 gives d = -2? Actually -b2/a2 = -2/(-1)=2? Wait compute: For a2=-1, b2=2 => -2/(-1) = 2, which is 2, not -2. But if we simplify fraction: -b_i/a_i = fraction. We need a canonical representation.
Define value v_i = -b_i / a_i. But to avoid floating errors, we represent it as a reduced fraction: numerator = -b_i, denominator = a_i. But we can reduce by gcd of numerator and denominator, also ensure denominator positive (or treat sign consistently). Standard approach: For each i with a_i != 0, compute numerator = -b_i, denominator = a_i. Reduce by g = gcd(|numerator|, |denominator|). Then divide both by g. Then ensure denominator > 0; if denominator < 0, flip signs on both numerator and denominator. (If denominator == 0 cannot happen because a_i != 0). But careful: we want to treat zero as well: if numerator == 0, then we should represent as (0, 1) (or any denominator positive). Because -b_i/a_i = 0 when b_i=0. That's fine.
Thus we can count frequency of (num, den) pairs. The maximum frequency is the answer.
But there is one more nuance: Indices with a_i != 0 and b_i such that -b_i/a_i same? Yes.
Now, constant_always: indices with a_i=0 and b_i=0 always zero. Are there any other cases that can be zero for any d? If a_i = 0 and b_i = 0 always, yes. If a_i != 0, it's zero only for one specific d. So our counting is correct.
But wait: Could there be a scenario where we can set d to something that makes a zero from an index with a_i=0, b_i=0? That's always zero anyway, so it's counted always. But the maximum number of zeroes we can get might be larger if we choose d that matches some fraction and also includes those always-zero indices; always-zero indices are always zero, so they simply add to the count regardless. So indeed answer = always_zero_count + max_freq.
Edge Cases:
-
If all a_i = 0? Then for each i, if b_i == 0 -> always zero, else never zero. Then we can't choose d to affect anything. So answer = count of b_i == 0. That matches our formula: constant_always = count(a_i==0 && b_i==0) = that count; max_freq among a_i!=0 is 0 because no such indices. So answer = constant_always.
-
If there are indices with a_i != 0 but also some always-zero indices, we add them.
Complexities: n up to 200k, need O(n log) due to gcd. Use map with pair<int,int> key. But a_i and b_i up to 1e9, so numbers after reduction can be up to 1e9; gcd can be computed fast. However, using map of pairs might be okay.
But careful: The fraction representation may overflow when multiplying? Not needed. We just reduce using gcd of absolute values of numerator and denominator. But note numerator = -b_i; b_i can be -1e9, so -b_i can be up to 1e9. The gcd of two ints up to 2e9 fits within 64-bit? Actually int in C++ can hold up to ~2e9, but gcd of two numbers up to 2e9 is fine within 32-bit? However, -b_i might be -2e9? Actually b_i in range -1e9 to 1e9, so -b_i in range -1e9 to 1e9. a_i also in range -1e9 to 1e9. So absolute values up to 1e9, fits in 32-bit signed. Use long long to be safe? But 1e9 fits within int. However, product might not be needed. So we can use int.
But careful: numerator = -b_i; what about when b_i = -1e9? -b_i = 1e9, okay. When a_i = -1e9? denominator = -1e9, okay. gcd(1e9,1e9) = 1e9, which also fits. But after division, numbers become smaller.
So we can use pair of ints.
But there is another nuance: For a_i != 0, we can also have the case where b_i=0, then fraction is 0. But also, if a_i != 0 and b_i=0, that fraction is 0, but we also have indices with a_i != 0 and b_i = something else that reduces to 0? Only b_i=0 yields 0. So fine.
Now, what about the possibility that we might choose d such that it makes zero for an index with a_i=0 and b_i=0? Already always zero; but what about a_i=0 and b_i !=0? Can never be zero. So not.
Thus the answer seems straightforward.
But wait: In the fourth example:
3
1 2 -1
-6 -12 6
Let's compute:
i1: a=1,b=-6 -> d = -(-6)/1=6? Actually -b_i/a_i = -(-6)/1 = 6. i2: a=2,b=-12 -> -(-12)/2 = 12/2=6. i3: a=-1,b=6 -> -6/(-1) = 6. So all give d=6, so max_freq = 3. And constant_always = count of a_i=0 && b_i=0 = 0. So answer = 3. matches.
Third example: all a_i=0, b_i non-zero? Actually a = [0 0 0 0], b=[1 2 3 4]. Then constant_always = 0 (since no b_i zero). max_freq for a_i !=0 none => 0. Answer = 0.
Second example: n=3: a=[13,37,39], b=[1,2,3]. Compute fractions: i1: -1/13, i2: -2/37, i3: -3/39 = -1/13? -3/39 reduces to -1/13 (gcd=3). So i1 and i3 share same fraction. i2 different. constant_always=0. max_freq=2. Answer=2. matches.
First example: a=[1,2,3,4,5]; b=[2,4,7,11,3]; compute fractions:
i1: -2/1 = -2
i2: -4/2 = -2
i3: -7/3
i4: -11/4
i5: -3/5
So max_freq=2 (for -2). constant_always=0. Answer=2. matches.
Thus solution is correct.
But careful: There is a nuance: When a_i != 0, the fraction -b_i/a_i may be not reduced but we reduce to count equal fractions. However, we need to be cautious about representation of negative numbers: For example, a_i positive and b_i positive gives negative fraction. If we reduce, we should have a canonical representation: denominator > 0. So if denominator after gcd is negative, multiply both by -1. For zero numerator, we can represent as (0,1). That will unify (0, anything) to (0,1). So (0, -1) becomes (0,1). Good.
Implementation steps:
- Read n, arrays a and b.
- Initialize always_zero = 0.
- Use map<pair<int, int>, int> freq.
- For each i from 0 to n-1:
- if a[i] == 0:
if b[i] == 0:
always_zero += 1
- else:
// compute reduced fraction (num, den) = (-b_i, a_i)
// But careful: treat b_i as integer; we can compute num = -b_i, den = a_i.
// However, better: we want d = -b_i / a_i. So numerator = -b_i, denominator = a_i.
// Compute g = gcd(abs(num), abs(den))
// num /= g; den /= g;
// if den < 0: den = -den; num = -num; (to ensure denominator positive)
// If num == 0: set den = 1 (canonical zero)
// Increment freq[{num, den}]
- Find max_count = 0; iterate over freq map and take maximum value.
- Answer = always_zero + max_count.
- Print answer.
Now check: Is there any scenario where two different fractions yield the same d? No, rational equality exactly means reduced fractions equal if and only if the real numbers are equal. So counting by reduced fractions is correct.
Potential pitfalls: Overflow when computing gcd of numbers that might be up to 1e9? It's fine. But careful: numerator = -b_i could be -2^31? b_i up to 1e9, so -b_i min = -1e9, max = 1e9. But b_i can be -1e9, then -b_i = 1e9. So within 32-bit signed (max 2,147,483,647) it's fine.
But what about a_i could be -1e9? So denominator -1e9. After reduction, numbers become smaller.
One more nuance: gcd of (0, x) is |x|? Actually gcd(0, x) = |x|. But we treat numerator zero separately? Actually if numerator=0, then fraction=0. Then gcd(0, den) = abs(den). If we apply, num/g = 0, den/g = den/|den| = ±1. Then we ensure den positive: den = 1, num = 0. So that works as well. But careful: den could be negative; after dividing by g = abs(den), we get den = den/abs(den) = sign(den) which is either 1 or -1. Then after flipping to ensure denominator positive, we set num = -num? Actually we need to ensure den>0. If den negative, multiply both by -1: den becomes positive, num flips sign. If numerator is 0, flipping sign does nothing. So we get (0,1). So simpler: if num == 0, we can set pair = (0,1) directly.
Alternatively, compute g = gcd(abs(num), abs(den)); then num/=g; den/=g; if den<0: den=-den; num=-num. This works for zero as well. But be careful: In C++, gcd for ints, gcd(0, x) returns x? Actually std::gcd in C++17 returns gcd(0, x) = |x|. That's fine. Then num=0/g = 0; den = den/g = den/abs(den) = sign(den). If den negative, den becomes -1? Actually if den positive, den/g = den/den = 1? Wait g = abs(den) if num=0. If den positive, g = den, then den/g = 1. If den negative, g = -den? Actually abs(den) is positive. For den = -5, g = gcd(0,5)=5. Then den/g = -5/5 = -1. Then after if den<0: den = -den -> 1, num = -num -> 0. So final (0,1). So consistent.
Thus we can use that.
Complexity: O(n log M) due to map insertion (log n) and gcd O(log value). n=2e5, fine.
But we might be able to use unordered_map with custom hash for pair, but map is fine.
One more thought: There might be a scenario where we have many indices with a_i=0 and b_i=0 which we always count. But could we also count indices with a_i=0 and b_i=0 as part of the fraction? No because they don't constrain d, but they are always zero; so they simply add to total. So our sum is correct.
But wait: Could there be a scenario where we choose a d that yields zero for an index with a_i=0 and b_i != 0? No, because c_i = b_i constant !=0, so can't.
Thus solved.
Now double-check with some edge cases:
Case: n=1, a[0]=0, b[0]=0 => answer = 1.
Case: n=1, a[0]=0, b[0]=5 => answer = 0.
Case: n=1, a[0]=5, b[0]=0 => fraction=0, max_freq=1, always_zero=0 => answer=1.
Case: n=2, a=[1,2], b=[1,2] => fractions: -1/1=-1, -2/2=-1 -> both -1 => max_freq=2 => answer=2.
Case: n=2, a=[1,2], b=[-1,-2] => fractions: 1/1=1, 2/2=1 => max_freq=2 => answer=2.
Case: n=2, a=[-1,-2], b=[1,2] => fractions: -1/(-1)=1? Actually -b/a: -1/(-1)=1; -2/(-2)=1 => max_freq=2 => answer=2.
Case: n=2, a=[-1,2], b=[1,2] => fractions: -1/(-1)=1; -2/2=-1 => two distinct => max_freq=1 => answer=1. Could we get 2? Only if we choose d=1 yields zero for first but second: d2+2 = 12+2=4 not zero; d=-1 yields second zero but first: -1*(-1)+1=2 not zero. So 1 is max. So correct.
Now: Test with mixed zeros: n=3: a=[0,1,2], b=[0,1,2]? Actually b[0]=0? Let's set: a=[0,1,2], b=[0,-1,-2] maybe. For i=0: a=0,b=0 => always zero. i=1: a=1,b=-1 => fraction = 1 (since -(-1)/1=1). i=2: a=2,b=-2 => fraction = 1? -(-2)/2=2/2=1. So max_freq=2? Actually we have indices 1 and 2 share fraction=1, plus index0 always zero => total 3. Check: choose d=1 => c1=11+(-1)=0, c2=12+(-2)=0, c0=0*1+0=0. So answer=3. Our formula: always_zero=1, max_freq=2 => total=3. Good.
Case: a=[0,0,1], b=[0,5,0] => always_zero: first index a=0,b=0 -> always zero. Second index a=0,b=5 -> never zero. Third index a=1,b=0 -> fraction 0 -> freq[0]=1. So max_freq=1. total=2. Check: choose d=0 => c3=0*1+0=0; c1 always zero; c2=5; So 2 zeros. Could we get more? If we choose d something else, c3 becomes non-zero, so only index1 always zero (1). So max=2. So formula works.
Thus solution seems correct.
Now, we need to implement. Complexity O(n log n) due to map.
But wait, n up to 200k, map with pair<int,int> might be okay. However, we might use unordered_map with hash of pair to get average O(n). But that's optional.
We also must be careful with integer division when reducing? Use gcd from algorithm library.
Implementation details:
- Use long long for numerator and denominator? But int is enough. However, to avoid overflow in gcd arguments? gcd expects non-negative ints? In C++17, std::gcd works with integer types, and it takes absolute values? Actually std::gcd returns the greatest common divisor of |m| and |n|. It's defined for non-negative? Standard: For signed integers, it returns gcd of absolute values? I recall that std::gcd returns the greatest common divisor of the absolute values of m and n. So we can pass signed ints; it's fine. But we'll compute g = gcd(abs(num), abs(den)) to be explicit.
Potential issues: When denominator is -2^31? Actually a_i can be -1e9, far from -2^31. So safe.
Now produce final answer.
But there is a twist: Could there be a scenario where we can set d to something that makes zero for indices with a_i=0? Only if b_i=0. Already considered.
Thus answer.
But wait: The problem statement says "choose some real (i.e. not necessarily integer) number d". So d can be any real number, so fractions are allowed.
Thus done.
Now let's think about any tricky nuance: Suppose we have indices with a_i != 0 and we reduce fraction. However, the fraction -b_i / a_i might be equal for many indices but we may also consider the possibility of d being infinite? Not allowed.
Another nuance: Could we combine the always-zero indices with the d fraction? They are independent. So maximum total zeroes is always_zero + max_freq, because we can pick d that gives max_freq for non-zero a, and always-zero are automatically zero.
But is there any situation where picking d that yields a smaller count among a_i != 0 could give more total because of some indices with a_i=0, b_i=0? But those are always zero regardless of d, so they always contribute. So you always get them. So you would always pick the d that maximizes the non-zero a_i count. So indeed answer is sum.
Thus final.
Now, can there be any scenario where an index with a_i=0, b_i=0 might also be counted as part of the fraction? No, because we don't have a fraction for it; but that doesn't matter. So sum is fine.
Thus solution.
Now let's think about the time complexity: we need to compute gcd for each index, which is O(log min(|a|,|b|)). With n=200k, fine.
Now implement in code.
But need to consider that we must avoid division by zero when a_i=0. Already handled.
Now final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i=0; i<n; i++) cin >> a[i];
for (int i=0; i<n; i++) cin >> b[i];
int always = 0;
map<pair<int,int>, int> freq;
for (int i=0; i<n; i++) {
if (a[i] == 0) {
if (b[i] == 0) always++;
} else {
int num = -b[i];
int den = a[i];
int g = gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
den = -den;
num = -num;
}
// For zero, ensure den=1? Already handled via den>0; if num=0 then den becomes positive; but we might have den=1 after reduction? Actually if num=0, after reduction den could be 1 or something? With our reduction, if num=0, g=abs(den), then num=0, den = den/g = sign(den). Then if den<0, we flip to positive: den=1, num=0. So fine.
freq[{num, den}]++;
}
}
int maxf = 0;
for (auto &[_, cnt] : freq) {
maxf = max(maxf, cnt);
}
cout << always + maxf << "\n";
return 0;
}
Test on examples:
Example 1: n=5, a=[1,2,3,4,5], b=[2,4,7,11,3].
Indices:
i0: num=-2, den=1; g=gcd(2,1)=1; ( -2,1 )
i1: num=-4, den=2; g=gcd(4,2)=2 => (-2,1)
i2: num=-7, den=3 => (-7,3)
i3: num=-11, den=4 => (-11,4)
i4: num=-3, den=5 => (-3,5)
Freq: ( -2,1):2, others 1. maxf=2, always=0 => output2.
Example2: ok.
Example3: all a=0 => only always counting: check b values: all non-zero => always=0; freq empty => maxf=0 => output0.
Example4: all a!=0:
i0: num=6? b=-6 => -(-6)=6? Actually b=-6 => -b = 6, den=1 => (6,1) after reduction? Actually gcd(6,1)=1 -> (6,1) den positive.
i1: a=2,b=-12 => -b =12, den=2 -> gcd(12,2)=2 -> (6,1)
i2: a=-1,b=6 => -b = -6? Actually b=6 => -b = -6, den=-1 -> g=gcd(6,1)=1? Wait absolute: |num|=6,|den|=1? den = -1 => abs=1, g=1 -> num=-6, den=-1 -> den<0 => den=1, num=6 => (6,1). So all (6,1) freq=3, maxf=3, always=0 -> output3.
Works.
Now check a scenario: a_i negative and b_i zero: e.g., a=-5, b=0 => num=0, den=-5 => g=gcd(0,5)=5? Actually abs(num)=0, abs(den)=5, g=5; num=0/5=0, den=-5/5=-1 -> den<0 => den=1, num=0 => (0,1). So counts as zero fraction.
Now check a scenario: a=2,b=4 => num=-4, den=2 => g=2 => (-2,1). But note we might have a=-2,b=-4 => num=4, den=-2 -> g=2 => (2,-2)? Actually compute: num=4 (since -(-4)=4), den=-2 -> g=gcd(4,2)=2 -> num=2, den=-1 -> den<0 => den=1, num=-2 -> (-2,1). So consistent.
Thus our reduction with denominator positive ensures canonical representation.
But wait: There is also the possibility of representing fractions with denominator positive and numerator can be any integer including negative. That works.
Now, potential issue: large number of distinct fractions, map overhead okay.
Edge Cases with very large n, but okay.
Thus final answer.
But hold on: There is one nuance: d is a real number, not necessarily rational. But our fractions are rational. However, we can choose d equal to any real number; so if we have many indices with the same rational value, we can pick that rational as d. So that's fine. For indices that would require irrational d to be zero, they won't match rational ones. But all fractions from integer a,b are rational.
Thus done.
Now, maybe there is a scenario where we can choose d such that multiple indices with a_i = 0 and b_i = 0 are always zero, plus some indices with a_i != 0 can also be zero if we pick d accordingly. So indeed sum.
Now finalize.
But the problem might also be considered as solving for d such that many equations da_i + b_i = 0 hold. That is a system of linear equations. The maximum number of equations that can be satisfied simultaneously is the maximum number of i for which the pairs (a_i,b_i) are linearly dependent (i.e., a_id + b_i = 0). But it's essentially same as fraction.
Thus solution.
Now, we should think about whether there is any scenario where picking d from the fraction might also inadvertently cause a zero for an index with a_i=0? Not unless b_i=0.
Thus answer.
Thus we can provide solution.
Now, I'll also think about an alternative: Could we do without map? Possibly sort the reduced fractions? But map is fine.
Now let's test with random case: n=200k, all a_i=1, b_i random. Then fractions are -b_i, reduced to -b_i/1. So map key is ( -b_i, 1). Count frequency of each b_i? Actually -b_i, so distinct b_i give distinct keys. So map size up to 200k, fine.
Now, memory: map with 200k entries, each storing int, okay.
Now implement in C++ with proper includes.
But wait: In C++17, we have gcd function in ? In ? Actually in C++17, gcd is in . So include . Or we can define our own. We'll include bits/stdc++.h anyway.
Thus final code.
But think about potential overflow when computing num = -b_i: b_i can be -1e9, -b_i = 1e9; safe. b_i can be -1e9? -1e9 is within int. So fine.
Now, sometimes people represent the fraction as a pair of (b_i, a_i) after reducing with gcd and sign normalized such that a_i>0, but they count d = -b_i/a_i. So they might map (-b_i, a_i). But our approach is fine.
Now, final answer.
But wait: There is another nuance: For indices with a_i != 0, we treat them as fractions. However, what about indices with a_i != 0 and both a_i and b_i zero? That can't happen because if a_i != 0, b_i can be zero. That's fine.
Now, we should also consider that the maximum possible answer might be n, if all indices can be zero. That occurs if all a_i=0 and b_i=0, or all fractions are equal (i.e., -b_i/a_i constant) plus any always-zero indices. So answer up to n.
Now, one more: There might be a situation where we have a_i = 0 for some indices with b_i != 0, but we might still be able to get zero for those indices by choosing d = infinite? No, d must be real finite. So not.
Thus final.
But before finishing, let's think about potential pitfalls: The fraction representation: (num, den) as pair of ints after reduction. However, there is a subtlety: For negative zero? Not an issue.
Now, edge case: a_i could be very large (1e9) and b_i also large (1e9), gcd might be 1e9? Actually gcd(1e9, 1e9) = 1e9. So after division, numerator= -1? Wait if a_i=1e9, b_i=1e9? Then num = -1e9, den=1e9 => g=1e9 => num=-1, den=1. So ( -1,1). Works.
Now, another nuance: When denominator after reduction becomes 0? Not possible.
Thus we are done.
One might also think about using double and map but that's dangerous due to floating errors.
Now, provide final solution explanation.
But the problem statement says "Print one integer — the maximum number of zeroes in array c". So we output integer.
Thus answer.
Now, let's also consider if we could have a scenario where we have a_i != 0 and the fraction is not defined? Actually it's always defined.
Now, time to write final answer.
But before finalizing, let's consider the following: Is it possible to get more than always_zero + max_freq by picking a d that makes zero for some indices with a_i != 0 that have different fractions? In general, if there are two different fractions, we cannot satisfy both simultaneously with a single d (unless they are equal). Because d must satisfy both equations, which would require the fractions to be equal. So no.
Thus done.
Now, code.
But I recall that there might be a special case: if we have a_i = 0 for some i, but b_i = 0, they are always zero. However, could we have a_i = 0 and b_i = 0 count as part of fraction? Not needed.
Now, final solution.
Time to produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector a(n), b(n);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
int always_zero = 0;
map<pair<int, int>, int> freq;
for (int i = 0; i < n; ++i) {
if (a[i] == 0) {
if (b[i] == 0) ++always_zero;
// if a[i]==0 and b[i]!=0, c_i can never be zero
} else {
// d = -b_i / a_i
int num = -b[i];
int den = a[i];
int g = gcd(abs(num), abs(den));
num /= g;
den /= g;
// make denominator positive
if (den < 0) {
den = -den;
num = -num;
}
// increment frequency of the reduced fraction
++freq[{num, den}];
}
}
int max_freq = 0;
for (auto &[_, cnt] : freq) {
max_freq = max(max_freq, cnt);
}
cout << always_zero + max_freq << "\n";
return 0;
}