3 import java
.math
.BigInteger
;
5 public class garbling_rs
implements Runnable
{
6 private BufferedReader in
;
7 private PrintWriter out
;
9 private void check(boolean expr
, String msg
) {
15 private void checkBounds(long n
, long low
, long hi
, String nStr
) {
16 check((low
<= n
) && (n
<= hi
), nStr
+ " is not in [" + low
+ ", " + hi
+ "]");
21 private int[] fTemp
, fSingle
;
22 private int[] aTemp
, aSingle
;
23 private static final int modulo
= 100000;
25 private void applySingle(int[] f
, int[] a
, int n
) {
27 for (int i
= 0; i
< r
- 1; i
++) {
28 for (int j
= 0; j
< c
- 1; j
++) {
35 int p22
= p11
+ c
+ 1;
38 while (a
[f
[p11
]] >= modulo
) {
41 if (map
[i
][j
] == 'L') {
47 } else if (map
[i
][j
] == 'R') {
58 private void inversePerm(int[] f
) {
59 for (int i
= 0; i
< f
.length
; i
++) {
62 System
.arraycopy(fTemp
, 0, f
, 0, f
.length
);
65 private void apply(BigInteger n
, int[] f
, int[] a
) {
66 if (n
.equals(BigInteger
.ZERO
)) {
67 for (int i
= 0; i
< f
.length
; i
++) {
73 apply(n
.shiftRight(1), f
, a
);
74 for (int i
= 0; i
< f
.length
; i
++) {
76 aTemp
[i
] = a
[i
] + a
[f
[i
]];
77 if (aTemp
[i
] >= modulo
) {
81 System
.arraycopy(fTemp
, 0, f
, 0, f
.length
);
82 System
.arraycopy(aTemp
, 0, a
, 0, a
.length
);
84 for (int i
= 0; i
< f
.length
; i
++) {
85 fTemp
[i
] = f
[fSingle
[i
]];
86 aTemp
[i
] = aSingle
[i
] + a
[fSingle
[i
]];
87 if (aTemp
[i
] >= modulo
) {
91 System
.arraycopy(fTemp
, 0, f
, 0, f
.length
);
92 System
.arraycopy(aTemp
, 0, a
, 0, a
.length
);
96 private void solve() throws IOException
{
97 StringTokenizer st
= new StringTokenizer(in
.readLine());
98 r
= Integer
.parseInt(st
.nextToken());
99 c
= Integer
.parseInt(st
.nextToken());
100 BigInteger n
= new BigInteger(st
.nextToken());
101 checkBounds(r
, 2, 300, "r");
102 checkBounds(c
, 2, 300, "c");
103 check(n
.compareTo(BigInteger
.ZERO
) >= 0, "n is negative");
104 check(n
.compareTo(BigInteger
.TEN
.pow(100)) < 0, "n is greater than 10^{100}");
105 map
= new char[r
- 1][];
106 for (int i
= 0; i
< r
- 1; i
++) {
107 String line
= in
.readLine();
108 check(line
.length() == c
- 1, "Invalid size of map");
109 map
[i
] = line
.toCharArray();
110 for (int j
= 0; j
< c
- 1; j
++) {
111 check(map
[i
][j
] == 'L' || map
[i
][j
] == 'R' || map
[i
][j
] == 'N', "invalid character in map");
115 fTemp
= new int[r
* c
];
116 aTemp
= new int[r
* c
];
117 int[] f
= new int[r
* c
];
118 int[] a
= new int[r
* c
];
119 int block
= (r
- 1) * (c
- 1);
121 fSingle
= new int[r
* c
];
122 aSingle
= new int[r
* c
];
123 for (int i
= 0; i
< f
.length
; i
++) {
127 applySingle(fSingle
, aSingle
, block
);
128 inversePerm(fSingle
);
130 apply(n
.divide(BigInteger
.valueOf(block
)), f
, a
);
132 applySingle(f
, a
, n
.mod(BigInteger
.valueOf(block
)).intValue());
133 for (int i
= 0; i
< f
.length
; i
++) {
138 public static void main(String
[] args
) {
139 new Thread(new garbling_rs()).start();
143 String problem
= getClass().getName().split("_")[0];
145 in
= new BufferedReader(new FileReader(new File(problem
+ ".in")));
146 out
= new PrintWriter(new File(problem
+ ".out"));
150 } catch (IOException e
) {