#abc335g. G - Discrete Logarithm Problems

G - Discrete Logarithm Problems

Score : 600600 points

问题描述

给定 NN 个整数 A1,,ANA_1,\ldots,A_N 和一个质数 PP,找出满足以下两个条件的整数对 (i,j)(i,j) 的数量:

  • 1i,jN1 \leq i,j \leq N
  • 存在一个正整数 kk,使得 AikAjmodPA_i^k \equiv A_j \mod P

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN integers A1,,ANA_1,\ldots,A_N and a prime number PP. Find the number of pairs of integers (i,j)(i,j) that satisfy both of the following conditions:

  • 1i,jN1 \leq i,j \leq N;
  • There is a positive integer kk such that AikAjmodPA_i^k \equiv A_j \mod P.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1Ai<P1 \leq A_i < P
  • 2P10132 \leq P \leq 10^{13}
  • PP is prime.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN PP

A1A_1 \ldots ANA_N

Output

Print the answer.

Sample Input 1

3 13
2 3 5

Sample Output 1

5

Five pairs satisfy the conditions: (1,1),(1,2),(1,3),(2,2),(3,3)(1,1),(1,2),(1,3),(2,2),(3,3).

For example, for the pair (1,3)(1,3), if we take k=9k=9, then A19=5125=A3mod13A_1^9 = 512 \equiv 5 = A_3 \mod 13.

Sample Input 2

5 2
1 1 1 1 1

Sample Output 2

25

Sample Input 3

10 9999999999971
141592653589 793238462643 383279502884 197169399375 105820974944 592307816406 286208998628 34825342117 67982148086 513282306647

Sample Output 3

63

update @ 2024/3/10 01:24:39