No.3136 F,B in FizzBuzzString16
タグ : / 解いたユーザー数 18
作問者 :
ID 21712
Introduction
This problem is reposted with permission from a problem on HackerRank. As nobody had solved it there, the whole set of problem statement, editorial, test cases and solution code was provided for this repost.
Problem Statement
Define FizzBuzz16 as follows (* pseudocode)
function FizzBuzz16(i) {
if ( i % 15 == 0 ) {
return "FizzBuzz"
} else if ( i % 3 == 0 ) {
return "Fizz"
} else if ( i % 5 == 0 ) {
return "Buzz"
} else {
return i.toString(16).toUpperCase()
}
}
* i % 15 == 0 is an expression that evaluates to the boolean value true when the remainder of the integer i divided by 15 equals 0
* i.toString(16).toUpperCase() is an operation that converts the integer i into a string in hexadecimal notation (with uppercase letters)
For example, FizzBuzz16(14) is "E", FizzBuzz16(15) is "FizzBuzz", FizzBuzz16(16) is "10", and FizzBuzz16(251) is "FB"
Define FizzBuzzString16 as follows (* pseudocode)
function FizzBuzzString16(n) {
s = ""
for ( i = 1 ; i <= n ; i++ ) {
s += FizzBuzz16(i)
}
return s
}
* i++ is an operation that increases the value of the integer i by 1
* s += is an operation that appends the string on the right-hand side to the end of the string s
For example, FizzBuzzString16(4) is "12Fizz4", and FizzBuzzString16(16) is "12Fizz4BuzzFizz78FizzBuzzBFizzDEFizzBuzz10"
You are given integers $X$ and $Y$. Find the total of the number of characters F and the number of characters B between the $X$-th character and the $Y$-th character (* including the $X$-th and $Y$-th characters) of FizzBuzzString16(1000000000000)
For example, when $X=1,Y=5$, the $1$st to $5$th characters of FizzBuzzString16(1000000000000) are "12Fiz", with $1$ character F and $0$ characters B, for a total of $1$
For example, when $X=3,Y=18$, the $3$rd to $18$th characters of FizzBuzzString16(1000000000000) are "Fizz4BuzzFizz78F", with $3$ characters F and $1$ character B, for a total of $4$
* Just in case, code corresponding to the definitions of FizzBuzz16 and FizzBuzzString16 in languages other than pseudocode is provided below (click each button to expand)
C++
#include <sstream>
std::string to_hex_string(int64_t i) {
std::stringstream s;
s << std::uppercase << std::hex << i;
return s.str();
}
std::string FizzBuzz16(int64_t i) {
if (i % 15 == 0) {
return "FizzBuzz";
} else if (i % 3 == 0) {
return "Fizz";
} else if (i % 5 == 0) {
return "Buzz";
} else {
return to_hex_string(i);
}
}
std::string FizzBuzzString16(int64_t n) {
std::string s = "";
for (int64_t i = 1; i <= n; i++) {
s += FizzBuzz16(i);
}
return s;
}
Python
def FizzBuzz16(i):
if i % 15 == 0:
return "FizzBuzz"
elif i % 3 == 0:
return "Fizz"
elif i % 5 == 0:
return "Buzz"
else:
return "%X" % i
def FizzBuzzString16(n):
s = ""
for i in range(1,n+1):
s += FizzBuzz16(i)
return s
Java
String FizzBuzz16(long i) {
if (i % 15L == 0L) {
return "FizzBuzz";
} else if (i % 3L == 0L) {
return "Fizz";
} else if (i % 5L == 0L) {
return "Buzz";
} else {
return String.format("%X", i);
}
}
String FizzBuzzString16(long n) {
String s = "";
for (long i = 1L; i <= n; i++) {
s += FizzBuzz16(i);
}
return s;
}
Input
$X$ $Y$
The integer $X$ is given on the first line
The integer $Y$ is given on the second line
Constraints
$1 \le X \le Y \le 1{,}000{,}000{,}000{,}000=10^{12}$
Output
Output the total of the number of characters F and the number of characters B between the $X$-th character and the $Y$-th character (* including the $X$-th and $Y$-th characters) of FizzBuzzString16(1000000000000)
Sample
Sample 1
Input
1 5
Output
1
The $1$st to $5$th characters of FizzBuzzString16(1000000000000) are "12Fiz", with $1$ character F and $0$ characters B, for a total of $1$
Sample 2
Input
3 18
Output
4
The $3$rd to $18$th characters of FizzBuzzString16(1000000000000) are "Fizz4BuzzFizz78F", with $3$ characters F and $1$ character B, for a total of $4$
Sample 3
Input
33 33
Output
1
The $33$rd character of FizzBuzzString16(1000000000000) is "F", with $1$ character F and $0$ characters B, for a total of $1$
Sample 4
Input
769 797
Output
11
The $769$th to $797$th characters of FizzBuzzString16(1000000000000) are "F4BuzzFizzF7F8FizzBuzzFBFizzF", with $8$ characters F and $3$ characters B, for a total of $11$
Sample 5
Input
128153 128159
Output
0
The $128153$rd to $128159$th characters of FizzBuzzString16(1000000000000) are "7777777", which contain neither the character F nor the character B
Sample 6
Input
808 277288
Output
52258
Sample 7
Input
1 1000000000000
Output
157481468199
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。