Codeforces Round #594 (Div. 2) A. Integer Points 水題
A. Integer Points
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew ?? distinct lines, given by equations ??=??+???? for some distinct ??1,??2,…,????.
Then JLS drew on the same paper sheet ?? distinct lines given by equations ??=???+???? for some distinct ??1,??2,…,????.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer ?? (1≤??≤1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer ?? (1≤??≤105), the number of lines drawn by DLS.
The second line of a test case contains ?? distinct integers ???? (0≤????≤109) describing the lines drawn by DLS. The integer ???? describes a line given by the equation ??=??+????.
The third line of a test case contains an integer ?? (1≤??≤105), the number of lines drawn by JLS.
The fourth line of a test case contains ?? distinct integers ???? (0≤????≤109) describing the lines drawn by JLS. The integer ???? describes a line given by the equation ??=???+????.
The sum of the values of ?? over all test cases in the input does not exceed 105. Similarly, the sum of the values of ?? over all test cases in the input does not exceed 105.
In hacks it is allowed to use only one test case in the input, so ??=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
題意
一個二維平面,有兩種線,一種是y=x+p[i],一種是y=-x+q[i],問你這兩種直線相交后,有多少個整數交點。
題解
我們解出來相交點為 x = (p[i]+q[i])/2,那么只要同奇偶,就是整數交點
代碼
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int p[maxn],q[maxn],n,m;
void solve(){
vector<int>cnt1(2,0),cnt2(2,0);
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&p[i]);
cnt1[p[i]%2]++;
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&q[i]);
cnt2[q[i]%2]++;
}
cout<<1ll*cnt1[0]*cnt2[0]+1ll*cnt1[1]*cnt2[1]<<endl;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
solve();
}
}

浙公網安備 33010602011771號