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 ...
随机推荐
- codeforces educational round25
A #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ; string s; c ...
- assert.ifError()
assert.ifError(value) 如果 value 为真,则抛出 value. 可用于测试回调函数的 error 参数(通俗解释ifError方法断定某个表达式是否false,如果该表达式对 ...
- 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 ...
- JS中双击和单击事件冲突解决
在JS中代码中同一功能块中通常同时会用到单击.双击事件,但通常会遇到一个问题,就是在双击的时候即执行了一次双击事件,而且还执行了两次单击事件.此类冲突在ZTree.DHTMLX中经常遇到. 想要解决两 ...
- HN669打包工具--调试文档
调试有两种方式,一是直接在游戏工程上面调试,这比较麻烦,需要根据插件配置文件和脚本文件去配置好工程选项后,才能调试.简单一点就是通过脚本文件打包后会有生成游戏工程对应每个渠道的工程. 如下图:这个工程 ...
- 定时处理组件---Quartz.net
1.认识任务调度 所谓任务调度,就是以将业务区块任务化(即抽象成每一个独立的任务,执行每个任务便完成某种业务的需求).比如,我们有一个订单系统,现在有这样的一个需求,就是需要在某一时间点去扫描数据库, ...
- Lxc的cgroup技术
你将学到什么 什么是cgroup 如何使用cgroup Cgroup简介 CGroup是Control Groups的缩写,是Linux内核提供的一种可以限制.记录.隔离进程组所使用的硬件资源的机制. ...
- UPC11073(DP,思维)
#include<bits/stdc++.h>using namespace std;long long dp[507][507];const long long mod = 998244 ...
- 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)
#include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...
- poj2763(lca / RMQ + 线段树)
题目链接: http://poj.org/problem?id=2763 题意: 第一行输入 n, q, s 分别为树的顶点个数, 询问/修改个数, 初始位置. 接下来 n - 1 行形如 x, y, ...