forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpix.cpp
181 lines (181 loc) · 3.51 KB
/
jpix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 2009-04-25
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
using namespace std;
typedef unsigned long long LL;
#define next1(x) ((x<(N>>1))?(x<<1):((x<<1)-N+1))
#define next2(x) ((x&1)?(x>>1)+(N>>1):(x>>1))
int GCD(int x,int y)
{
if (x<y)
return GCD(y,x);
if (y==0)
return x;
return GCD(y,x%y);
}
int LCM(int x,int y)
{
LL res=(LL)x*(LL)y/GCD(x,y);
if (res>2147483647) throw;
return res;
}
static int a[1024][1024];
static int A[1024][1024];
int tmp[1024];
int tmp2[1024];
static bool b[1024][1024];
bool B[1024];
int main()
{
int N,i,j,cnt,z;
char s[1000];
string cmds[32];
string cmd;
for(;;)
{
scanf("%d",&N);
getchar();
if (!N) return 0;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
a[i][j]=j+(i<<10);
gets(s);
istringstream ss; ss.str(s);
int k=0;
while (!ss.eof())
{
ss>>cmds[k];
if (cmds[k].length())
k++;
else
break;
}
while (k--)
{
cmd=cmds[k];
if (cmd[0]=='i') //id or id-
continue;
if (cmd[0]=='r')
if (cmd.length()==3) //90 degree rotation counterclockwise
for (i=0; i<(N>>1); i++)
for (j=0; j<(N>>1); j++)
{
z=a[j][i];
a[j][i]=a[i][N-j-1];
a[i][N-j-1]=a[N-j-1][N-i-1];
a[N-j-1][N-i-1]=a[N-i-1][j];
a[N-i-1][j]=z;
}
else //90 degree rotation clockwise
for (i=0; i<(N>>1); i++)
for (j=0; j<(N>>1); j++)
{
z=a[j][i];
a[j][i]=a[N-i-1][j];
a[N-i-1][j]=a[N-j-1][N-i-1];
a[N-j-1][N-i-1]=a[i][N-j-1];
a[i][N-j-1]=z;
}
else if (cmd[0]=='s') //horizontal symmetry
for (j=0; j<N; j++)
for (i=0; i<(N>>1); i++)
swap(a[j][i],a[j][N-i-1]);
else if (cmd[1]=='h') //bhsym+-
for (j=N>>1; j<N; j++)
for (i=0; i<(N>>1); i++)
swap(a[j][i],a[j][N-i-1]);
else if (cmd[1]=='v') //bvsym+-
for (j=(N>>1),z=N-1; j<z; j++,z--)
{
memcpy(tmp,a[j],N<<2);
memcpy(a[j],a[z],N<<2);
memcpy(a[z],tmp,N<<2);
}
else if (cmd[0]=='d') //div
{
memset(B,false,sizeof(bool)*N);
if (cmd.length()==3) //div+
for (i=0; i<N; i++)
{
if (B[i]) continue;
B[i]=true;
memcpy(tmp,a[i],N<<2);
j=i;
while ((z=next1(j))!=i)
{
B[z]=true;
memcpy(a[j],a[z],N<<2);
j=z;
}
memcpy(a[j],tmp,N<<2);
}
else //div-
for (i=0; i<N; i++)
{
if (B[i]) continue;
B[i]=true;
memcpy(tmp,a[i],N<<2);
j=i;
while ((z=next2(j))!=i)
{
B[z]=true;
memcpy(a[j],a[z],N<<2);
j=z;
}
memcpy(a[j],tmp,N<<2);
}
}
else if (cmd.length()==3) //mix+
for (i=0; i<N; i+=2)
{
memcpy(tmp,a[i],N<<2);
memcpy(tmp2,a[i+1],N<<2);
for (j=0; j<N; j+=2)
{
a[i][j]=tmp[j>>1];
a[i][j+1]=tmp2[j>>1];
a[i+1][j]=tmp[(j>>1)+(N>>1)];
a[i+1][j+1]=tmp2[(j>>1)+(N>>1)];
}
}
else //mix-
for (i=0; i<N; i+=2)
{
memcpy(tmp,a[i],N<<2);
memcpy(tmp2,a[i+1],N<<2);
for (j=0; j<N; j+=2)
{
a[i][j>>1]=tmp[j];
a[i][(j>>1)+(N>>1)]=tmp2[j];
a[i+1][j>>1]=tmp[j+1];
a[i+1][(j>>1)+(N>>1)]=tmp2[j+1];
}
}
}
/*for (i=0; i<N; i++)
memset(b[i],false,sizeof(bool)*N);*/
memset(b,false,sizeof(b));
int x,y,t;
LL res=1;
for (j=0; j<N; j++)
for (i=0; i<N; i++)
{
if (b[j][i])
continue;
x=i,y=j;
cnt=0;
while (!b[y][x])
{
b[y][x]=true;
cnt++;
t=x;
x=a[y][x]&1023;
y=a[y][t]>>10;
}
res=LCM(res,cnt);
}
printf("%d\n",res);
}
}