Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门
题目
fn=c2∗n−6fn−1fn−2fn−3\begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} fn=c2∗n−6fn−1fn−2fn−3
思路
我们通过迭代发现fnf_nfn其实就是由ct1,f1t2,f2t3,f3t4c^{t_1},f_1^{t_2},f_2^{t_3},f_3^{t_4}ct1,f1t2,f2t3,f3t4相乘得到,因此我们可以分别用矩阵快速幂求出t1,t2,t3,t4t_1,t_2,t_3,t_4t1,t2,t3,t4,最后用快速幂求得答案。
对于n<=3n<=3n<=3的我们直接输出即可,n>3n>3n>3的我们先将nnn减去333,然后进行求解。
对f1,f2,f3f_1,f_2,f_3f1,f2,f3的指数,我们可以推出xn=xn−1+xn−2+xn−3x_n=x_{n-1}+x_{n-2}+x_{n-3}xn=xn−1+xn−2+xn−3:
(xnxn−1xn−2)=(xn−1xn−2xn−3)[110101100]\begin{aligned} (x_n&&x_{n-1}&&x_{n-2})=(x_{n-1}&&x_{n-2}&&x_{n-3}) \left[ \begin{matrix} 1 & 1 & 0\\ 1 & 0 & 1\\ 1 & 0 & 0\\ \end{matrix} \right] \end{aligned} (xnxn−1xn−2)=(xn−1xn−2xn−3)⎣⎡111100010⎦⎤
对ccc的指数,我们可以推出xn=xn−1+xn−2+xn−3+2n=xn−1+xn−2+xn−3+2(n−1)+2x_n=x_{n-1}+x_{n-2}+x_{n-3}+2n=x_{n-1}+x_{n-2}+x_{n-3}+2(n-1)+2xn=xn−1+xn−2+xn−3+2n=xn−1+xn−2+xn−3+2(n−1)+2:
(xnxn−1xn−2n1)=(xn−1xn−2xn−3n−11)[1100010100100002001020011]\begin{aligned} (x_n&&x_{n-1}&&x_{n-2}&&n&&1)=(x_{n-1}&&x_{n-2}&&x_{n-3}&&n-1&&1) \left[ \begin{matrix} 1 & 1 & 0 & 0 & 0\\ 1 & 0 & 1 & 0 & 0\\ 1 & 0 & 0 & 0 & 0\\ 2 & 0 & 0 & 1 & 0\\ 2 & 0 & 0 & 1 & 1\\ \end{matrix} \right] \end{aligned} (xnxn−1xn−2n1)=(xn−1xn−2xn−3n−11)⎣⎢⎢⎢⎢⎡1112210000010000001100001⎦⎥⎥⎥⎥⎤
注意,由于我们处理出来的x1,x2,x3,x4x_1,x_2,x_3,x_4x1,x2,x3,x4都是指数部分,这里如果膜1e9+71e9+71e9+7的话是不对的,我们还需要对其进行欧拉降幂。
代码实现
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 2e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;int f[10], a[10][10];void mulself(int a[10][10]) {int c[10][10];memset(c, 0, sizeof(c));for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {for(int k = 0; k < 3; k++) {c[i][j] = (c[i][j] + (long long)a[i][k] * a[k][j] % (mod - 1)) % (mod - 1);}}}memcpy(a, c, sizeof(c));
}void mul(int f[10], int a[10][10]) {int c[10];memset(c, 0, sizeof(c));for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {c[i] = (c[i] + (long long)f[j] * a[j][i] % (mod - 1)) % (mod - 1);}}memcpy(f, c, sizeof(c));
}void mulself1(int a[10][10]) {int c[10][10];memset(c, 0, sizeof(c));for(int i = 0; i < 5; i++) {for(int j = 0; j < 5; j++) {for(int k = 0; k < 5; k++) {c[i][j] = (c[i][j] + (long long)a[i][k] * a[k][j] % (mod - 1)) % (mod - 1);}}}memcpy(a, c, sizeof(c));
}void mul1(int f[10], int a[10][10]) {int c[10];memset(c, 0, sizeof(c));for(int i = 0; i < 5; i++) {for(int j = 0; j < 5; j++) {c[i] = (c[i] + (long long)f[j] * a[j][i] % (mod - 1)) % (mod - 1);}}memcpy(f, c, sizeof(c));
}int qpow(int x, int n) {int res = 1;while(n) {if(n & 1) res = 1LL * res * x % mod;x = 1LL * x * x % mod;n >>= 1;}return res;
}LL n;
int f1, f2, f3, c;int main(){scanf("%lld%d%d%d%d", &n, &f1, &f2, &f3, &c);if(n == 1) return printf("%d\n", f1) * 0;if(n == 2) return printf("%d\n", f2) * 0;if(n == 3) return printf("%d\n", f3) * 0;n -= 3;LL ans = 1;f[0] = 1, f[1] = 0, f[2] = 0;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;LL x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f3, f[0]) % mod;f[0] = 0, f[1] = 1, f[2] = 0;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f2, f[0]) % mod;f[0] = 0, f[1] = 0, f[2] = 1;a[0][0] = 1, a[0][1] = 1, a[0][2] = 0;a[1][0] = 1, a[1][1] = 0, a[1][2] = 1;a[2][0] = 1, a[2][1] = 0, a[2][2] = 0;x = n;while(x) {if(x & 1) mul(f, a);mulself(a);x >>= 1;}ans = ans * qpow(f1, f[0]) % mod;if(n == 1) f[0] = 2;if(n == 2) f[0] = 6;if(n == 3) f[0] = 14;if(n > 3) {n -= 3;f[0] = 14, f[1] = 6, f[2] = 2, f[3] = 3, f[4] = 1;memset(a, 0, sizeof(a));a[0][0] = a[0][1] = 1;a[1][0] = a[1][2] = 1;a[2][0] = 1;a[3][0] = 2, a[3][3] = 1;a[4][0] = 2, a[4][3] = a[4][4] = 1;while(n) {if(n & 1) mul1(f, a);mulself1(a);n >>= 1;}}ans = ans * qpow(c, f[0]) % mod;printf("%lld\n", ans);return 0;
}
Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)相关推荐
- Codeforces Round #566 (Div. 2)-E. Product Oriented Recurrence
地址:https://codeforces.com/contest/1182/problem/E 思路:矩阵快速幂 fn=c^(2n−6)⋅f(n−1)⋅f(n−2)⋅f(n−3)=c^sc * f1 ...
- 【Codeforces Gym - 101635C Macarons 】【矩阵快速幂+状压】【dfs时间换空间】
[链接] http://codeforces.com/gym/101635/attachments [题意] 求用1*1,1*2的方格填n*m的矩阵的方法数 [知识点] 状压dfs+矩阵快速幂 [分析 ...
- E. Product Oriented Recurrence(codeforces R566 div2)
矩阵快速幂+欧拉降幂 思路: #include<bits/stdc++.h> using namespace std;typedef long long ll;ll mod=1e9+7;l ...
- CodeForces 392C Yet Another Number Sequence 矩阵快速幂
题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...
- Codeforces 1182E Product Oriented Recurrence 矩阵快速幂
Product Oriented Recurrence 先化简原式子 c ^ x * f[x] = c ^ (x-1) * f[x-1] * c ^ (x-2) * f[x-2] * c ^ (x- ...
- Codeforces Round #716 (Div. 2)
Codeforces Round #716 (Div. 2) CodeForces 1514 题号 题目 知识点 难度 A Perfectly Imperfect Array B AND 0, Sum ...
- Codeforces Round #506 (Div. 3)
Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...
最新文章
- CSS类命名的语义化VS结构化方式
- 分布式离线计算—Spark—SparkStreaming
- 设计模式中类之间的关系
- 外点惩处函数法·约束优化问题
- 前端开发知识点解答-HTML-面试
- linux 什么数据类型 8字节,linuxea:go数值类型(8)
- Smoke Test Ad hoc Test
- ad13批量安装元件库_常用的Altium Designer AD09 AD14 AD18元件库 原理图库(543个)+PCB封装库(509个)...
- 【微信支付】小案例,Java版
- 如何将两个PDF合并成一个?PDF合并方法
- 计算机房电缆,机房设计常用计算公式大全
- 微信头像 尺寸 php,怎么把照片缩小做微信头像
- hdu1429推箱子
- 阿里cdn请求原理以及缓存机制
- clip_gradient_norms()
- 陈丹青版画作品首次元宇宙拍卖明日揭幕!
- 学习hutool源码TreeUtil.build()得到了什么
- 企业微信oauth认证_微信企业号OAuth2验证接口实例(使用SpringMVC)
- DTMF通信系统设计—基于MATLAB和STM32
- 每日一学:Python 将时间戳转换为指定格式日期
热门文章
- 蓝桥杯学习记录||ALGO-1004 无聊的逗
- 数据分析的步骤都有什么?
- html使table表头不动,html怎么实现表头不动
- 配置路由器,两个PC通过路由器通信
- Vector,和vector的应用
- 通过省份和城市获取车籍地(车牌号码前2位)
- 【ICNP2020】6Fit-A-Part: A Protocol for Physical Distancing on a Custom Wearable Device论文解读
- python和plc哪个难_PLC是学西门子的好还是学三菱的?
- 石墨烯是一种由碳原子紧密堆积构成的二维晶体,你了解它的特性和应用吗?
- 机器学习之模型训练(二)皮马印第安人糖尿病数据集