Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
题目
&f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\
\end{aligned}
\]
思路
我们通过迭代发现\(f_n\)其实就是由\(c^{t_1},f_1^{t_2},f_2^{t_3},f_3^{t_4}\)相乘得到,因此我们可以分别用矩阵快速幂求出\(t_1,t_2,t_3,t_4\),最后用快速幂求得答案。
对于\(n<=3\)的我们直接输出即可,\(n>3\)的我们先将\(n\)减去\(3\),然后进行求解。
对\(f_1,f_2,f_3\)的指数,我们可以推出\(x_n=x_{n-1}+x_{n-2}+x_{n-3}\):
(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}
\]
对\(c\)的指数,我们可以推出\(x_n=x_{n-1}+x_{n-2}+x_{n-3}+2n=x_{n-1}+x_{n-2}+x_{n-3}+2(n-1)+2\):
(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}
\]
注意,由于我们处理出来的\(x_1,x_2,x_3,x_4\)都是指数部分,这里如果膜\(1e9+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 #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛
D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...
- Codeforces Round #566 (Div. 2)
Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...
- Codeforces Round #566 (Div. 2)题解
时间\(9.05\)好评 A Filling Shapes 宽度为\(3\),不能横向填 考虑纵向填,长度为\(2\)为一块,填法有两种 如果长度为奇数则显然无解,否则\(2^{n/2}\) B Pl ...
- Codeforces Round #566 (Div. 2) C. Beautiful Lyrics
链接: https://codeforces.com/contest/1182/problem/C 题意: You are given n words, each of which consists ...
- Codeforces Round #566 (Div. 2) B. Plus from Picture
链接: https://codeforces.com/contest/1182/problem/B 题意: You have a given picture with size w×h. Determ ...
- Codeforces Round #566 (Div. 2) A. Filling Shapes
链接: https://codeforces.com/contest/1182/problem/A 题意: You have a given integer n. Find the number of ...
随机推荐
- commitlint那些事儿
这里主要介绍提交信息用到的 cz 工具集. 一.生成器 commitizen,cz`生成提交说明`,格式化 git commit message. # 全局安装cz npm install -g co ...
- TELNET可以连通但无法创建数据库连接(Oracle)
问题描述: 近期客户方进行了网络调整,申请A服务器的1521端口开通后,telnet可以访问,但是SQLPLUS.PLSQL等工具一直无法创建相应连接,提示连接超时. 问题排查: 对开放端口的所有服务 ...
- win7下IntelliJ IDEA使用curl
curl是利用URL语法在命令行方式下工作的开源文件传输工具 curl命令可以在开发web应用时,模拟前端发起的HTTP请求 1.下载curl https://curl.haxx.se/downloa ...
- 通Shell获取Tomcat进程号并杀死进程
#!/bin/bash echo "begin get tomcat8 pid" tomcat8_id=$( | grep -v grep | awk '{print $2}') ...
- centos个性化命令行提示符
为了在满屏的命令中找到用户的命令行,所以很有必要设置一种字体颜色.我就设置最实用的一种,可以用蓝色字体显示当前所在路径 命令行输入: echo "PS1='[\${debian_chroot ...
- 二叉树根结点到任意结点的路径(C语言)
有一棵二叉树,如下图所示: 其中 # 表示空结点. 先序遍历:A B D E G C F 问题:怎么得到从根结点到任意结点的路径呢? 示例:输入 G,怎么得到从结点 A 到结点 G 的路径呢? 很明显 ...
- golang中,map作为函数参数是如何传递的
当你声明一个map的时候: m := make(map[int]int) 编译器会调用 runtime.makemap: // makemap implements a Go map creation ...
- S02_CH12_ AXI_Lite 总线详解
S02_CH12_ AXI_Lite 总线详解 12.1前言 ZYNQ拥有ARM+FPGA这个神奇的架构,那么ARM和FPGA究竟是如何进行通信的呢?本章通过剖析AXI总线源码,来一探其中的秘密. 1 ...
- 「UR#6」懒癌
「UR#6」懒癌 妈妈我居然看了六个小时题解,快救救乌干达的可怜儿童吧. 接下来开始膜官方题解: 其实就算有上面两个结论也不是很好想到任意复杂度的做法,关键在于要想到一个人是怎么推断自己的狗是不是 ...
- 创建maven父项目以及子项目
创建maven父项目以及子项目(Eclipse创建Maven Project跟Maven Module)https://blog.csdn.net/Mrsanger/article/details/8 ...