B - How many? Editorial by en_translator

Consider searching exhaustively every tuple \((a,b,c)\) such that \(a+b+c \leq S\) and check if each of them satisfies \(a \times b \times c \leq T\).

This can be implemented with for statements as follows.

Sample code (C++)

using namespace std;

int main(){
    int S, T; cin >> S >> T;
    int cnt = 0;
    for(int a = 0; a <= S; a++){
        for(int b = 0; a+b <= S; b++){
            for(int c = 0; a+b+c <= S; c++){
                if(a*b*c <= T) cnt++;
    cout << cnt << endl;

Sample code (Python)

S, T = map(int, input().split())
res = 0
for a in range(S+1):
    for b in range(S+1-a):
        for c in range(S+1-a-b):
            if a*b*c <= T:

bonus: \(0 \leq S,T \leq 10^{18}\)

last update: