题目链接:少项式复合幂

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

  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. C++ 的两种换行符区别

    当我们在C++执行一个输出语句时,在输出语句最后可以使用 std::endl 或 \n 建立一个新行. 但这两种换行方式对程序有不同的影响. std::endl 它在建立一个新的行的同时,还会自动刷新 ...

  2. 如何做一次完美的 ABTest?

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/mO5MdwG7apD6RzDhFwZhog作者:DuZhimin 越来越多的公司都在尝试 AB ...

  3. 运行vue项目时报错“ValidationError: Progress Plugin Invalid Options”

    https://blog.csdn.net/M_Nobody/article/details/123135041?spm=1001.2101.3001.6650.1&utm_medium=di ...

  4. Servlet系列:(HttpServletRequest、HttpServletResponse、ServletContext、ServletConfig)详解

    HttpServletRequest HttpServletRequest 对象:主要作用是用来接收客户端发送过来的请求信息,例如:请求的参数,发送的头信息等都属于客户端发来的信息,service() ...

  5. Clickhouse执行处理查询语句(包括DDL,DML)的过程

    Clickhouse执行处理查询语句(包括DDL,DML)的过程 总体过程 启动线程处理客户端接入的TCP连接: 接收请求数据,交给函数executeQueryImpl()处理: executeQue ...

  6. Git Clone一个GitHub仓库时,发生报错

    1.问题 1.使用HTTP方式:Git: fatal: unable to access ' https://github. com/Light-City/CPlusPlusThings. git/' ...

  7. 【MicroPython] 用 c 添加接口 -- 添加 module

    [来源]https://www.eemaker.com/micropython-add-module.html

  8. [转帖]记一次探索内存cache优化之旅

    https://developer.aliyun.com/article/972803 背景 项目上线以来,曾出现上传镜像.下发镜像时可用内存不足,性能发生抖动的情况.研究发现是容器的 page ca ...

  9. [转帖]Debian开启SSH

    一.Debian开启SSH 参考链接: https://blog.csdn.net/zzpzheng/article/details/71170572 https://help.aliyun.com/ ...

  10. [转帖]Linux之/etc/fstab文件讲解

    https://www.cnblogs.com/FengGeBlog/p/10178824.html /etc/fstab是用来存放文件系统的静态信息的文件.位于/etc/目录下,可以用命令less ...