알고리즘

[알고리즘] 백준 2858 - 기숙사 바닥 (C++)

blueberrysoda 2025. 2. 27. 00:52

[문제 링크] : https://www.acmicpc.net/problem/2858

#include <iostream>
using namespace std;

int R, B, S;
bool flag;

void solve(int cnt, int tmp){
    int r = 0, b = 0;

    for(int i=0; i<tmp; i++){
        for(int j=0; j<cnt; j++){
            if(i == 0 || j == 0 || i == tmp - 1 || j == cnt - 1){
                r++;
            }
            else{
                b++;
            }
        }
    }

    if(r == R && b == B){
        flag = true;
        return;
    }
    return;
}

int main(){
    cin >> R >> B;
    S = R + B;

    int l = 1;

    while(true){
        if(S % l == 0){
            int tmp = l;
            int cnt = S / l;
            solve(tmp, cnt);

            if(flag == true){
                cout << cnt << " " << tmp << "\n";
                return 0;
            }
        }
        l++;
    }

    return 0;
}