D. Zero Quantity Maximization
You are given two arrays a and b, each contains integers.
You want to create a new array as follows: choose some real (i.e. not necessarily integer) number d, and then for every let .
Your goal is to maximize the number of zeroes in array . What is the largest possible answer, if you choose optimally?
Input
The first line contains one integer — the number of elements in both arrays.
The second line contains n
integers
The third line contains
integers
Output
Print one integer — the maximum number of zeroes in array , if you choose d optimally.
Examples
Input
3
13 37 39
1 2 3
Output
2
题目大意
给你数组A和数组B, 让你找到任意实数d使得,尽可能多的
.成立。
本来我是直接用
除以
, 得到一个
, 然后用
计数。但是精度问题,不可以这样做。
所以必须想办法保存这个状态。观察,要使
成立,固定d不变。 那么对于
属于任意数都成立。
从这里我们看出来
了,将
除以
后组合成
,再用
统计即可。
注意对于每组 有四种情况
- 此时,数据无效
- 此时, 可以为任意数,一定可以入选
- 此时, 仅可为
- 此时,需要除以最小公倍数,进行统计
#include<bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int a[MAX];
int ans;
int s, t;
map<pair<int, int>, int> mp;
int gcd(int x, int y){
return !(x%y) ? y : gcd(y, x % y);
}
int main(){
int n; cin >> n;
ans = s = t = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int temp;
for(int i = 0; i < n; i++){
cin >> temp;
if(!temp && !a[i]) s++;
else if(!temp) t++;
else if(!a[i]) continue;
else{
if(a[i] < 0){
a[i] = -a[i];
temp = -temp;
}
int gd = gcd(a[i], abs(temp));
pair<int, int> p = make_pair(a[i]/gd, temp/gd);
mp[p]++;
ans = max(ans, mp[p]);
}
}
ans = max(t, ans);
cout << ans + s << endl;
return 0;
}