CF1182E Product Oriented Recurrence
思路:
fn = can * f1xn * f2yn * f3zn, 首先dp计算指数部分an = an-1 + an-2 + an-3 + 2 * n - 6, 而an-1 = an-2 + an-3 + an-4 + 2 * n - 8,相减可以得到an = 2 * an-1 - an-4 + 2。xn,yn和zn是普通的三阶斐波那契。计算完指数部分要对p - 1取模(由费马小定理知p为质数的情况ap - 1 % p = 1),然后再用快速幂计算各个部分相乘即可。
再记录一种两个互相交互的递推式的矩阵快速幂构造:

实现:
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef vector<vector<ll>> matrix; const ll mod = 1e9 + , p_mod = mod - ; matrix mat_mul(matrix & a, matrix & b)
{
matrix c(a.size(), vector<ll>(b[].size()));
for (int i = ; i < a.size(); i++)
{
for (int k = ; k < a[].size(); k++)
{
for (int j = ; j < b[].size(); j++)
{
c[i][j] = ((c[i][j] + a[i][k] * b[k][j] % p_mod) + p_mod) % p_mod;
}
}
}
return c;
} matrix mat_pow(matrix & a, ll n)
{
matrix res(a.size(), vector<ll>(a[].size()));
for (int i = ; i < a.size(); i++) res[i][i] = ;
while (n > )
{
if (n & ) res = mat_mul(res, a);
a = mat_mul(a, a);
n >>= ;
}
return res;
} ll pow(ll x, ll n)
{
ll res = ;
while (n > )
{
if (n & ) res = res * x % mod;
x = x * x % mod;
n >>= ;
}
return res;
} int main()
{
ll n, f1, f2, f3, c;
while (cin >> n >> f1 >> f2 >> f3 >> c)
{
matrix x(, vector<ll>(, )), a(, vector<ll>(, ));
x[][] = ; x[][] = -; x[][] = ;
x[][] = x[][] = x[][] = x[][] = ;
a[][] = ; a[][] = ;
matrix c_p = mat_pow(x, n - );
c_p = mat_mul(c_p, a);
matrix y(, vector<ll>(, )), b1(, vector<ll>(, )), b2(, vector<ll>(, )), b3(, vector<ll>(, ));
y[][] = y[][] = y[][] = y[][] = y[][] = ;
b1[][] = b2[][] = b3[][] = ;
matrix t = mat_pow(y, n - );
matrix f1_p = mat_mul(t, b1);
matrix f2_p = mat_mul(t, b2);
matrix f3_p = mat_mul(t, b3);
ll ans = ;
ans = ans * pow(c, c_p[][]) % mod;
ans = ans * pow(f1, f1_p[][]) % mod;
ans = ans * pow(f2, f2_p[][]) % mod;
ans = ans * pow(f3, f3_p[][]) % mod;
cout << ans << endl;
}
return ;
}
CF1182E Product Oriented Recurrence的更多相关文章
- cf 1182 E - Product Oriented Recurrence
当时脑残了, 不会写矩阵快速幂中更改的系数, 其实把他扔到矩阵里同时递推就好了 #include<cstdio> #include<algorithm> #include< ...
- 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} \] 思路 我们通过迭代发 ...
- codeforces 1182E Product Oriented Recurrence 矩阵快速幂
题意:设f(n) = c ^ (2n - 6) * f(n - 1) * f(n - 2) * f(n - 3), 问第n项是多少? 思路:官方题解:我们先转化一下,令g(x) = c ^ x * ...
- 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\). ...
- 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 ...
- Into concurrent LRU caching once again
But this time, with a more product oriented point of view, instead of researching. http://openmymind ...
- Face recognition using Histograms of Oriented Gradients
Face recognition using Histograms of Oriented Gradients 这篇论文的主要内容是将Hog算子应用到人脸识别上. 转载请注明:http://blog. ...
- Goal Oriented Action Planning for a Smarter AI
Goal Oriented Action Planning for a Smarter AI by Brent Owens23 Apr 2014 Goal Oriented Action Planni ...
随机推荐
- 关于SCANF 与 GETS(C语言)
SCANF遇到空格会自动停止录入字符串,而GETS不会,GETS可以用于输入带空格的字符串
- [hdu4734]F(x)数位dp
题意:求0~f(b)中,有几个小于等于 f(a)的. 解题关键:数位dp #include<bits/stdc++.h> using namespace std; typedef long ...
- VMware 虚拟机 Ubuntu 系统没有IP地址 解决:UP BROADCAST MULTICAST 问题
VMware 虚拟机 ifconfig没有net_addr地址的解决方法 使用时间长的虚拟机,会莫名其妙的连接不上网 在终端中,使用ifconfig命令查看Ubuntu系统的IP地址,发现没有分配IP ...
- R语言对矩阵按某一列排序
[plain] view plaincopy a <- c(5,4,3,2,1) b <- c(1,2,3,4,5) c <- cbind(a,b) [plain] view pla ...
- sql获取当日减去几天的几天前日期
CONVERT(varchar(10),DATEADD(DAY, -220 ,CONVERT(nvarchar(10),getdate(),23)),23)
- Syslog+Fluentd+InfluxDB日志收集系统搭建
环境配置 节点 配置 类型 操作系统 Sched 2G 2CPU 50GB ens3=>192.168.200.11 KVM虚拟机 CentOS 7 Nova 4G 2CPU 50GB ens3 ...
- SCUT - 223 - Maya - 构造
https://scut.online/p/223 给定两个数N,M,构造M个在[0,80000]以内的互不相同的数使之异或和为N. 首先特判一下M<=2的两个简单情况,还有坑爹的-1! 然后想 ...
- validate验证注册表单
点击预览; <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- MVVM与Controller瘦身实践
MVC是一个做iOS开发都知道的设计模式,也是Apple官方推荐的设计模式.实际上,Cocoa Touch就是按照MVC来设计的. 这里,我们先不讲MVC是什么,我们先来谈谈软件设计的一些原则或者说理 ...
- Git 撤销中间某次的提交记录
今天遇到一个问题,一周前一位同事把非发布代码合并到发布分支上并已发布线上,在这个提交点后已经有了很多次的提交记录,所以不能直接回滚,使用到了gIt提供的revert.以此记录. git revert ...