Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:矩阵快速幂

Posted by liaoshuai at 2022-11-24 20:07:41 on Problem 3233
In Reply To:矩阵快速幂 Posted by:ACAccepted at 2019-07-22 09:59:55
> #include <cstdio>
> #include <串>
使用命名空间 std >;
>
>模板<类型名 T>
> 内联 T 读取()
> {
> T x=0,f=1;char c=getchar();
> while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
> while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
>返回 x*f;
> }
>
> const int MAXN=35;
> int n,k,mod;
>
>结构矩阵1
> {
> int n,m;
> 长 a[MAXN][MAXN];
>
> inline matrix1(){n=m=0;memset(a,0,sizeof(a));}
>
> inline void init(int n,int w)
> {
> this->n=n;this->m=n;
> for (int i=1;i<=n;i++) a[i][i]=w;
> }
>
> 内联无效打印()
> {
> for (int i=1;i<=n;i++)
> {
> for (int j=1;j<=m;j++) printf(“%d ”,a[i][j]);
> printf(“\n”);
> }
> }
>
> 内联结构矩阵 1 运算符 + (结构矩阵 1 TMP)
> {
> struct matrix1 ans;ans.n=tmp.n;ans.m=tmp.m;
> for (int i=1;i<=ans.n;i++)
> for (int j=1;j<=ans.m;j++)
> ans.a[i][j]=(a[i][j]+tmp.a[i][j])%mod;
>返回;
> }
>
>内联结构矩阵 1 运算符 * (结构矩阵 1 TMP)
> {
>结构矩阵1 ans;
> ans.n=n;ans.m=tmp.m;
> for (int i=1;i<=ans.n;i++)
> for (int j=1;j<=ans.m;j++)
> for (int k=1;k<=tmp.n;k++)
> ans.a[i][j]=(ans.a[i][j]+a[i][k]*tmp.a[k][j]%mod)%mod;
>返回;
> }
> }A;
>
>结构矩阵2
> {
> int n,m;
>结构矩阵1 a[3][3];
>
> inline matrix2(){n=m=0;}
>
> 内联结构矩阵 2 运算符 * (结构矩阵 2 TMP)
> {
>结构矩阵2 ans;
> ans.n=n;ans.m=tmp.m;
> for (int i=1;i<=ans.n;i++)
> for (int j=1;j<=ans.m;j++)
> for (int k=1;k<=tmp.n;k++)
> ans.a[i][j]=ans.a[i][j]+a[i][k]*tmp.a[k][j];
>返回;
> }
> }tmp,f;
>
> 内联结构矩阵 2 qpow(struct matrix2 a,int b)
> {
>结构矩阵2 ans;
> ans.m=ans.n=2;
> ans.a[1][1].init(n,1);ans.a[1][2].init(n,0);
> ans.a[2][1].init(n,0);ans.a[2][2].init(n,1);
> 同时 (b)
> {
> if (b&1) ans=ans*a;
> a=a*a;b>>=1;
> }
>返回;
> }
>
> int main()
> {
> n=read<int>();k=read<int>();mod=read<int>();
> A.n=n;上午=n;
> for (int i=1;i<=n;i++)
> for (int j=1;j<=n;j++)
> A.a[i][j]=read<int>();
> tmp.n=2;tmp.m=2;
> tmp.a[1][1]=A;tmp.a[1][2].init(n,1);
> tmp.a[2][1].init(n,0);tmp.a[2][2].init(n,1);
> f.n=2;f.m=1;
> f.a[1][1]=A;f.a[2][1]=A;
> tmp=qpow(tmp,k-1);
> f=tmp*f;
> f.a[1][1].print();
>返回 0;
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator