题意:设f(n) = c ^ (2n - 6) * f(n - 1) * f(n - 2) * f(n - 3), 问第n项是多少?

思路:官方题解:我们先转化一下,令g(x) =  c ^ x * f(x), 那么原式转化为了g(x) = g(x - 1) * g(x - 2) * g(x - 3)。之后我们可以考虑把f(1), f(2), f(3)和c的质因子找出来,枚举质因子对答案的贡献。我们发现,如果是质因子的数目的话,乘法就变成了加法(相当于统计质因子的指数),这样就可以用矩阵乘法优化了。注意,矩阵转移的时候,模数是1e9 + 6,因为转移的时候是指数(欧拉定理)。其实基于这种想法,我们可以不用处理质因子,直接计算g(1), g(2),g(3)对答案的贡献。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod = 1e9 + 7;
const LL mod1 = 1e9 + 6;
map<LL, LL> mp[4];
set<LL> s;
set<LL>::iterator it;
struct Matrix {
LL a[3][3]; void init(LL num = 0) {
memset(a, 0, sizeof(a));
for (int i = 0; i < 3; i++)
a[i][i] = num;
} Matrix operator * (const Matrix& now) const {
Matrix res;
res.init();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
res.a[i][j] = (res.a[i][j] + (a[i][k] * now.a[k][j]) % mod1) % mod1;
return res;
} Matrix operator ^ (const LL num) const {
Matrix ans, x = *this;
ans.init(1);
LL now = num;
for (; now; now >>= 1) {
if(now & 1) ans = ans * x;
x = x * x;
}
return ans;
} void print() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%lld ", a[i][j]);
}
printf("\n");
} }
};
void div(LL num, int pos) {
for (LL i = 2; i * i <= num; i++) {
if(num % i == 0) {
s.insert(i);
while(num % i == 0) {
mp[pos][i]++;
num /= i;
}
}
}
if(num > 1) {
s.insert(num);
mp[pos][num]++;
}
}
LL qpow(LL x, LL y) {
LL ans = 1;
for (; y; y >>= 1ll) {
if(y & 1ll) ans = (ans * x) % mod;
x = (x * x) % mod;
}
return ans;
}
LL a[4];
int main() {
LL n;
scanf("%lld", &n);
for (int i = 0; i < 4; i++) {
scanf("%lld", &a[i]);
div(a[i], i);
}
Matrix x, y, x1;
x.init();
x.a[0][2] = x.a[1][2] = x.a[2][2] = x.a[2][1] = x.a[1][0] = 1;
x = x ^ (n - 1);
LL ans = 1;
for (it = s.begin(); it != s.end(); it++) {
y.init();
for (int i = 0; i < 3; i++) {
y.a[0][i] = mp[i][*it];
}
for (int i = 0; i < 3; i++)
y.a[0][i] = (y.a[0][i] + ((LL)(i + 1) * mp[3][*it] % mod)) % mod;
y = y * x;
ans = (ans * qpow(*it, y.a[0][0]) % mod) % mod;
}
ans = ans * qpow(qpow(a[3], mod - 2), n) % mod;
printf("%lld\n", ans);
}

  

codeforces 1182E Product Oriented Recurrence 矩阵快速幂的更多相关文章

  1. CodeForces 1182E Product Oriented Recurrence

    题意 给定五个整数 \(n,f_1,f_2,f_3,c\),其中数列 \(f\) 满足以下递推式: \[f_x=c^{2x-6}f_{x-1}f_{x-2}f_{x-3} \] 求 \(f_n\). ...

  2. Educational Codeforces Round 60 D dp + 矩阵快速幂

    https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...

  3. Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)

    Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...

  4. codeforces 678D Iterated Linear Function 矩阵快速幂

    矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵  { g[n] }    =  {A   B}  * { g[n-1]} {   1   }         {0   1 ...

  5. Educational Codeforces Round 14E. Xor-sequences(矩阵快速幂)

    传送门 题意 给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足\(x_i与x_{i+1}\)异或的二进制表示中1的个数是3的倍数.问长度为k的满足条件的序列有多少种 ...

  6. Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...

  7. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  8. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  9. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

随机推荐

  1. 【异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters

    出现此异常原因是引文使用feign客户端的时候,参数没有用注解修饰 1.1GET方式错误写法 @RequestMapping(value="/test", method=Reque ...

  2. css3 序列帧动画抖动

    页面需要一个动画,设计师给了动画的序列帧 项目由vue构建,使用css3做动画 html <div class="work_two_main"></div> ...

  3. 数据结构---Java---Hastable

    1.概述 1.1 Hashtable是线程安全的: 1.2 源码 public class Hashtable<K,V> extends Dictionary<K,V> imp ...

  4. Centos MySQL 5.7安装、升级教程

    MySQL 5.7安装.升级笔记分享: 卸载当前的 MySQL 查看当前 MySQL 版本: ? 1 2 [root@coderknock ~]# mysql -V mysql Ver 14.14 D ...

  5. 企业实践 | 如何更好地使用 Apache Flink 解决数据计算问题?

    业务数据的指数级扩张,数据处理的速度可不能跟不上业务发展的步伐.基于 Flink 的数据平台构建.运用 Flink 解决业务场景中的具体问题等随着 Flink 被更广泛的应用于广告.金融风控.实时 B ...

  6. doT模板引擎

    doT模板引擎是一个比较高效的引擎,一直都在使用,只有3kb大小,简洁的语法,无任何依赖,简单易用:下面的代码直接拷贝引用就可以使用: 插件代码 (function(){function p(b,a, ...

  7. Linux系统之-常用命令及技巧

    一. 通用命令:1.date :print or set the system date and time2. stty -a: 可以查看或者打印控制字符(Ctrl-C, Ctrl-D, Ctrl-Z ...

  8. 埋在MySQL数据库应用中的17个关键问题

    出处:https://blog.csdn.net/weixin_42882439 MySQL的使用非常普遍,跟MySQL有关的话题也非常多,如性能优化.高可用性.强一致性.安全.备份.集群.横向扩展. ...

  9. (4)C++ 复合类型-指针

    篇幅长从 https://www.cnblogs.com/buchizaodian/p/11511256.html 提取出来 七.指针和自由存储空间  1.寻址运算符 * #include<io ...

  10. Autofac框架详解 转载https://www.cnblogs.com/lenmom/p/9081658.html

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...