[矩阵乘法] CF 1182 E.Product Oriented Recurrence

Fn = c2n-6 * Fn-1 * Fn-2 * Fn-3,求 F.

推一下式子,变成 F* c= ( Fn-1 * cn-1 ) * ( Fn-2 * cn-2 ) * ( Fn-3 * cn-3 )

记 Tn = Fn * cn,就有 Tn = Tn-1 * Tn-2 * Tn-3 .

再推一下,Tn = Tn-22 * Tn-32 * Tn-4

发现数列的指数是可以 ( a , b , c )   ----- >   ( b+a , c+a , a ) 递推的,

这个显然是可以矩阵乘法优化的,式子很容易推就不放了。

要注意的是矩阵乘法求出来的是指数,所以中间过程是对 p - 1 取模,

因为 at mod p = at mod (p-1) mod p,p为质数,a 和 p 互质 .

 1 #include<bits/stdc++.h>
 2 #define rep(i,a,b) for(register int i=a;i<=b;++i)
 3 #define rpd(i,a,b) for(register int i=a;i>=b;--i)
 4 #define rep1(i,x) for(register int i=head[x];i;i=nxt[i])
 5 typedef long long ll;
 6 const int N=10+5;
 7 const ll Mod=1000000007LL;
 8 using namespace std;
 9 inline int read(){
10     int x=0,f=1;char ch=getchar();
11     while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();}
12     while(isdigit(ch)){x=x*10+ch-‘0‘;ch=getchar();}
13     return x*f;
14 }
15 ll n,f1,f2,f3,c0;ll a[N][N],f[N][N],ans[N][N],a1[N][N],b1[N][N],anss[N][N];
16 void work(ll a[N][N],ll b[N][N],ll c[N][N],int x,int y){
17     rep(i,1,x)rep(j,1,3)a1[i][j]=a[i][j];
18     rep(i,1,y)rep(j,1,3)b1[i][j]=b[i][j];
19     rep(i,1,x)rep(j,1,y){
20         ll sum=0;
21         rep(t,1,3)(sum+=(a1[i][t]*b1[t][j])%(Mod-1LL))%=(Mod-1LL);
22         c[i][j]=sum;
23     }
24 }
25 void mul(ll now){
26     while(now){
27         if(now&1)work(a,f,f,3,3);
28         work(a,a,a,3,3);
29         now>>=1;
30     }
31 }
32 ll pow(ll x,ll k){
33     if(k==0)return 1LL;
34     if(k==1)return x;
35     ll t=pow(x,k/2LL);t*=t;t%=Mod;
36     if(k&1)t*=x,t%=Mod;
37     return t;
38 }
39 int main(){
40     cin>>n>>f1>>f2>>f3>>c0;
41     memset(f,0,sizeof(f));
42     rep(i,1,3)f[i][i]=1LL;
43     a[1][1]=1LL;a[1][2]=1LL;a[1][3]=1LL;
44     a[2][1]=1LL;a[2][2]=0;a[2][3]=0;
45     a[3][1]=0;a[3][2]=1LL;a[3][3]=0;
46     mul(n-4LL);
47     ans[1][1]=ans[1][2]=ans[1][3]=1LL;
48     memset(anss,0,sizeof(anss));
49     work(ans,f,anss,1,3);
50     rep(i,1,3)(f3*=c0)%=Mod;
51     rep(i,1,2)(f2*=c0)%=Mod;
52     rep(i,1,1)(f1*=c0)%=Mod;
53     ll now=1LL;(now*=pow(f3,anss[1][1]))%=Mod;
54     (now*=pow(f2,anss[1][2]))%=Mod;
55     (now*=pow(f1,anss[1][3]))%=Mod;
56     ll nows=pow(c0,n);nows=pow(nows,Mod-2LL);
57     (now*=nows)%=Mod;
58     printf("%lld\n",now);
59     //system("pause");
60     return 0;
61 }

原文地址:https://www.cnblogs.com/maximumhanyu/p/11428440.html

时间: 2024-08-04 18:28:11

[矩阵乘法] CF 1182 E.Product Oriented Recurrence的相关文章

E. Product Oriented Recurrence(矩阵快速幂+欧拉降幂)

题目链接: https://codeforces.com/contest/1182/problem/E 题目大意:  f(x)=c^(2x−6)⋅f(x−1)⋅f(x−2)⋅f(x−3)    for x≥4x≥4. 给你f1,f2,f3,n,c.求第n项的结果. 具体思路: 看到递推式想到用矩阵快速幂优化:但是如果都是乘法的话,是无法化成矩阵相乘的形式的.然后就开始想怎么将乘法转换成加法.推了一个多小时也没有推出来.. 具体的解题过程如下: 对于每一项,这一项里面包含的f1 和 f2 和 f3

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} \] 思路 我们通过迭代发现\(f_n\)其实就是由\(c^{x_1},f_1^{x_2},f_2^{x_3},f_3^{x_4}\)相乘得到,因此我们可以分别用矩阵快速幂求出\(x_1,x_2,x_3,x_4\),最后用快速幂求得答案. 对\(f_1,f_2,f_3\): \[ \begin{aligned} (x_n&&

矩阵乘法&lt;简单总结&gt;

原理:矩阵相乘最重要的方法是一般矩阵乘积.它只有在第一个矩阵的 行数 和第二个矩阵的 列数 相同时才可进行.若A为m×n矩阵,B为n×p矩阵,则他们的乘积AB会是一个m×p矩阵. 若A=    a    b    c        d    e    f        g    h    i    B=    A    D        B    E        C    F    A*B=CC=    aA+bB+cC    aD+bE+cF        dA+eB+fC    dD+eE

hdu 4920 Matrix multiplication (矩阵乘法)

Problem Description Given two matrices A and B of size n×n, find the product of them.bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests:The first line contains n (1≤n≤

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

使用blas做矩阵乘法

#define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <stdlib.h> #include <cublas_v2.h> #include <iostream> #include <vector> //extern "C" //{ #include <cblas.h> //} using namespace std; in

学习心得:《十个利用矩阵乘法解决的经典题目》from Matrix67

本文来自:http://www.matrix67.com/blog/archives/tag/poj大牛的博文学习学习 节选如下部分:矩阵乘法的两个重要性质:一,矩阵乘法不满足交换律:二,矩阵乘法满足结合律经典题目1 给定n个点,m个操作,构造O(m+n)的算法输出m个操作后各点的位置.操作有平移.缩放.翻转和旋转    这 里的操作是对所有点同时进行的.其中翻转是以坐标轴为对称轴进行翻转(两种情况),旋转则以原点为中心.如果对每个点分别进行模拟,那么m个操作总共耗时 O(mn).利用矩阵乘法可

hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 225 Problem Description Given two matrices A and B of size n×n, find the product o