結果
問題 | No.1349 Subset Product Queries |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:19:19 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,614 bytes |
コンパイル時間 | 142 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 311,908 KB |
最終ジャッジ日時 | 2025-03-20 21:20:34 |
合計ジャッジ時間 | 5,560 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 10 TLE * 1 -- * 19 |
ソースコード
import sysfrom sys import stdinfrom math import isqrtdef input():return stdin.read()def factorize(n):factors = {}i = 2while i * i <= n:while n % i == 0:factors[i] = factors.get(i, 0) + 1n = n // ii += 1if n > 1:factors[n] = 1return factorsdef main():data = sys.stdin.read().split()ptr = 0N = int(data[ptr])Q = int(data[ptr+1])P = int(data[ptr+2])ptr +=3A = list(map(int, data[ptr:ptr+N]))ptr +=Nqueries = []for _ in range(Q):L = int(data[ptr])-1 # 0-basedR = int(data[ptr+1])-1K = int(data[ptr+2])queries.append( (L, R, K) )ptr +=3# Preprocessing for K=0 case# 1. Check for A[j] mod P ==0has_zero = [0]*(N+1)for i in range(N):has_zero[i+1] = has_zero[i] + (1 if A[i] % P ==0 else 0)# Factorize Pfactors = factorize(P)if not factors:# P is 1passelse:primes = list(factors.items())# Precompute prefix sums for exponentsex_prefix = {}for p, e in primes:ex = [0]*(N+1)for i in range(N):a = A[i]cnt =0while a % p ==0 and a !=0:cnt +=1a = a //pex[i+1] = ex[i] + cntex_prefix[p] = ex# Preprocessing for K!=0 case: DP for each L# Since P can be up to 5000, N is 5000, and Q is 2e5, precompute masks for each L and R.MOD = Ppre = [ [0]*(N) for _ in range(N) ] # pre[L][R] = mask for [L, R]for L in range(N):current_mask = 0for R in range(L, N):a = A[R] % MODif a == 0:new_part = 0else:new_part = 0if current_mask ==0:new_part = 0else:tmp = current_maskwhile tmp:lsb = tmp & -tmpidx = (lsb).bit_length() -1m = (idx * a) % MODnew_part |= 1 << mtmp ^= lsbcurrent_mask = (current_mask | new_part)if a !=0:current_mask |= 1 << acurrent_mask &= (1 << MOD) -1pre[L][R] = current_mask# Process queriesresults = []for L, R, K in queries:if K ==0:# Check if any zero in [L, R]count = has_zero[R+1] - has_zero[L]if count >0:results.append("Yes")continueif P ==1:# Any non-empty subset, mod 1 is 0results.append("Yes")continue# Check if all primes have enough exponentsok = Truefor p, e in primes:total = ex_prefix[p][R+1] - ex_prefix[p][L]if total < e:ok = Falsebreakif ok:results.append("Yes")else:results.append("No")else:if P ==1:# K must be 0, but K!=0 hereresults.append("No")continueif L > R:results.append("No")continuemask = pre[L][R]if (mask & (1 << K)) !=0:results.append("Yes")else:results.append("No")print('\n'.join(results))if __name__ == "__main__":main()