P5550 Chino的数列

P5550 Chino的数列

解题思路:

假设有一个矩阵 b a s e base base
n = 4 , s = 2 , m = 4 n=4,s=2,m=4 n=4,s=2,m=4
[ a 1 a 2 a 3 a 4 ] ∗ b a s e = [ a 4 a 3 a 2 a 1 ] \begin{bmatrix}a1&a2&a3&a4\end{bmatrix} * base = \begin{bmatrix}a4&a3&a2&a1\end{bmatrix} [a1a2a3a4]base=[a4a3a2a1]
b a s e base base能得到一个四行四列的矩阵。接着用快速幂就可以了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 2e5+10;
const ll mod = 1e9+7;
const double PI = 3.1415926535;
const double eps = 1e-4;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline string readstring(){
    string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}return str;
}
int random(int n){
    return (int)(rand()*rand())%n;
}



typedef struct str{
    ll mx[85][85];
}mx;
int n;


mx multiply(mx a,mx b){
    mx c;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            c.mx[i][j] = 0ll;
            for(int k = 1;k <= n;k++){
                c.mx[i][j] += a.mx[i][k]*b.mx[k][j];
            }
        }
    }
    return c;
}
mx fast_power(mx a,ll p){
    mx ans;
    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++){
            ans.mx[i][j] = 0;
        }
        ans.mx[i][i] = 1;
    }
    while(p){
        if(p&1) ans = multiply(ans,a);
        p /= 2;
        a = multiply(a,a);
    }
    return ans;
}
ll num[85];
mx a;
void init(int s,int m){
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){a.mx[i][j] = 0;}
    }
    a.mx[1][n] = 1ll;
    for(int i = 2,j = 1;i <= n;i++,j++){
        a.mx[i][j] = 1;
    }
    /*
    for(int i = 1;i <= n;i++){
        b.mx[i][i] = 1;
    }
    b.mx[s][s] = b.mx[m][m] = 0;
    b.mx[s][m] = b.mx[m][s] = 1;
    */
}
int main(){
    srand((unsigned)time(NULL));
    n = read();
    int s = read(),m = read();
    ll k;cin >> k;
    init(s,m);
    swap(a.mx[s],a.mx[m]);

    a = fast_power(a,k);
    for(int i = 1;i <= n;i++){
        cin >> num[i];
    }
    for(int j = 1;j <= n;j++){
        ll sum = 0;
        for(int i = 1;i <= n;i++){
            sum += num[i]*a.mx[i][j];
        }
        printf("%lld ",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42868863/article/details/113406755