#P10918. 小分图最大匹配

小分图最大匹配

Problem Description

You are given a bipartite graph with some left-side vertices and mm right-side vertices, numbered from 00 to m1m-1.

For each left-side vertex ii, there is an edge parameter aia_i, meaning it connects to every jj such that xN,aixj(modm)\exist x\in \mathbb{N}, a_ix\equiv j \pmod m.

Now you are given two arrays ai,cia_i, c_i of length nn, meaning there are cic_i left-side vertices whose edge parameter is aia_i. In other words, this graph has a total of i=1nci\sum\limits_{i=1}^n c_i left-side vertices. This is a fast way to input the edge parameters of the left-side vertices.

It is guaranteed that all aia_i are pairwise distinct.

Find the maximum matching of this bipartite graph.

Input Format

The first line contains two integers nn and mm.

In the next nn lines, each line contains two positive integers. On the ii-th line, the two numbers are aia_i and cic_i.

Output Format

Output one integer in one line, which is the answer.

3 6
1 1
2 2
3 1
4
6 12
12 3
3 1
2 1
6 2
4 2
1 2
8
11 60
4 1
15 10
3 1
20 3
2 2
30 2
6 4
10 5
5 3
60 5
12 14
23

Hint

This problem uses bundled subtasks.

Subtask nn\le mm\le Special Property Score
0 10310^3 None 10
1 11 101210^{12}
2 2×1032 \times 10^3 2×1072\times 10^7 Yes 20
3 None
4 101210^{12} Yes
5 7×1037 \times 10^3 None

For all testdata, it is guaranteed that $1\le n \le 7\times10^3, 1\le m \le 10^{12},1\le a_i \le m,0\le c_i \le 10^{12}$.

Special property: it is guaranteed that μ(m)0\mu(m)\not=0.

Here μ\mu is the Möbius function, defined as

$$\mu(n)=\left\{\begin{array}{ll} 1 & n=1 \\ 0 & n \text { 含有平方因子 } \\ (-1)^{k} & k \text { 为 } n \text { 的本质不同质因子个数 } \end{array}\right.$$

For all testdata, it is guaranteed that all aia_i are pairwise distinct.

Translated by ChatGPT 5