E. Product Oriented Recurrence (矩阵快速幂新模板)
E. Product Oriented Recurrence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Let fx=c2x−6⋅fx−1⋅fx−2⋅fx−3fx=c2x−6⋅fx−1⋅fx−2⋅fx−3 for x≥4x≥4.
You have given integers nn, f1f1, f2f2, f3f3, and cc. Find fnmod(109+7)fnmod(109+7).
Input
The only line contains five integers nn, f1f1, f2f2, f3f3, and cc (4≤n≤10184≤n≤1018, 1≤f11≤f1, f2f2, f3f3, c≤109c≤109).
Output
Print fnmod(109+7)fnmod(109+7).
Examples
input
Copy
5 1 2 5 3
output
Copy
72900
input
Copy
17 97 41 37 11
output
Copy
317451037
Note
In the first example, f4=90f4=90, f5=72900f5=72900.
In the second example, f17≈2.28×1029587f17≈2.28×1029587.
仔细观察可以发现
其实f(n) 可以拆成 c^x1+f1^x2+f2^x3+f4^x4
我们只需要用递推式来求出x1 x2 x3 x4即可
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=5;
const long long mod=1e9+6;
const long long mod1=1e9+7;
struct node
{long long a[maxn][maxn];
}pos;
node milt(node x,node y)
{node res;memset(res.a,0,sizeof(res.a));for(int i=0;i<maxn;i++)for(int j=0;j<maxn;j++)for(int k=0;k<maxn;k++)res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;return res;
}
node power(node mat,long long n)
{node x,y;memset(x.a,0,sizeof(x.a));memset(y.a,0,sizeof(y.a));y=mat;for(int i=0;i<maxn;i++) x.a[i][i]=1;while(n!=0){if(n%2==1) x=milt(x,y);y=milt(y,y);n/=2;}return x;
}
long long quick_pow(long long a,long long b)
{long long ans=1;a=a%mod1;while(b){if(b&1) ans=(ans*a)%mod1;b/=2;a=(a*a)%mod1;}return ans;
}
int main()
{long long n,f1,f2,f3,c;long long cex,f1ex,f2ex,f3ex;cin>>n>>f1>>f2>>f3>>c;n-=3;node tmp;memset(tmp.a,0,sizeof(tmp.a));tmp.a[0][0]=1;tmp.a[0][1]=1;tmp.a[0][2]=1;tmp.a[0][3]=2;tmp.a[0][4]=-6;tmp.a[1][0]=1;tmp.a[2][1]=1;tmp.a[3][3]=1;tmp.a[3][4]=1;tmp.a[4][4]=1;node ans=power(tmp,n);cex=(ans.a[0][3]*4+ans.a[0][4])%mod;memset(tmp.a,0,sizeof(tmp.a));tmp.a[0][0]=1;tmp.a[0][1]=1;tmp.a[0][2]=1;tmp.a[1][0]=1;tmp.a[2][1]=1;ans=power(tmp,n);f1ex=(ans.a[0][2])%mod;f2ex=(ans.a[0][1])%mod;f3ex=(ans.a[0][0])%mod;long long ans1;long long num;// cout<<f1ex<<" "<<f2ex<<" "<<f3ex<<endl;ans1=quick_pow(c,cex)%mod1;ans1=ans1*quick_pow(f1,f1ex)%mod1;ans1=ans1*quick_pow(f2,f2ex)%mod1;ans1=ans1*quick_pow(f3,f3ex)%mod1;printf("%lld\n",ans1);
}
E. Product Oriented Recurrence (矩阵快速幂新模板)相关推荐
- Codeforces 1182E Product Oriented Recurrence 矩阵快速幂
Product Oriented Recurrence 先化简原式子 c ^ x * f[x] = c ^ (x-1) * f[x-1] * c ^ (x-2) * f[x-2] * c ^ (x- ...
- codeforces 1182E Product Oriented Recurrence 矩阵快速幂
题意:设f(n) = c ^ (2n - 6) * f(n - 1) * f(n - 2) * f(n - 3), 问第n项是多少? 思路:官方题解:我们先转化一下,令g(x) = c ^ x * ...
- 【洛谷P3390】 矩阵快速幂(模板)
贴一下矩阵快速幂的模板 #include<iostream> #include<cstdio> #include<cstring> #include<stri ...
- POJ3070 Fibonacci【矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...
- hdu 2842 Chinese Rings 矩阵快速幂
分析: 后面的环能不能取下来与前面的环有关,前面的环不被后面的环所影响.所以先取最后面的环 设状态F(n)表示n个环全部取下来的最少步数 先取第n个环,就得使1~n-2个环属于被取下来的状态,第n-1 ...
- 又见斐波那契~矩阵快速幂入门题
链接:https://www.nowcoder.com/acm/contest/105/G 来源:牛客网 题目描述 这是一个加强版的斐波那契数列. 给定递推式 求F(n)的值,由于这个值可能太大,请对 ...
- 上海理工大学第二届“联想杯”全国程序设计邀请赛 - Little Witch Academia(矩阵快速幂)
题目链接:点击查看 题目大意:给出两种型号的瓷砖,尺寸分别是 a∗1a*1a∗1 和 b∗1b*1b∗1,现在需要填满 w∗hw*hw∗h 的矩阵,需要满足以下两个情况: 瓷砖不能旋转 相邻的两行中, ...
- HDU 2256(矩阵快速幂)
传送门 题面: Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 矩阵快速幂(51nod)
1113 矩阵快速幂 基准时间限制:3 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大, ...
最新文章
- 使用SpringBoot发送邮件 在本地测试是好的 放到服务器连接超时问题
- 自定义Dialog(一)
- 感谢有你们,架构师修行之路!
- (39)FPGA四种常用逻辑门(与非门)
- vivo手机通用的官方售后解锁工具包箱_可更换镜头拍照手机不远了!vivo IFEA分离式镜头获奖...
- PP点点通介绍与下载
- 苹果Mac删除windows后无法合并分区的简单解决方法
- 如何利用IDM加速下载百度网盘大文件
- origin 8.0 win 7 破解版安装及使用教程
- 用于将 InfoPath 2007 集成到 Visual Studio 2005 中的 InfoPath Designer API 概述
- -Cannot use v-for on stateful component root element because it renders multiple elements
- [数论+模板] 分解质因数(模板)
- 爬在NLP的大道上——Question Answering Infused Pre-training of General-Purpose Contextualized Representations
- VoLTE用户码号和卡
- php中文加密解密,php加密解密详解
- 应用程序中的服务器错误 怎么解决办法,iwms出现“/”应用程序中的服务器错误。解决办法...
- SEO上下线营销思维:二者兼备实现“落地”
- [机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
- 微信支付:小程序支付/扫码支付
- git更新pull不下来代码_git pull更新错误解决办法
热门文章
- [极客大挑战 2019]HardSQL
- 279#FLUENT精典案例-考虑地下水渗流作用下的地源热泵竖直双 U 地埋管群传热特性仿真
- cf #825 Div.2(A~C2)
- 咸鱼菌玩3D—123D创建桌子
- s110 raid linux,dell s110阵列卡驱动下载
- 华为u8860手机 android4.0 轻松root
- vue.js之非关系组件通信(八竿子打不着的关系组件通信)
- 给自己动力:一万小时定律。。简简单单的实现。。
- STM32CubeMX、keil、simulink联合开发MCU教程
- 玩转X-CTR100 l STM32F4 l PS2无线手柄