Topic background
No background
I can not write a qwq
Title Description
Chino given n n number A_1 ... A_N A . 1 . . . A n , given a constant S , m, she will turn these n the number n do k group operations, each operation comprising the following step:
1.swap(as,am)(交换a_s,a_m)
2. 2 to n number of both a forward translation (the first . 1 1 moved to the n-th positions)
Chino want to know, k the set of operations, which n number n are how much?
Orz yky, dyh, wjk, jjy, cxr, gsy, cpy, zcy, tyz, yy, Hz, zhr YG,
Input Format
The first row, four numbers, n-, S, m, K
The next line n number, representing A_1, A_2 ... A_N A . 1 , A 2 . . . A n
Output Format
An output line, n- number, representing A_1, A_2 ... A_N A . 1 , A 2 . . . A n-
Sample input and output
4 1 2 3 1 2 3 4
1 2 3 4
Description / Tips
All figures are in long long or less
Ideas:
Matrix acceleration recursive, configured two matrices, a memory swap operation, a shift operation memory.
If the exchange 1 and 2:
0 1 0 0
1 0 0 0
0 0 0 1
0 0 0 1
Shift operation, all moved forward one,
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
Fast power to solve the matrix K power, namely the number of operations:
KSM JZ (JZ A, int B) { JZ ANSS; Memset (anss.c, 0, the sizeof (anss.c)); for (int I =. 1; I <= n-; I ++) anss.c [I] [ I] =. 1; for (; B; B >> =. 1, a = a * a) { IF (B &. 1) ANSS = a * ANSS; // a = a * a; was wrong, because previously calculated } ANSS return; }
Overloaded multiplication sign:
struct jz{ int c[100][100]; }f,base,l1,l2; int n,m,s,k; jz operator * (const jz &a,const jz &b) { jz lin; for(int i=1;i<=80;i++) for(int j=1;j<=80;j++) { lin.c[i][j]=0; for(int k=1;k<=80;k++) { lin.c[i][j]+=(a.c[k][j] * b.c[i][k]); } } return lin; }
Code:
#include<cstdio> #include<cstdlib> #include<iostream> #include<cmath> #include<cstring> #define int long long using namespace std; struct jz{ int c[100][100]; }f,base,l1,l2; int n,m,s,k; jz operator * (const jz &a,const jz &b) { jz lin; for(int i=1;i<=80;i++) for(int j=1;j<=80;j++) { lin.c[i][j]=0; for(int k=1;k<=80;k++) { lin.c[i][j]+=(a.c[k][j] * b.c[i][k]); } } return lin; } void dy(jz x)//调试用的,可以忽略 { for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) cout<<x.c[i][j]<<" "; cout<<endl; } } jz ksm(jz a,int b) { jz anss; memset(anss.c,0,sizeof(anss.c)); for(int i=1;i<=n;i++) anss.c[i][i]=1; for(;b;b>>=1,a=a*a) { if(b&1)anss=a*anss; // a=a*a; } return anss; } signed main() { scanf("%lld%lld%lld%lld",&n,&s,&m,&k); for(int i=1;i<=n;i++) scanf("%lld",&f.c[i][1]); for(int i=1;i<=n;i++) if(i!=s&&i!=m)l1.c[i][i]=1; l1.c[s][m]=l1.c[m][s]=1; for(int i=1;i<=n-1;i++)l2.c[i][i+1]=1; l2.c[n][1]=1; base=l1*l2; base=ksm(base,k); f=f*base; for(int i=1;i<=n;i++)printf("%lld ",f.c[i][1]); cout<<endl; dy(l1); cout<<endl; dy(l2); return 0; }
Thanks to the help of wlj_dy