3267.Hamming Triples

Time Limit: 1s Memory Limit: 256MB

Little Chris is having a nightmare. Even in dreams all he thinks about is math.

Chris dreams about m binary strings of length n, indexed with numbers from 1 to m. The most horrifying part is that the bits of each string are ordered in either ascending or descending order. For example, Chris could be dreaming about the following 4 strings of length 5:

3267_1.png

The Hamming distance H(a,b) between two strings a and b of length n is the number of positions at which the corresponding symbols are different.

Chris thinks that each three strings with different indices constitute a single triple. Chris's delusion is that he will wake up only if he counts the number of such string triples a, b, c that the sum H(a,b)+H(b,c)+H(c,a) is maximal among all the string triples constructed from the dreamed strings.

Help Chris wake up from this nightmare!

Input Format(From the terminal/stdin)

The first line of input contains two space-separated integers n and m (1 \le n \le 109; 3 \le m \le 105), the length and the number of strings. The next m lines contain the description of the strings. The i-th line contains two space-separated integers si and fi (0 \le si \le 1; 1 \le fi \le n), the description of the string with index i; that means that the first fi bits of the i-th string are equal to si, and the remaining n-fi bits are equal to 1-si. There can be multiple equal strings in Chris's dream.

Output Format(To the terminal/stdout)

Output a single integer, the number of such string triples among the given that the sum of the Hamming distances between the strings of the triple is maximal.

Sample Input 1

Copy
5 4
0 3
0 5
1 4
1 5
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 1

Copy
3
 \n

Sample Input 2

Copy
10 4
1 5
0 5
0 5
1 5
  · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
4
 \n

Submit

请先 登录

© 2025 FAQs