题目大意:求:
$$
\sum\limits_{i=0}^na^{n-i}b^i\pmod{p}
$$
$T(T\leqslant10^5)$组数据,$a,b,n,p\leqslant10^{18}​$

题解:$\sum\limits_{i=0}^na^{n-i}b^i=\dfrac{a^{n+1}-b^{n+1}}{a-b}$,然后$a-b$可能在$\pmod p$下没有逆元或者干脆是$0$。

出题人给了一个递归讨论$n$奇偶性的做法。(出题人在题解中各种表达他的毒瘤)

这边讲一个矩阵快速幂的。

令$f_n=\sum\limits_{i=0}^na^{n-i}b^i$

考虑$f_n\to f_{n+1}$,发现$f_{n+1}=af_n+b^{n+1}$,于是就可以愉快地矩阵快速幂啦。转移矩阵:
$$
\left[
\begin{matrix}
a&0\\
1&b
\end{matrix}
\right]
$$

把$[f_n,b^{n+1}]$左乘转移矩阵就可以得到$[f_{n+1},b_{n+2}]$,为了方便,可以把向量写成矩阵,然后发现若初始矩阵如下时:
$$
\left[
\begin{matrix}
0&0\\
1&b
\end{matrix}
\right]
$$
转移矩阵、状态矩阵右上角一定为$0$,就可以减少常数啦!

卡点:

C++ Code:

#include <cstdio>
#include <cctype>
namespace std {
struct istream {
#define M (1 << 22 | 3)
char buf[M], *ch = buf - 1;
inline istream() { fread(buf, 1, M, stdin); }
inline istream& operator >> (int &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
inline istream& operator >> (long long &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cin;
struct ostream {
#define M (1 << 20 | 3)
char buf[M], *ch = buf - 1;
inline ostream& operator << (long long x) {
if (!x) {*++ch = '0'; return *this;}
static int S[20], *top; top = S;
while (x) {*++top = x % 10 ^ 48; x /= 10;}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (const char x) {*++ch = x; return *this;}
inline ~ostream() { fwrite(buf, 1, ch - buf + 1, stdout); }
#undef M
} cout;
} int Tim;
long long n, a, b, mod; inline void reduce(long long &x) { x += x >> 63 & mod; }
inline long long mul(long long x, long long y) {
long long res = x * y - static_cast<long long> (static_cast<long double> (x) * y / mod + 0.5) * mod;
return res + (res >> 63 & mod);
} struct Matrix {
long long s00, s10, s11;
Matrix() { }
Matrix(long long __s00, long long __s10, long long __s11) : s00(__s00), s10(__s10), s11(__s11) { }
#define M(l, r) mul(s##l, rhs.s##r)
inline void operator *= (const Matrix &rhs) {
static long long __s00, __s10, __s11;
__s00 = M(00, 00);
reduce(__s10 = M(10, 00) + M(11, 10) - mod);
__s11 = M(11, 11);
s00 = __s00, s10 = __s10, s11 = __s11;
}
#undef M
} ; long long calc(long long n) {
a %= mod, b %= mod;
Matrix base(a, 1, b), res(0, 1, b);
for (; n; n >>= 1, base *= base) if (n & 1) res *= base;
return res.s10;
} int main() {
std::cin >> Tim;
while (Tim --> 0) {
std::cin >> n >> a >> b >> mod;
std::cout << calc(n) << '\n';
}
return 0;
}

  

[洛谷P5137]polynomial的更多相关文章

  1. 【洛谷4721】【模板】分治FFT(CDQ分治_NTT)

    题目: 洛谷 4721 分析: 我觉得这个 "分治 FFT " 不能算一种特殊的 FFT ,只是 CDQ 分治里套了个用 FFT (或 NTT)计算的过程,二者是并列关系而不是偏正 ...

  2. 【洛谷3321_BZOJ3992】[SDOI2015]序列统计(原根_多项式)

    题目: 洛谷3321 分析: 一个转化思路比较神(典型?)的题-- 一个比较显然的\(O(n^3)\)暴力是用\(f[i][j]\)表示选了\(i\)个数,当前积在模\(m\)意义下为\(j\)的方案 ...

  3. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  4. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  5. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  6. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  7. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  8. 洛谷P1371 NOI元丹

    P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交  讨论  题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...

  9. 洛谷P1538迎春舞会之数字舞蹈

    题目背景 HNSDFZ的同学们为了庆祝春节,准备排练一场舞会. 题目描述 在越来越讲究合作的时代,人们注意的更多的不是个人物的舞姿,而是集体的排列. 为了配合每年的倒计时,同学们决定排出——“数字舞蹈 ...

随机推荐

  1. AngularJS中Directive指令系列

    近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用.  &, <, =, @ 符 ...

  2. Jmeter直连postgresql数据库进行压测

    关于Jmeter直连数据库进行压测,网上有好多教程了,pg数据库的相对少一些,今天自己测试了下,还是挺简单的,有个别需要注意的地方.相较于Loadrunner这么全面庞大的压测工具,Jmeter在数据 ...

  3. Python接口测试实战4(下) - 框架完善:用例基类,用例标签,重新运行上次失败用例

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

  4. Viper--方便好用的Golang 配置库

    前言 本文主要是为读者介绍一个轻便好用的Golang配置库viper 正文 viper 的功能   viper 支持以下功能:   1. 支持Yaml.Json. TOML.HCL 等格式的配置   ...

  5. redis 为什么快

    redis采用自己实现的事件分离器,效率比较高,内部采用非阻塞的执行方式,吞吐能力比较大. 不过,因为一般的内存操作都是简单存取操作,线程占用时间相对较短,主要问题在io上,因此,redis这种模型是 ...

  6. Professional Books

    Machine Learning:     Pattern Recognition and Machine Learning(PRML)    https://mqshen.gitbooks.io/p ...

  7. Fiber Network ZOJ 1967(Floyd+二进制状态压缩)

    Description Several startup companies have decided to build a better Internet, called the "Fibe ...

  8. Weighted Median

    For n elements x1, x2, ..., xn with positive integer weights w1, w2, ..., wn. The weighted median is ...

  9. Beta阶段中间产物

    空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...

  10. OpenCV学习笔记——图像平滑处理

    1.blur 归一化滤波器Blurs an image using the normalized box filter.C++: void blur(InputArray src, OutputArr ...