작성일 :

문제 링크

24309번 - РАВЕНСТВО

설명

방정식 a·x = b - c를 만족하는 정수 x를 구하는 문제입니다.


접근법

x는 b에서 c를 뺀 값을 a로 나눈 것입니다.

b와 c가 매우 크므로 큰 정수 연산이 필요합니다.

C#에서는 BigInteger를, C++에서는 문자열 기반 큰 수 연산을 사용합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Numerics;

class Program {
  static void Main() {
    var a = BigInteger.Parse(Console.ReadLine()!);
    var b = BigInteger.Parse(Console.ReadLine()!);
    var c = BigInteger.Parse(Console.ReadLine()!);
    Console.WriteLine((b - c) / a);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

string sub(string x, string y, bool& neg) {
  neg = false;
  if (x.size() < y.size() || (x.size() == y.size() && x < y)) {
    swap(x, y);
    neg = true;
  }
  string res;
  int carry = 0;
  int i = x.size() - 1, j = y.size() - 1;
  while (i >= 0) {
    int d = (x[i] - '0') - carry - (j >= 0 ? y[j] - '0' : 0);
    if (d < 0) {
      d += 10;
      carry = 1;
    }
    else carry = 0;
    res.push_back('0' + d);
    i--;
    j--;
  }
  while (res.size() > 1 && res.back() == '0')
    res.pop_back();
  reverse(res.begin(), res.end());
  return res;
}

string divByLL(const string& x, ll d, bool neg) {
  string res;
  ll rem = 0;
  for (char c : x) {
    rem = rem * 10 + (c - '0');
    res.push_back('0' + rem / d);
    rem %= d;
  }
  int st = 0;
  while (st < (int)res.size() - 1 && res[st] == '0')
    st++;
  return (neg ? "-" : "") + res.substr(st);
}

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

  ll a; string b, c;
  cin >> a >> b >> c;
  bool neg;
  string diff = sub(b, c, neg);
  cout << divByLL(diff, a, neg) << "\n";

  return 0;
}