传送门

题目

\[\begin{aligned}
&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}\):

\[\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}
\]

对\(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\):

\[\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}
\]

注意,由于我们处理出来的\(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;
}

最新文章

  1. [HTML5] ArrayBuffer与类型化数组
  2. Java生成和操作Excel文件(转载)
  3. 用Python写爬虫爬取58同城二手交易数据
  4. 实现手机扫描二维码页面登录,类似web微信-第一篇,业务分析
  5. OpenStack 物理资源问题
  6. vue通过判断写样式(v-bind)
  7. c# 函数及out传值
  8. JavaScript 随机数函数
  9. K-近邻算法python实现
  10. Good Vim plugin for python [Vim python mode]
  11. Django 入门案例开发(中)
  12. 详细的&lt;select&gt;下拉列表详解
  13. Java中三种代理模式
  14. leetcode数据库题目及答案汇总
  15. Python网络编程Socket之协程
  16. jdbcTemplate 调用存储过程。 入参 array 返回 cursor
  17. 蛋疼的mysql_ping()以及MYSQL_OPT_RECONNECT
  18. Python3 tkinter基础 Label pack 设置控件在窗体中的位置
  19. shell 通配符
  20. Linux下高并发socket最大连接数所受的各种限制(转)

热门文章

  1. myeclipse的ctrl+f搜索面板功能详解
  2. sed取某域名对应的IP
  3. 最近公共祖先 LCA 递归非递归
  4. MySQL高级 之 order by、group by 优化
  5. OpenLayers加载高德地图离线瓦片地图
  6. Data-Structure-Notes
  7. windwos源码安装mysql
  8. Navicat Premium 12 安装与破解,Navicat Premium通用的数据库管理工具
  9. nginx+lua+storm的热点缓存的流量分发策略自动降级
  10. 解决本地Bootstrap字体图标不可见的问题