题意
给定多项式 。定义 ,。
给定模数 。有 次询问,每次给出 ,查询 的值。
思路
预处理出所有 ,然后用倍增维护
,所以是可行的。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 22, M = 1e5 + 2;
int m, q, p;
int a[N], b[N];
int x, y;
int f[M];
ll po(ll a, ll b, ll mod){
ll ans = 1;
while(b){
if(b & 1){
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
int f_x(int x){
int sum = 0;
for(int i = 1; i <= m; i ++){
sum = (sum + a[i] * po(x, b[i], p) % p) % p;
}
return sum;
}
int t[M][30];
int ans;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> m >> q >> p;
for(int i = 1; i <= m; i ++){
cin >> a[i] >> b[i];
}
for(int i = 0; i < p; i ++){
f[i] = f_x(i);
}
for(int i = 0; i < p; i ++){
t[i][0] = f[i];
}
for(int j = 1; j < 30; j ++){
for(int i = 0; i < p; i ++){
t[i][j] = t[t[i][j - 1]][j - 1];
}
}
///cout << "11111111111111111111111111\n";
while(q --){
cin >> x >> y;
ans = x % p;
for(int i = 0; i < 30; i ++){
if(y >> i & 1){
ans = t[ans][i];
}
}
cout << ans << '\n';
}
return 0;
}