Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18-janghw0126 #188

Merged
merged 13 commits into from
Nov 21, 2024
Merged

18-janghw0126 #188

merged 13 commits into from
Nov 21, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Nov 12, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | ์นด์ž‰ ๋‹ฌ๋ ฅ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๐Ÿ‘‰๋ฌธ์ œ ์ฝ”๋“œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ๋‘ ํ•ด์˜ ์ฃผ๊ธฐ์—์„œ ๊ฐ๊ฐ์˜ ํŠน์ • ์—ฐ๋„๊ฐ€ ๋™์‹œ์— ๋ช‡๋ฒˆ ์งธ ํ•ด์— ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‘ ํ•ด์˜ ์ฃผ๊ธฐ์™€ ์‹œ์ž‘ ์—ฐ๋„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์›ํ•˜๋Š” ์—ฐ๋„๋ฅผ ์ฐพ๋Š” ๋กœ์ง์„ ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๐Ÿ‘€๋ฌธ์ œ ํ•ด๊ฒฐ ๋กœ์ง

1. year ๋ณ€์ˆ˜๋ฅผ target_x๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • max_year1 ์ฃผ๊ธฐ์—์„œ target_x์—ฐ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์‚ฐ์„ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
year = target_x

2. ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ตœ๋Œ€ ๊ฐ€๋Šฅํ•œ ์—ฐ๋„๊นŒ์ง€ ๋ฐ˜๋ณต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ๋‘ ์ฃผ๊ธฐ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ์ดํ•˜์˜ ํ•ด๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์—ฐ๋„๋ฅผ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค.
  • year๊ฐ€ target_y์™€ ์ผ์น˜ํ•  ๋•Œ๊นŒ์ง€ max_year1 ์ฃผ๊ธฐ๋ฅผ ๋”ํ•˜๋ฉด์„œ ๋‹ค์Œ ์—ฐ๋„๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋•Œ, (year - target_y) % max_year2 == 0 ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด year๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์›ํ•˜๋Š” ํ•ด๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

Note

๐Ÿง ์—ฌ๊ธฐ์„œ ์™œ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ์ดํ•˜์˜ ํ•ด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š๋ƒ?

max_year1๊ณผ max_year2๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š” ์ฃผ๊ธฐ์„ฑ์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, max_year1 = 10๊ณผ max_year2 = 12๋ผ๋ฉด, ์ด ๋‘ ์ฃผ๊ธฐ๊ฐ€ ๋™์‹œ์— ๋Œ์•„์™€ ๊ฐ™์€ ํ•ด๊ฐ€ ๋˜๋Š” ์ฃผ๊ธฐ๋Š” ์ตœ์†Œ 60๋…„(์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜)๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‘ ์ฃผ๊ธฐ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ณด๋‹ค ํฐ ๋ฒ”์œ„์—์„œ๋Š” ์ด๋ฏธ ๊ฐ™์€ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๊นŒ์ง€๋งŒ ํƒ์ƒ‰ํ•˜๋ฉด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

while year <= max_year1 * max_year2:
        if (year - target_y) % max_year2 == 0:

3. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ year๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•˜๊ณ , ์•„๋‹ˆ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

        year += max_year1
    return -1

์˜ˆ๋ฅผ ๋“ค์–ด,
max_year1 = 10, max_year2 = 12, target_x = 3, target_y = 9์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.

  1. ์—ฌ๊ธฐ์„œ year๋Š” 3๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ  10์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

  2. ๊ทธ ํ›„ year = 3, 13, 23, ...์— ๋Œ€ํ•ด (year - 9) % 12 == 0 ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  3. ์กฐ๊ฑด์„ ํ™•์ธํ•˜๋‹ค๊ฐ€ 63 ํ•ด์—์„œ (63 - 9) % 12 == 0์„ ๋งŒ์กฑํ•˜๋ฏ€๋กœ 63์ด ๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ‘ฉโ€๐Ÿ’ป์ตœ์ข… ์ฝ”๋“œ

def find_year(max_year1, max_year2, target_x, target_y):
    year = target_x
    while year <= max_year1 * max_year2:
        if (year - target_y) % max_year2 == 0:
            return year
        year += max_year1
    return -1

T = int(input())
for _ in range(T):
    max_year1, max_year2, target_x, target_y = map(int, input().split())
    print(find_year(max_year1, max_year2, target_x, target_y))

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

.

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ์— M * ?? + x = N * ??? + y = ๋‹ต ์ด๋ ‡๊ฒŒ ์‹์„ ์ ์–ด๋ณด๋‹ˆ ๋ชจ๋ฅด๋Š” ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ
x๋ฅผ ๊ณ ์ •ํ•˜๊ณ  y๋งŒ ๋ณ€ํ™”์‹œ์ผœ์„œ x์— ๋งž๋Š” y ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค!

์ข…๋ง์ด ์˜ค๋Š” ์—ฐ๋„๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด N, M์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ํƒ์ƒ‰์ด ํ•„์š”ํ•ด์„œ ์ €๋Š” ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์œผ๋กœ ์ตœ์†Œ๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค!

์ด๋ฒˆ ์ฐจ์‹œ๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    int T = Integer.parseInt(bf.readLine());
    StringBuilder printSet = new StringBuilder();

    // ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐฏ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต
    while(T > 0) {
      // ์ž…๋ ฅ ๋ฐ์ดํ„ฐ: M, N, x, y
      StringTokenizer st = new StringTokenizer(bf.readLine());
      int M = Integer.parseInt(st.nextToken());
      int N = Integer.parseInt(st.nextToken());
      int x = Integer.parseInt(st.nextToken());
      int y = Integer.parseInt(st.nextToken());
      T--;

      // ์„ธ์ƒ์˜ ์ข…๋ง์ด ์˜ค๋Š” ํ•ด ๊ตฌํ•˜๊ธฐ
      // ๋งˆ์ง€๋ง‰ํ•ด = M, N์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜
      int lastYear = (M*N) / gdc(M, N);

      // x์— ๋งž์ถฐ์„œ y ๊ตฌํ•˜๊ธฐ
      int year = x;
      int checkY = ((year-1) % N) + 1;
      while(checkY != y) {
        year += M;
        checkY = ((year-1) % N) + 1;

        // ์„ธ์ƒ์˜ ์ข…๋ง์ด ์ง€๋‚ฌ์œผ๋ฉด ์ข…๋ฃŒ
        if(year > lastYear) {
          year = -1;
          break;
        }
      }

      printSet.append(year).append("\n");
    }
    
    System.out.print(printSet);
  }

  // ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์„ ์ด์šฉํ•œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ๊ณ„์‚ฐ
  static int gdc(int a, int b) {
    // ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์€ a>b์ผ ๋•Œ๋งŒ ์„ฑ๋ฆฝํ•˜๋ฏ€๋กœ
    if(a < b) {
      int temp = a;
      a = b;
      b = temp;
    }
    while(b != 0) {
      int r = a%b;
      a = b;
      b = r;
    }

    return a;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants