codeforces 1182E Product Oriented Recurrence 矩阵快速幂
题意:设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 矩阵快速幂的更多相关文章
- 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\). ...
- Educational Codeforces Round 60 D dp + 矩阵快速幂
https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...
- Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)
Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...
- codeforces 678D Iterated Linear Function 矩阵快速幂
矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵 { g[n] } = {A B} * { g[n-1]} { 1 } {0 1 ...
- Educational Codeforces Round 14E. Xor-sequences(矩阵快速幂)
传送门 题意 给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足\(x_i与x_{i+1}\)异或的二进制表示中1的个数是3的倍数.问长度为k的满足条件的序列有多少种 ...
- Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...
- 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} \] 思路 我们通过迭代发 ...
- Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
随机推荐
- 【知识强化】第五章 传输层 5.2 UDP协议
这节课我们来学习一下UDP协议. 那在上节课呢我们学了这样一个打油诗. 啊,就是传输层有两个好兄弟,大哥TCP和二弟UDP.大哥很靠谱,二弟不靠谱.那只要说到UDP协议我们就要知道它的一个重要的特点, ...
- 安装双系统Windows+Centos安装完成之后提示双系统选项菜单!
原因:在windows下安装centos系统完成之后重启无法显示windows系统菜单选项 1.安装Windows系统 2.安装Centos系统 3.在Centos系统中安装ntfs-3g yum i ...
- 绘图matplotlib
前言 matplotlib是python的一个绘图库,如果你没有绘制过图,可以先试试js的绘图库http://www.runoob.com/highcharts/highcharts-line-lab ...
- 力扣——gas station (加油站) python实现
题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...
- jquey弹出框demo
默认 $('#btn-01').click(function(){ $.dialog({ contentHtml : '<p>我是默认弹出对话框示例展示.我只是用来占位的内容展示,仅仅用来 ...
- sql 实现分页+分组并取出分组内的前n条数据
一.建表 if exists (select * from sysobjects where id = OBJECT_ID('[test]') and OBJECTPROPERTY(id, 'IsUs ...
- 如果全球的沙子都对你发起DDoS攻击,如何破?
IPv6已来 2016年6月1日开始,苹果规定所有提交至AppStore的应用必须兼容IPv6-only标准.可以预计,2018年底会有大量互联网资源.上网用户使用IPv6协议.这意味着,如果一个互联 ...
- Linux 应用程序编程基础
一个计算机应用程序在内存中可以分成两个部分:存放代码的代码段和存放数据的数据段.代码段存放用户编写的代码;数据段存放栈和堆. 相关内存管理函数 #include <stdlib.h> vo ...
- SCP-bzoj-1058
项目编号:bzoj-1058 项目等级:Safe 项目描述: 戳这里 特殊收容措施: STL好题.维护两个set,一个存储数列里相邻元素差,另一个存储整个数列. 对于MIN_SORT_GAP操作,维护 ...
- C#排列组合类,写彩票算法的朋友们可以来看一看
public class PermutationAndCombination<T> { /// <summary> /// 交换两个变量 ...