P9816 少项式复合幂 题解
题目链接:少项式复合幂
注意到题目的模并不是很大,我们考虑两个核心的性质。
- \(f(f(x)) \bmod p=f(f(x) \bmod p) \bmod p\),证明直接代入 \(f(x)\) 进去得到:\(f(f(x))=a_0 \times f^0(x)+a_1\times f^1(x)...+a_n\times f^n(x) \bmod p=\)
\]
- \(f_{x_1+x_2}(x)=f_{x_2}(f_{x_1}(x))\),这个很显然,进行 \(x_1\) 次函数迭代以后再进行 \(x_2\) 次函数迭代,总函数迭代次不变。
基于第二点,我们就可以倍增了,基于第一点,我们可以预处理出所有的 \(f(i) \bmod p\),用于第二点的倍增。
倍增实际上就是将一个需要走 \(k\) 步的函数迭代,根据它的二进制分解为走 \(k_1+k_2+k_3...\) 步,而一个数的二进制数不会超过 \(\log{V_{max}}\) 级别。
参考代码
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 1e5 + 10;
constexpr int PMax = 1e5;
int a[N], b[N];
int n, p, q;
inline int f(const ll x)
{
ll ans = 0;
forn(i, 1, n)
{
ll curr = qPow(x, b[i], p); //快速幂快速算x^b[i]
ans = (ans + a[i] * curr) % p; //算出a*x^b[i]加上去
}
return ans;
}
constexpr int T = ceil(log2(1e7));
int go[N][T + 1]; //倍增预处理
inline void solve()
{
cin >> n >> q >> p;
forn(i, 1, n)cin >> a[i] >> b[i];
forn(i, 0, PMax)go[i][0] = f(i);
forn(j, 1, T)forn(i, 0, PMax)go[i][j] = go[go[i][j - 1]][j - 1];//倍增预处理
while (q--)
{
int x, y;
cin >> x >> y;
x %= p;
//二进制分解
while (y)
{
int t = log2(y);
x = go[x][t];
y -= 1 << t;
}
cout << x << endl;
}
}
signed int main()
{
Spider
//------------------------------------------------------
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
}
\]
P9816 少项式复合幂 题解的更多相关文章
- 《Concrete Mathematics》-chaper5-二项式系数
二项式系数,也是我们常用的组合数,最直观的组合意义就是从n个元素取k个元素所有可能的情况数,因此我们自然的得到下面二项式系数的定义式. 那么我们通过具有组合意义的二项系数,给出更加一般的二项式系数的定 ...
- PTA-多项式A除以B
多项式A除以B 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出A, ...
- poj3070 求斐波那契数列第n项 ——矩阵快速幂
题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...
- [SCOI2009]迷路(矩阵快速幂) 题解
Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...
- [51NOD1126]求递推序列的第n项(矩阵快速幂)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 存在参数a,b为负数的情况.这时候要这么处理: 根据mo ...
- 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换
两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时 ...
- Python----多项式回归
多项式线性回归 1.多项式线性方程: 与多元线性回归相比,它只有一个自变量,但有不同次方数. 2.举例: import numpy as np import matplotlib.pyplot as ...
- poj1840 五项式等于0(哈希)
题目传送门 题意很好懂,注意一下xi不能等于0 思路:智商检测题,一开始想着五重for暴力...Orz,后来移向(把a4a5移到右边)了发现减了1e8数量级的复杂度,再次Orz,所以直接三重循环,记录 ...
- 2018-6-29-PTA-6-2-多项式求值
title author date CreateTime categories PTA 6-2 多项式求值 lindexi 2018-06-29 15:24:28 +0800 2018-6-14 22 ...
- LuoguB2034 计算 2 的幂 题解
Content 给定整数 \(n\),求 \(2^n\). 数据范围:\(0\leqslant n<31\). Solution 第一种各位都能想得到的,直接循环 \(n\) 次,往答案里面乘以 ...
随机推荐
- Codeforce 515C. Drazil and Factorial(字符串思维题)
[CodeForces]C. Drazil and Factorial 题目链接:Click Here 题意:找一个最大的数,使得每个位的阶乘的乘积与给定数相同. 首先將 2~9 轉成這樣(0,1為空 ...
- 活动回顾|阿里云 Serverless 技术实战与创新成都站回放&PPT下载
7月29日"阿里云 Serverless 技术实战与创新"成都站圆满落幕.活动受众以关注 Serverless 技术的开发者.企业决策人.云原生领域创业者为主,活动形式为演讲.动手 ...
- vue权限管理
https://www.bilibili.com/video/BV1nq4y1i7BU/?spm_id_from=333.788.recommend_more_video.6&vd_sourc ...
- [Qt开发/毕业设计/求职项目]局域网环境下远程文件发送部署系统-服务端、客户端双端的讲解
写在前面 本文旨在做一个简单的代码讲解,我会给出源码,然后整个代码的讲解都在源码的基础上进行. 代码可能会随着更新而进行修改,但是整体框架变化不会太大. 整个文章内容不会太多,算是我自己的一个复盘,整 ...
- GCC 指定运行期动态链接库搜索路径
链接器 ld 的 -rpath=dir 选项可以指定运行期 so 文件的搜索路径. GCC 的 -Wl,option 选项可以传递选项给链接器 ld. 所以组合起来,可以直接使用 -Wl,-rpath ...
- [AGC058C] Planar Tree 题解
前言 赛时没做出来,赛后把题补了.果然是 maroonrk 出的,名不虚传啊--真的很好的一道题目. 解法 题目中的圆周有以下几个性质: 圆周上如果有相邻的等值,我们可以去掉一个而不改变答案(这个很好 ...
- HanLP — 感知机(Perceptron)
感知机(Perceptron)是一个二类分类的线性分类模型,属于监督式学习算法.最终目的: 将不同的样本分本 感知机饮食了多个权重参数,输入的特征向量先是和对应的权重相乘,再加得到的积相加,然后将加权 ...
- bootstrap : 解决使图片全屏显示有空白边距的问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8 ...
- [转帖]Web技术(四):TLS 握手过程与性能优化(TLS 1.2与TLS 1.3对比)
https://blog.csdn.net/m0_37621078/article/details/106126033?ops_request_misc=%257B%2522request%255Fi ...
- [转帖]038-拯救大兵瑞恩之 TiDB 如何在 TiKV 损坏的情况下恢复
https://tidb.net/blog/4b5451bb?utm_source=tidb-community&utm_medium=referral&utm_campaign=re ...