{ }codelper
powersadditionP0409

Praise stickers on the New Year's resolution checklist

Starting on New Year's Day, you wrote down every day for n days whether you kept your New Year's resolution.
The first line gives n, b and c separated by single spaces.
The second line gives the checklist as a string of length n.
The i-th character of the string is the record of day i: the uppercase letter O means you kept the resolution that day, and the uppercase letter X means you did not.
On each day you kept it, you get praise stickers.
Let s be the number of consecutive O days ending on that day, counting that day and going backwards without a break.
If s ≤ c, you get b^(s−1) stickers that day.
If s > c, you get b^(c−1) stickers that day.
In other words, the c-th consecutive O day gives b^(c−1), and from the (c+1)-th consecutive day on it stays b^(c−1).
b^k is the product of k copies of b, and b^0 = 1.
On an X day you get no stickers, and s starts again from 1 on the next O day.
Print the total number of stickers you got over the n days.
If you never kept the resolution, print 0.
Print the number with digits only, without commas.

Input
1 ≤ n ≤ 366, 2 ≤ b ≤ 10, 1 ≤ c ≤ 40, b^(c−1) ≤ 1,000,000,000,000 (10^12), the checklist contains only O and X
Output
One line: the total number of stickers
Example 1 · stdin
7 2 3
OOOOXOO
stdout
14
Example 2 · stdin
15 10 13
OOOOOOOOOOOOOOO
stdout
3111111111111
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

This problem takes all 8 languages.
You don't need to recompute a power every day: the first day of a run gives 1 sticker, up to the c-th day you multiply yesterday's count by b (today *= b), and after that it stays.
Only the Python solution writes it directly as b ** (min(streak, c) - 1).
Trap one is ^.
In Python, JS, TS, C, C++, Java and C#, ^ is bitwise XOR, not a power, so 2 ^ 2 is 0 and 10 ^ 12 is 6.
Computing example 1 with b ^ (s - 1) gives 2+3+0+0+2+3 = 10.
Only Python, JS and TS have a power operator, **; C/C++ pow, Java Math.pow and C# Math.Pow return a double.
Craykon has neither a power operator nor a Mathf function for it (** and ^ are compile errors), so repeat multiplication.
Trap two is overflow.
One day can give up to 10^12 stickers and the total can pass 3.5×10^14, which does not fit in an int (about 2.1 billion).
In C, C++, Java and C#, keep the daily count and the total in long (long long).
Java and C# ints wrap around without an error, so example 2 prints 1554788807 instead of 3111111111111 (signed int overflow in C/C++ is undefined behaviour).
Python integers never overflow, and JS/TS/Craykon numbers are doubles, but the total stays far below 2^53 (about 9×10^15), so they are exact.
Trap three is the edge of the cap.
Day s gives b^(s−1), so the c-th day already reaches the cap b^(c−1).
Capping at b^c instead makes day 4 of example 1 give 8 instead of 4, and the answer becomes 18.
Trap four is the X day.
If you don't reset the run length to 0, days 6 and 7 of example 1 give 4 and 4 instead of 1 and 2, and the answer becomes 19.

Pythonmain.py · 12 lines
n, b, c = map(int, input().split())days = input().strip()total = 0streak = 0  # 오늘까지 이어진 O의 날 수for ch in days:    if ch == "O":        streak += 1        # c일째부터는 늘지 않는다        total += b ** (min(streak, c) - 1)    else:        streak = 0print(total)
JavaScriptmain.js · 17 lines
const [first, days] = require("fs").readFileSync(0, "utf8").split("\n");const [n, b, c] = first.trim().split(/\s+/).map(Number);let total = 0;let streak = 0; // 어제까지 이어진 O의 날 수let today = 0; // 오늘 받는 스티커for (const ch of days.trim()) {  if (ch === "O") {    // 첫날은 1장, 그다음은 c일째까지 b배씩    if (streak === 0) today = 1;    else if (streak < c) today *= b;    streak++;    total += today; // 많아야 약 3.7 × 10^14라 2^53 안에서 정확하다  } else {    streak = 0;  }}console.log(total);
TypeScriptmain.ts · 18 lines
import { readFileSync } from "node:fs";const [first, days] = readFileSync(0, "utf8").split("\n");const [n, b, c]: number[] = first.trim().split(/\s+/).map(Number);let total = 0;let streak = 0; // 어제까지 이어진 O의 날 수let today = 0; // 오늘 받는 스티커for (const ch of days.trim()) {  if (ch === "O") {    // 첫날은 1장, 그다음은 c일째까지 b배씩    if (streak === 0) today = 1;    else if (streak < c) today *= b;    streak++;    total += today; // 많아야 약 3.7 × 10^14라 2^53 안에서 정확하다  } else {    streak = 0;  }}console.log(total);
JavaMain.java · 24 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();        String days = sc.next();        long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다        long today = 0;        int streak = 0; // 어제까지 이어진 O의 날 수        for (char ch : days.toCharArray()) {            if (ch == 'O') {                // 첫날은 1장, 그다음은 c일째까지 b배씩                if (streak == 0) today = 1;                else if (streak < c) today *= b;                streak++;                total += today;            } else {                streak = 0;            }        }        System.out.println(total);    }}
C#Main.cs · 30 lines
using System; class Program{    static void Main()    {        string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int b = int.Parse(t[1]), c = int.Parse(t[2]);        string days = Console.ReadLine().Trim();        long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다        long today = 0;        int streak = 0; // 어제까지 이어진 O의 날 수        foreach (char ch in days)        {            if (ch == 'O')            {                // 첫날은 1장, 그다음은 c일째까지 b배씩                if (streak == 0) today = 1;                else if (streak < c) today *= b;                streak++;                total += today;            }            else            {                streak = 0;            }        }        Console.WriteLine(total);    }}
C++main.cpp · 23 lines
#include <iostream>#include <string> int main() {    int n, b, c;    std::string days;    std::cin >> n >> b >> c >> days;    long long total = 0; // 10^12장짜리 날이 하루만 있어도 int를 넘는다    long long today = 0;    int streak = 0; // 어제까지 이어진 O의 날 수    for (char ch : days) {        if (ch == 'O') {            // 첫날은 1장, 그다음은 c일째까지 b배씩            if (streak == 0) today = 1;            else if (streak < c) today *= b;            streak++;            total += today;        } else {            streak = 0;        }    }    std::cout << total << "\n";}
Cmain.c · 23 lines
#include <stdio.h> int main(void) {    int n, b, c;    char days[400];    scanf("%d %d %d %399s", &n, &b, &c, days);    long long total = 0; /* 10^12장짜리 날이 하루만 있어도 int를 넘는다 */    long long today = 0;    int streak = 0; /* 어제까지 이어진 O의 날 수 */    for (int i = 0; days[i] != '\0'; i++) {        if (days[i] == 'O') {            /* 첫날은 1장, 그다음은 c일째까지 b배씩 */            if (streak == 0) today = 1;            else if (streak < c) today *= b;            streak++;            total += today;        } else {            streak = 0;        }    }    printf("%lld\n", total);    return 0;}
Craykonmain.cray · 29 lines
수들 = Console.ReadNumbers();b = 수들[1];c = 수들[2];days = Console.ReadLine().Trim();// 크레이콘에는 거듭제곱이 없어서 어제 받은 장수에 b를 곱해 간다total = 0;today = 0;streak = 0;for (i = 0; i < days.Length; i++){    if (days[i] == "O")    {        if (streak == 0)        {            today = 1;        }        else if (streak < c)        {            today *= b;        }        streak++;        total += today;    }    else    {        streak = 0;    }}Console.WriteLine(total);