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使得d*a[i]+b[i]能得到的0最多,如果a[i]和b[i]都为0,则任意值都可满足i这个数据,如果r[i]为0
而B[i]不为0则不存在这样的d。其他情况可用map存long double ,进行更新最优值即可。注意这里一定是long double ,否则会损失精度导致输出错误。也可用pair类型映射,运用gcd将分式化为最简分式存入map的pair,进行最优值选取即可。
感受:思路很清晰的一道题,打比赛时想的太复杂,还是缺少将实际问题快速转化为数学或算法问题的能力。
高精度版本:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
typedef long long ll;
map<long double,ll>mp;
ll a[200010],b[200010];
ll ans,n,cnt,x,y;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
if(a[i]==0)
{
if(b[i]==0)//全为0时可任意取值
cnt++;
continue; //如果a[i]为0而b[i]不为0则不存在d
}
mp[(long double)b[i]/a[i]]++;
ans=max(ans,mp[(long double)b[i]/a[i]]);
}
cout<<ans+cnt<<endl;//最大值加上可任意取值的个数
return 0;
}
pair版本:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
typedef long long ll;
map<P,ll>mp;
ll a[200010],b[200010];
ll ans,n,cnt,x,y;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
if(a[i]==0)
{
if(b[i]==0)//全为0时可任意取值
cnt++;
continue; //如果a[i]为0而b[i]不为0则不存在d
}
int g=gcd(a[i],b[i]);
mp[P(a[i]/g,b[i]/g)]++;
ans=max(ans,mp[P(a[i]/g,b[i]/g) ]);
}
cout<<ans+cnt<<endl;//最大值加上可任意取值的个数
return 0;
}