Submission #7128920


Source Code Expand

Copy
<?php
define('MOD', 10 ** 9 + 7);

fscanf(STDIN, '%d', $n);
fscanf(STDIN, '%s', $s);

if ($s[0] === 'W' || $s[2 * $n - 1] === 'W') {
    echo (0) . PHP_EOL;
    exit();
}

$lr[0] = 'L';
$l_cnt_sum[0] = 0;
$l_cnt_sum[1] = 1;
$l_cnt = 1;
$r_cnt = 0;
$x = 1;
for ($i = 1; $i < 2 * $n; $i++) {
    if ($s[$i] !== $s[$i - 1]) {
        if ($lr[$i - 1] === 'L') $lr[$i] = 'L';
        else $lr[$i] = 'R';
    } else {
        if ($lr[$i - 1] === 'L') $lr[$i] = 'R';
        else $lr[$i] = 'L';
    }
    if ($lr[$i] === 'L') {
        $l_cnt_sum[$i + 1] = $l_cnt_sum[$i] + 1;
        $l_cnt++;
    } else {
        $x = $x * ($l_cnt_sum[$i] - $r_cnt) % MOD;
        $l_cnt_sum[$i + 1] = $l_cnt_sum[$i];
        $r_cnt++;
    }
}

if ($l_cnt !== $r_cnt) {
    echo (0) . PHP_EOL;
    exit();
}

$ans = $x * kaijou($n) % MOD;
echo $ans . PHP_EOL;

function kaijou($n) {
    return $n > 1 ? $n * kaijou($n - 1) % MOD : 1;
}

Submission Info

Submission Time
Task C - Cell Inversion
User hideyuk1
Language PHP7 (7.0.15)
Score 500
Code Size 959 Byte
Status
Exec Time 82 ms
Memory 45548 KB

Test Cases

Set Name Score / Max Score Test Cases
All 500 / 500 sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19, testcase_20, testcase_21, testcase_22, testcase_23, testcase_24, testcase_25, testcase_26, testcase_27, testcase_28, testcase_29, testcase_30, testcase_31, testcase_32, testcase_33
Sample 0 / 0 sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 9 ms 4604 KB
sample_02 9 ms 4604 KB
sample_03 9 ms 4604 KB
testcase_01 9 ms 4604 KB
testcase_02 38 ms 14840 KB
testcase_03 9 ms 4604 KB
testcase_04 23 ms 12792 KB
testcase_05 42 ms 21876 KB
testcase_06 36 ms 19316 KB
testcase_07 60 ms 35308 KB
testcase_08 78 ms 45548 KB
testcase_09 80 ms 45548 KB
testcase_10 9 ms 4604 KB
testcase_11 9 ms 4604 KB
testcase_12 10 ms 4604 KB
testcase_13 78 ms 41452 KB
testcase_14 60 ms 33260 KB
testcase_15 80 ms 45548 KB
testcase_16 44 ms 24180 KB
testcase_17 73 ms 39404 KB
testcase_18 54 ms 23024 KB
testcase_19 57 ms 23024 KB
testcase_20 31 ms 14840 KB
testcase_21 67 ms 23024 KB
testcase_22 66 ms 23024 KB
testcase_23 39 ms 14840 KB
testcase_24 70 ms 23024 KB
testcase_25 80 ms 45548 KB
testcase_26 9 ms 4604 KB
testcase_27 9 ms 4604 KB
testcase_28 82 ms 45548 KB
testcase_29 9 ms 4604 KB
testcase_30 79 ms 45548 KB
testcase_31 10 ms 4604 KB
testcase_32 10 ms 4604 KB
testcase_33 10 ms 4604 KB