题目链接:少项式复合幂

注意到题目的模并不是很大,我们考虑两个核心的性质。

  1. \(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=\)
\[\sum_{i=0}^{n}a_i \times f^{i}(x) \bmod p=\sum_{i=0}^{n}a_i \times (f^{i}(x) \bmod p) \bmod p,根据模运算的四则运算得证。
\]
  1. \(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();
}
\[时间复杂度为 \ O(m\times p\log{b_{max}}+p\log{p}+q\log{y_{max}})
\]

P9816 少项式复合幂 题解的更多相关文章

  1. 《Concrete Mathematics》-chaper5-二项式系数

    二项式系数,也是我们常用的组合数,最直观的组合意义就是从n个元素取k个元素所有可能的情况数,因此我们自然的得到下面二项式系数的定义式. 那么我们通过具有组合意义的二项系数,给出更加一般的二项式系数的定 ...

  2. PTA-多项式A除以B

    多项式A除以B 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出A, ...

  3. poj3070 求斐波那契数列第n项 ——矩阵快速幂

    题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...

  4. [SCOI2009]迷路(矩阵快速幂) 题解

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  5. [51NOD1126]求递推序列的第n项(矩阵快速幂)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 存在参数a,b为负数的情况.这时候要这么处理: 根据mo ...

  6. 《Introduction to Algorithm》-chaper30-多项式与快速傅里叶变换

    两个n次多项式的相加最直接的方法所需要的时间是O(n),而实现两个n次多项式的乘法的直接方法则需要O(n^2),本章讨论的快速傅里叶变换(FFT),将会将这一过程的时间复杂度降至O(nlogn).同时 ...

  7. Python----多项式回归

    多项式线性回归 1.多项式线性方程: 与多元线性回归相比,它只有一个自变量,但有不同次方数. 2.举例: import numpy as np import matplotlib.pyplot as ...

  8. poj1840 五项式等于0(哈希)

    题目传送门 题意很好懂,注意一下xi不能等于0 思路:智商检测题,一开始想着五重for暴力...Orz,后来移向(把a4a5移到右边)了发现减了1e8数量级的复杂度,再次Orz,所以直接三重循环,记录 ...

  9. 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 ...

  10. LuoguB2034 计算 2 的幂 题解

    Content 给定整数 \(n\),求 \(2^n\). 数据范围:\(0\leqslant n<31\). Solution 第一种各位都能想得到的,直接循环 \(n\) 次,往答案里面乘以 ...

随机推荐

  1. 最简单的http文件服务器

    概述 需求来了,部署一个简单方便的文件服务器,可以查看.下载文件,不需要鉴权,越简单越好. 环境 centos:CentOS  release 7.0 (Final)或以上版本 httpd:2.4.6 ...

  2. python之十进制、二进制、八进制、十六进制转换

    数字处理的时候偶尔会遇到一些进制的转换,以下提供一些进制转换的方法 一.十进制转化成二进制 使用bin()函数 1 x=10 2 print(bin(x)) 二.十进制转化为八进制 使用oct()函数 ...

  3. Windows Terminal 配色方案

    { "background": "#FFF2E2", "black": "#000000", "blue&qu ...

  4. JUC包常用类原理

    放眼望去,java.util.concurrent包下类大致包括:atomic 原子类.锁.并发集合.线程池.工具类.我们挑重要的了解一下. Atomic 原子类 Java针对并发编程已经有了各种锁, ...

  5. SoC scan implementation

    scan chain产生之前需要进行scan drc的过程,判断cell是不是能够串到scan chain上去 mux-d scan cell(是最常用的scan cell),还有其他的scan ce ...

  6. SpringMVC05——SSM整合

    整合SSM 需求:熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识 CREATE DATABASE `ssmbuild`; USE `ssmbuild`; D ...

  7. UofTCTF 2024 比赛记录

    这次的题目挺有意思,难度适中,*开头的代表未做出,简单记录一下解题笔记. Introduction General Information 题目 The flag format for all cha ...

  8. [转帖]LSM-Tree:从入门到放弃——入门:基本概念、操作和Trade-Off分析

    https://zhuanlan.zhihu.com/p/428267241 LSM-Tree,全程为日志结构合并树,有趣的是,这个数据结构实际上重点在于日志结构合并,和 tree 本身的关系并不是特 ...

  9. [转帖]docker-compose完全清除

    https://www.cnblogs.com/gelandesprung/p/12112420.html#:~:text=docker-compose%E5%AE%8C%E5%85%A8%E6%B8 ...

  10. [转帖]Elasticsearch-索引性能调优

    1:设置合理的索引分片数和副本数 索引分片数建议设置为集群节点的整数倍,初始数据导入时副本数设置为 0,生产环境副本数建议设置为 1(设置 1 个副本,集群任意 1 个节点宕机数据不会丢失:设置更多副 ...