#P9914. 「RiOI-03」匀速相遇

    ID: 10214 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>洛谷原创O2优化哈希 hashing洛谷月赛双指针 two-pointer

「RiOI-03」匀速相遇

Background

When everyone is accelerating, you and I met at a crossroads in life at a constant speed.

It was a glance that stirred my heart, yet also a regret that we could not stay. Once again, we run at a constant speed toward our own directions. When will we meet again next time...

Problem Description

There are n+mn + m points on the Cartesian plane, where:

  • There are nn type A\rm A points. Initially, they are located at (1,0),(2,0),(3,0),,(n,0)(1, 0), (2, 0), (3, 0), \dots, (n, 0) in order.
  • There are mm type B\rm B points. Initially, they are located at (0,1),(0,2),(0,3),,(0,m)(0, 1), (0, 2), (0, 3), \dots, (0, m) in order.

At some moment, type A\rm A and type B\rm B points start moving at the same time. Specifically:

  • For the ii-th type A\rm A point, it moves upward (i.e. in the positive direction of the yy axis) at a constant speed of aia_i units per second. In particular, if ai=0a_i = 0, then the point stays still forever.
  • For the ii-th type B\rm B point, it moves rightward (i.e. in the positive direction of the xx axis) at a constant speed of bib_i units per second. In particular, if bi=0b_i = 0, then the point stays still forever.

Meeting and parting are nothing special. As a passer-by in rushing time, at this station where you stay briefly, can you help Little T solve this simple problem: find how many pairs of points will meet at some moment, i.e. they are at the same position at some time.

Since you cannot stop time, all points will keep moving endlessly, whether they meet or not. May you, running on this road, one day meet your ideals at a constant speed, never stopping.

Input Format

The first line contains two positive integers n,mn, m.

The second line contains nn integers a1ana_1\dots a_n, representing the moving speeds of the 1n1\dots n-th type A\rm A points in order.

The third line contains mm integers b1bmb_1\dots b_m, representing the moving speeds of the 1m1\dots m-th type B\rm B points in order.

Output Format

One line with one integer, representing how many pairs of points will meet at some moment.

3 3
1 2 3
3 2 1
1
3 3
2 5 1
83 101 98
0

Hint

Sample Explanation 1

When t=1t = 1, the 22-nd type A\rm A point and the 22-nd type B\rm B point reach the point (2,2)(2, 2) at the same time. This is also the only meeting in this sample, so the output is 11.

Data Scale and Constraints

This problem uses bundled testdata.

  • Subtask 0 (10 pts): n10n \leq 10, m10m \leq 10.
  • Subtask 1 (20 pts): n5×103n \leq 5\times 10^3, m5×103m \leq 5\times 10^3.
  • Subtask 2 (30 pts): guaranteed ai1\forall a_i \geq 1, bi1\forall b_i \geq 1.
  • Subtask 3 (40 pts): no special restrictions.

For all testdata, 1n,m1061 \leq n, m \leq 10^6, 0ai,bi1090 \leq a_i, b_i \leq 10^9.

Translated by ChatGPT 5