작성일 :

문제 링크

25494번 - 단순한 문제 (Small)

설명

(x mod y) = (y mod z) = (z mod x)를 만족하는 (x, y, z) 쌍의 개수를 구하는 문제입니다.


접근법

범위가 작으므로 세 중첩 반복으로 모든 조합을 확인합니다.

조건을 만족하면 개수를 증가시킵니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    while (t-- > 0) {
      var p = Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);
      var a = p[0]; var b = p[1]; var c = p[2];
      var cnt = 0;
      for (var x = 1; x <= a; x++)
        for (var y = 1; y <= b; y++)
          for (var z = 1; z <= c; z++)
            if (x % y == y % z && y % z == z % x) cnt++;
      Console.WriteLine(cnt);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    int a, b, c; cin >> a >> b >> c;
    int cnt = 0;
    for (int x = 1; x <= a; x++)
      for (int y = 1; y <= b; y++)
        for (int z = 1; z <= c; z++)
          if (x % y == y % z && y % z == z % x) cnt++;
    cout << cnt << "\n";
  }

  return 0;
}