[Matrix multiplication] [fast power] Jzoj P6275 small number of columns L

Description

 

answer

  • It is easy to think of matrix multiplication, matrix and then how to build it?
  •  a[i][K]=b[i],a[i][i-1]=(i!=1)
  • Each number is then independent view, respectively, do matrix multiplication can get k <= 30 points, but in fact, because of the same matrix, the matrix may be pretreated Power
  • Note that moment when the index is multiplied mod p-1

 

Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define ll long long
 5 using namespace std;
 6 const ll N=210,mo=998244353;
 7 ll n,k,ans,f[N];
 8 struct node { ll a[N][N]; }A;
 9 node mul(node x,node y)
10 {
11     node c;
12     for(ll i=1;i<=k;i++) for(ll j=1;j<=k;j++) c.a[i][j]=0;
13     for(ll i=1;i<=k;i++) for(ll j=1;j<=k;j++) for(ll p=1;p<=k;p++) (c.a[i][j]+=x.a[i][p]*y.a[p][j]%(mo-1))%=mo-1;
14     return c;
15 }
16 ll Ksm(ll a,ll b) 
17 {
18     if (!b) return 1;
19     if (!a) return 0;
20     b--; ll r=a; 
21     for (;b;b>>=1,a=a*a%mo) if (b&1) r=a*r%mo; return r; 
22 }
23 node ksm(node x,ll y) { y--; node r=x; for (;y;y>>=1,A=mul(A,A)) if (y&1) r=mul(r,A); return r; }
24 int main()
25 {
26     freopen("seq.in","r",stdin),freopen("seq.out","w",stdout),scanf("%lld%lld",&n,&k);
27     for (ll i=2;i<=k;i++) A.a[i][i-1]=1;
28     for (ll i=k;i>=1;i--) scanf("%lld",&A.a[i][k]);
29     for (ll i=1;i<=k;i++) scanf("%lld",&f[i]);
30     if (n<=k) { printf("%lld",f[n]); return 0;}
31     n-=k,A=ksm(A,n),ans=1;
32     for (ll i=1;i<=k;i++) ans=ans*Ksm(f[i],A.a[i][k])%mo;
33     printf("%lld",ans);
34 }

 

Guess you like

Origin www.cnblogs.com/Comfortable/p/11317990.html