ABC051 B - Sum of Three Integers

Calendar Clock iconCalendar Clock icon

atcoder

Table of contents

# Problem

https://atcoder.jp/contests/abc051/tasks/abc051_b

Given integers K and S, report the number of combinations of integers X, Y, Z which satisfy 0X,Y,ZK0 \leq X, Y, Z \leq K and X+Y+Z=SX + Y + Z = S.

# Time complexity

O(K2)O(K^2)

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
#define MAX_K 2501
#define MAX_S 3 * MAX_K

Int K, S;

void input() {
  cin >> K >> S;
}

void solve() {
  Int count = 0;
  loop(x,0,K+1) {
    loop(y,0,K+1) {
      Int z = S - x - y;
      if (0 <= z && z <= K) count++;
    }
  }
  cout << count << endl;
}

int main(void) {
  input();
  solve();
}

Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!

Comments