fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String S = scanner.nextLine();
  7. long L = scanner.nextLong();
  8. long R = scanner.nextLong();
  9.  
  10. int n = S.length();
  11. long countB = 0;
  12. for (char c : S.toCharArray()) {
  13. if (c == 'B') {
  14. countB++;
  15. }
  16. }
  17.  
  18. int[] prefix = new int[n + 1];
  19. prefix[0] = 0;
  20. for (int i = 1; i <= n; i++) {
  21. prefix[i] = prefix[i - 1] + (S.charAt(i - 1) == 'B' ? 1 : 0);
  22. }
  23.  
  24. long result = f(R, n, countB, prefix) - f(L - 1, n, countB, prefix);
  25. System.out.println(result);
  26. }
  27.  
  28. private static long f(long i, int n, long countB, int[] prefix) {
  29. if (i == 0) {
  30. return 0;
  31. }
  32. long k = i / n;
  33. int r = (int) (i % n);
  34. return k * countB + prefix[r];
  35. }
  36. }
Success #stdin #stdout 0.12s 54648KB
stdin
BBRB
4 8
stdout
4