B3831 [NICA #2] 亘久不变 题解
AndyPomeloMars · · 题解
B3831 [NICA #2] 亘久不变
题目大意
-
给定
4 个正整数n, x, y, p ,有n 个数的数列a 以及q 个数的询问队列k ,求出有多少个数满足进行k_i 次 变换 后该数不变。 -
定义一次 变换 为
t = (x \times t + y) \bmod p 。 -
数据保证,
1\le n\le 10^5 ,1\le p,q\le 10^3 ,1\le a_i,x,y,k_i\le 10^9 ,p 不一定是质数。
题目分析
易证:对于一个式子
那么题目中的一次 变换 也是如此,
因此根据题目范围
再加上询问的
题目代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 100010;
int N, X, Y, P, Q, K;
int ans[MAXN];
ll A[MAXN], B[MAXN];
int main(){
cin >> N >> X >> Y >> P;
for (int i = 1; i <= N; ++i) cin >> A[i], B[i] = A[i];
cin >> Q;
for (int i = 1; i <= P; ++i)
for (int j = 1; j <= N; ++j)
A[j] = (A[j] * X + Y) % P, ans[i] += (A[j] == B[j]);
ans[0] = ans[P];
while (Q--){
cin >> K;
cout << ans[K % P] << endl;
}
return 0;
}