4 public class garbling_gk
{
6 static PrintWriter out
;
16 Permutation(int[] perm
, long[] times
) {
21 public Permutation
multiply(Permutation that
) {
23 int[] perm
= new int[n
];
24 long[] times
= new long[n
];
26 for (int i
= 0; i
< n
; i
++) {
28 times
[i
] = this.times
[i
] + that
.times
[j
];
29 perm
[i
] = that
.perm
[j
];
32 return new Permutation(perm
, times
);
36 private void garbleLeft(int[][] f
, int i
, int j
) {
38 f
[i
][j
] = f
[i
][j
+ 1];
39 f
[i
][j
+ 1] = f
[i
+ 1][j
+ 1];
40 f
[i
+ 1][j
+ 1] = f
[i
+ 1][j
];
44 private void garbleRight(int[][] f
, int i
, int j
) {
46 f
[i
][j
] = f
[i
+ 1][j
];
47 f
[i
+ 1][j
] = f
[i
+ 1][j
+ 1];
48 f
[i
+ 1][j
+ 1] = f
[i
][j
+ 1];
52 private int[][] fill() {
53 int[][] f
= new int[r
][c
];
54 for (int i
= 0; i
< r
; i
++) {
55 for (int j
= 0; j
< c
; j
++) {
56 f
[i
][j
] = index(i
, j
);
62 private long[] garble(long n
, char[][] g
, int[][] f
) {
63 long[] times
= new long[r
* c
];
64 for (int i
= 0; i
< r
- 1; i
++) {
65 for (int j
= 0; j
< c
- 1; j
++) {
72 } else if (g
[i
][j
] == 'R') {
81 private Permutation
garble(long n
, char[][] g
) {
84 long[] times
= garble(n
, g
, f
);
85 int[] perm
= new int[r
* c
];
86 for (int i
= 0; i
< r
; i
++) {
87 for (int j
= 0; j
< c
; j
++) {
88 perm
[f
[i
][j
]] = index(i
, j
);
91 return new Permutation(perm
, times
);
94 private int index(int i
, int j
) {
101 long n
= in
.nextLong();
103 char[][] g
= new char[r
- 1][];
104 for (int i
= 0; i
< r
- 1; i
++) {
105 g
[i
] = in
.next().toCharArray();
108 int loop
= (r
- 1) * (c
- 1);
109 Permutation result
= garble(n
% loop
, g
);
110 Permutation power
= garble(loop
, g
);
114 result
= power
.multiply(result
);
116 power
= power
.multiply(power
);
120 for (int i
= 0; i
< r
; i
++) {
121 for (int j
= 0; j
< c
; j
++) {
122 out
.println(result
.times
[index(i
, j
)]);
127 public static void main(String
[] args
) throws Exception
{
128 in
= new Scanner(new File("garbling.in"));
129 out
= new PrintWriter("garbling.out");
131 new garbling_gk().run();