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 ...
随机推荐
- makefile那些事儿
一.好处 自动化编译,一条make命令,整个工程可以完全自动编译,make命令是构建大型项目的首选方案. makefile就像一个shell脚本一样,用来定义规则,一个名称包含一条或多条命令,在终端m ...
- SonarQube - 常用配置与操作
1 - SonarQube服务器中的数据库配置 2019年4月10号,SonarQube发文称在7.9之后,所有的SonarQube的版本(CE.DE.EE和DCE)中将停止对MySQL的支持. 建议 ...
- 【C/C++开发】C++11:左值引用VS右值引用
左值引用VS右值引用 左值引用对于一般的C++程序员再熟悉不过,但对于右值引用(C++0X新特性),就稍微有点不知所云 左值VS右值 在定义变量的时候,经常会用到左值和右值,比如:int a = 1; ...
- 自动问答最新研究成果展示(SQuAD)
地址:https://rajpurkar.github.io/SQuAD-explorer/ Stanford Question Answering Dataset (SQuAD) is a read ...
- (转)Intellij Idea工具栏添加打开选中文件的资源管理器位置
背景:在idea的view>toolbar上面添加工具按钮,能够简化操作,现在添加打开资源管理按钮,后续功能待研究 Intellij Idea工具栏添加打开选中文件的资源管理器位置 工具栏-右击 ...
- keystone源码阅读--python函数
按照setup.sfg文件中[entry_poubts]中的声明前后阅读: 1.cmd.manage:main os.path.join(path,name):连接目录与文件名或目录os.path.e ...
- .NET Core 之 Nancy 基本使用
Nancy简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能 ...
- Intellij IDEA debug断点调试技巧与总结详解篇
1. Rerun . 这个就是结束debug模式,直接以run的方式重新跑某个程序.2. 直接跑完. 到下一个断点停下. 没有就直接跑完程序.3. 停止项目或者程序.要是自己的main呢. 点一下就停 ...
- poj1458公共子序列 C语言
/*Common SubsequenceTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 56416 Accepted: 23516D ...
- 迪杰斯特拉算法(Dijkstra) (基础dij+堆优化) BY:优少
首先来一段百度百科压压惊... 迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最 ...