D. Zero Quantity Maximization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two arrays aa and bb, each contains nn integers.
You want to create a new array cc as follows: choose some real (i.e. not necessarily integer) number dd, and then for every i∈[1,n]i∈[1,n] let ci:=d⋅ai+bici:=d⋅ai+bi.
Your goal is to maximize the number of zeroes in array cc. What is the largest possible answer, if you choose dd optimally?
Input
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in both arrays.
The second line contains nn integers a1a1, a2a2, ..., anan (−109≤ai≤109−109≤ai≤109).
The third line contains nn integers b1b1, b2b2, ..., bnbn (−109≤bi≤109−109≤bi≤109).
Output
Print one integer — the maximum number of zeroes in array cc, if you choose dd optimally.
Examples
input
Copy
5 1 2 3 4 5 2 4 7 11 3
output
Copy
2
input
Copy
3 13 37 39 1 2 3
output
Copy
2
input
Copy
4 0 0 0 0 1 2 3 4
output
Copy
0
input
Copy
3 1 2 -1 -6 -12 6
output
Copy
3
Note
In the first example, we may choose d=−2d=−2.
In the second example, we may choose d=−113d=−113.
In the third example, we cannot obtain any zero in array cc, no matter which dd we choose.
In the fourth example, we may choose d=6d=6.
枚举每组满足条件所对应的d的值,map记录取最大即可,注意0的情况。
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
long long a[200004];
long long b[200004];
int main()
{
long long n;cin>>n;
for (int i = 0; i < n; ++i) {
scanf("%lld",&a[i]);
}
for (int i = 0; i < n; ++i) {
scanf("%lld",&b[i]);
}
long long ans=0;
long long x=0;
map<pair<long long,long long>,long long>mp;
for (int i = 0; i < n; ++i) {
if(a[i]==0){
if(b[i]==0)x++;
continue;
}
else if(a[i]!=0&&b[i]==0)
{
mp[make_pair(0,0)]++;
}
else
{
int gcd=__gcd(a[i],b[i]);
a[i]/=gcd,b[i]/=gcd;
mp[make_pair(a[i],b[i])]++;
ans=max(ans,mp[make_pair(a[i],b[i])]);
}
}
ans=max(ans,mp[make_pair(0,0)]);
printf("%lld\n",ans+x);
}