This page is still under construction.

Parts of this page are still being built. What you see may change.

Marbles in a Circle

Time limit1sMemory limit256 MB

Summary
Given a ring of red, white, and green marbles with a neighbor rule, compute how many marbles of each color remain after N steps.
Level

Hard8 of 10

Topics
Number theory, Math, Combinatorics, Simulation
Solved
No attempts yet

Problem

Colored marbles sit in a circle. Each marble is red, white, or green. Every second all marbles change color at the same time, and a marble's new color depends only on its own color and the color of the marble to its right.

The rules for changing color are:

  • if the marble is white, its new color is the current color of the marble to its right.
  • otherwise, if the marble to its right is white, the marble keeps its color.
  • otherwise, if the marble to its right has a different color, the marble becomes white.
  • otherwise the marble has the same color as the one to its right, and it flips: red becomes green, green becomes red.

You are given a string SS and an integer NN. Read SS as an array of characters of length LL, so the circle holds LL marbles. The character W is a white marble, R is red, and G is green. Marble i+1i+1 is to the right of marble ii, and marble 00 is to the right of marble L−1L-1. Determine the state of the circle after NN seconds.

Input

The first line contains TT (T≤20T \le 20), the number of test cases.

Each of the next TT lines contains a string SS (1≤∣S∣≤200001 \le |S| \le 20000) and an integer NN (1≤N≤10181 \le N \le 10^{18}) separated by a single space. SS consists only of the characters W, R, and G.

Output

For each test case, print one line in the form Case #X: W R G, where XX is the test case number starting from 1, WW is the number of white marbles, RR is the number of red marbles, and GG is the number of green marbles after NN seconds. Separate the four values with a single space.

Examples1

  1. Example 1

    Input
    5
    RRGWRGW 2
    WRGWRGWRGWRG 4
    WGRWRGRRWGRWRG 3
    RGRGWRGRG 3
    RGRGRGRGWWWWRR 5
    
    Expected output
    Case #1: 3 1 3
    Case #2: 4 4 4
    Case #3: 10 1 3
    Case #4: 5 2 2
    Case #5: 8 2 4