結果
問題 |
No.1025 Modular Equation
|
ユーザー |
![]() |
提出日時 | 2025-04-15 22:12:28 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,811 bytes |
コンパイル時間 | 300 ms |
コンパイル使用メモリ | 81,900 KB |
実行使用メモリ | 182,648 KB |
最終ジャッジ日時 | 2025-04-15 22:14:11 |
合計ジャッジ時間 | 9,462 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 6 TLE * 1 -- * 25 |
ソースコード
import sys MOD = 10**9 + 7 def main(): p, n, k, b = map(int, sys.stdin.readline().split()) a_list = list(map(int, sys.stdin.readline().split())) # Separate zero and non-zero a_i zero_a = [] non_zero_a = [] for a in a_list: if a == 0: zero_a.append(a) else: non_zero_a.append(a) m = len(zero_a) n_prime = len(non_zero_a) # Precompute d = gcd(k, p-1) d = 0 if p > 1: d = pow(k, 1, p-1) d = gcd(d, p-1) else: d = 1 # p=2, p-1=1 # Precompute primitive root (not necessary here, but we can compute k-th power residues) # For each non-zero a_i, compute the set S_i of residues v where a_i x^k ≡ v mod p has solutions # For a_i !=0, f_i[v] = 1 if v ==0, else d if v*inv_ai is a k-th power residue, else 0 # So for each non-zero a_i, precompute inv_ai and the set of v where v*inv_ai is a k-th power residue # But instead of precomputing S_i, we can compute f_i[v] on the fly # Precompute for each non-zero a_i, the frequency array # But to optimize, precompute for each a_i, the inv_ai and then for each v, compute c = v * inv_ai mod p # and check if c is a k-th power residue # Function to compute the frequency array for a given a_i def compute_f(a): if a == 0: return [0]*p # handled separately inv_a = pow(a, p-2, p) f = [0]*p # Compute d = gcd(k, p-1) if p == 1: d_p = 1 else: k_mod = k % (p-1) d_p = gcd(k_mod, p-1) # For each v in 0..p-1 for v in range(p): c = (v * inv_a) % p if c == 0: f[v] = 1 else: # Check if c is a k-th power residue # Compute c^((p-1)/d_p) mod p. If it's 1, then yes. if pow(c, (p-1)//d_p, p) == 1: f[v] = d_p else: f[v] = 0 return f # Compute the frequency arrays for non-zero a_i freq_list = [] for a in non_zero_a: freq = compute_f(a) freq_list.append(freq) # Now, compute the DP dp = [0] * p dp[0] = 1 for freq in freq_list: new_dp = [0] * p for s in range(p): if dp[s] == 0: continue for v in range(p): if freq[v] == 0: continue new_s = (s + v) % p new_dp[new_s] = (new_dp[new_s] + dp[s] * freq[v]) % MOD dp = new_dp # Handle the zero terms pow_p_m = pow(p, m, MOD) answer = (dp[b] * pow_p_m) % MOD print(answer) def gcd(a, b): while b: a, b = b, a % b return a if __name__ == '__main__': main()