bnuoj 51277(魔方复原-手推置换群)

题意:给一个魔方,定义一堆操作,现给出操作序列,问这个操作序列重复多少次之后魔方复原?

用了大半天时间设计程序计算6个置换……结果没做出

其实手推最省事。

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
ll gcd(ll a,ll b){if (!b) return a;return gcd(b,a%b);}
const int d[6][5][4]=
{
    {{1,3,9,7},{2,6,8,4},{43,19,48,18},{44,22,47,15},{45,25,46,12}},//F
    {{10,12,18,16},{11,15,17,13},{37,1,46,36},{40,4,49,33},{43,7,52,30}},//L
    {{19,21,27,25},{20,24,26,22},{54,9,45,28},{51,6,42,31},{48,3,39,34}},//R
    {{37,39,45,43},{38,42,44,40},{28,19,1,10},{29,20,2,11},{30,21,3,12}},//U
    {{16,7,25,34},{17,8,26,35},{46,48,54,52},{47,51,53,49},{18,9,27,36}},//D
    {{28,30,36,34},{29,33,35,31},{27,39,10,52},{24,38,13,53},{21,37,16,54}}//B
};
#define MAXN (6*9+1)
const int n = 54;
const char c[7]="FLRUDB";
int h[1000];

void work(vector<int> &p,int type) { // 一个循环节 
    Rep(k,5) {
        swap(p[d[type][k][2]],p[d[type][k][3]]);
        swap(p[d[type][k][1]],p[d[type][k][2]]);
        swap(p[d[type][k][0]],p[d[type][k][1]]);
    }
}
int t[MAXN],t2[MAXN];

void multi(vector<int> &p,vector<int> q) {
    For(i,n) t[i]=p[q[i]];
    For(i,n) p[i]=t[i];
} 
char s[100000+10];
int x;
vector<int> solve() {
    vector<int> p(n+1);
    For(i,n) p[i]=i;
    while (s[x] && s[x]!=')') {  
        if (isdigit(s[x])) { //数字() 
            ll r=0; 
            while (isdigit(s[x])) {
                r=r*10+s[x++]-'0';
            }
            x++; //去掉左括号
            vector<int> q=solve(); 
            while(r) {
                if (r&1) multi(p,q);
                multi(q,q);
                r>>=1;
            }       
        } else { //字母
            work(p,h[s[x++]]);
        }
    }
    x++; //去掉右括号
    return p;
}
bool vis[MAXN];
ll get_ans(vector<int> p) {
    MEM(vis)
    ll res=1;
    For(i,n) if (!vis[i]) {
        int u=i,s=0;
        while (!vis[u]) {
            vis[u]=1;
            u=p[u];
            s++;
        }
        res=res/gcd(res,s)*s;
    }
    return res;
}
int main()
{
// freopen("B.in","r",stdin);
// freopen(".out","w",stdout);

    Rep(i,6) h[c[i]]=i;

    int T=read();
    while(T--) {
        scanf("%s",s);
        x=0;
        cout<<get_ans(solve())<<endl;       
    }

    return 0;
}

你可能感兴趣的:(bnuoj 51277(魔方复原-手推置换群))