1. 程式人生 > >L - Ray in the tube Gym - 101911L (暴力)

L - Ray in the tube Gym - 101911L (暴力)

coo div mit repr 取代 alt eve ast number

---恢復內容開始---

You are given a tube which is reflective inside represented as two non-coinciding, but parallel to OxOx lines. Each line has some special integer points — positions of sensors on sides of the tube.

You are going to emit a laser ray in the tube. To do so, you have to choose two integer points

AA and BB on the first and the second line respectively (coordinates can be negative): the point AA is responsible for the position of the laser, and the point BB — for the direction of the laser ray. The laser ray is a ray starting at AA and directed at BB which will reflect from the sides of the tube (it doesn‘t matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor.

技術分享圖片 Examples of laser rays. Note that image contains two examples. The 3 sensors (denoted by black bold points on the tube sides) will register the blue ray but only 2 will register the red.

Calculate the maximum number of sensors which can register your ray if you choose points AA and BB on the first and the second lines respectively.

Input

The first line contains two integers n and y1 (1n1051≤n≤105 , 0y11090≤y1≤109 ) — number of sensors on the first line and its yy coordinate.

The second line contains nn integers a1,a2,,an (0ai1090≤ai≤109 ) — xx coordinates of the sensors on the first line in the ascending order.

The third line contains two integers m and y2 (1m1051≤m≤105 , y1<y2109y1<y2≤109 ) — number of sensors on the second line and its yy coordinate.

The fourth line contains mm integers b1,b2,,bm(0bi1090≤bi≤109 ) — xx coordinates of the sensors on the second line in the ascending order.

Output

Print the only integer — the maximum number of sensors which can register the ray.

Example

Input
3 1
1 5 6
1 3
3
Output
3

Note

One of the solutions illustrated on the image by pair A2 and B2 .

題意:

給出n個點在y1的水平線上,給出m個點在y2的水平面上,有一道光線可以在這兩個水平線中折射,並且從任意位置開始,求對多可以經過多少了點

思路:我們可以枚舉光的折射長度,也就是從下界到上界再回到下界的長度,可以發現這樣兩界面的高度y可以忽略

另外,我們不可能從1枚舉1e9。

①顯然,任何奇數步長可以有步長1取代

關鍵就是偶數步長,任何偶數長度可以有 2n * m (n>=1,m為奇數), 因為任何偶數都可以被2整除,那麽當商為偶數時,我們可以將商提出2,將2乘上2,這樣仍是2的冪次,然後直到商就變成了奇數

②偶數可以用2n代替

綜上,枚舉長度2n (0 <= n <= log(1e9)),

然後我們可以知道,對於一個定長度的步長,然後界面上的點取模2倍步長,余數相同的就是在一條折射線上的,對於另一界面,不能直接取模2倍步長,應該加上步長再取模2倍步長

技術分享圖片
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int maxn = 1e5+5;
int a[maxn];
int b[maxn];
int tmp[maxn<<1];

int main()
{
    int val;
    scanf("%d%d",&n,&val);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    scanf("%d%d",&m,&val);
    for(int i=1;i<=m;i++)scanf("%d",&b[i]);
    int ans = 2;
    int tot = n+m;
    tmp[tot+1] = 2e9+1;
    for(int step = 1;step <= int(1e9);step<<=1)
    {
        int mod = step<<1;
        for(int i=1;i<=n;i++)tmp[i] = a[i]%mod;
        for(int i=1;i<=m;i++)tmp[i+n] = (b[i]+step)%mod;
        sort(tmp+1,tmp+1+tot);
        for(int i=1,last=1;i<=tot;i++)
        {
            if(tmp[i+1] != tmp[i])
            {
                ans = max(ans,i+1-last);
                last = i+1;
            }
        }
    }
    printf("%d\n",ans);
}
View Code

L - Ray in the tube Gym - 101911L (暴力)