洛谷 - P9816

· · 个人记录

题意

给定多项式 f(x)=\sum_{i=1}^ma_ix^{b_i}。定义 f_1(x)=f(x),f_n(x)=f(f_{n-1}(x))。

给定模数 p。有 q 次询问,每次给出 x,y,查询 f_y(x)\bmod p 的值。

思路

预处理出所有 f(x),然后用倍增维护 f_y(x)

# 代码 ~~~Cpp #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; } ~~~