[백준 31261] НАМИСЛИХ СИ ЧИСЛО (C#, C++) - soo:bak
작성일 :
문제 링크
설명
어떤 수 x에 대해 a로 나누고 a를 빼는 연산을 두 번 반복하면 b가 되는 조건이 주어질 때, 원래 수 x를 구하는 문제입니다.
접근법
연산을 역으로 추적하면 됩니다. b에서 시작하여 a를 더하고 a를 곱하는 연산을 두 번 반복하면 x가 됩니다.
식을 전개하면 x는 a의 제곱에 b + a + 1을 곱한 값이 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
using System;
class Program {
static void Main() {
var parts = Console.ReadLine()!.Split();
var a = int.Parse(parts[0]);
var b = int.Parse(parts[1]);
var ans = a * a * (b + a + 1);
Console.WriteLine(ans);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b; cin >> a >> b;
int ans = a * a * (b + a + 1);
cout << ans << "\n";
return 0;
}