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

28-alstjr7437 #203

Merged
merged 5 commits into from
Jun 14, 2024
Merged

28-alstjr7437 #203

merged 5 commits into from
Jun 14, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented May 30, 2024

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

์นด์ž‰ ๋‹ฌ๋ ฅ

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

30๋ถ„

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

0๋ถ€ํ„ฐ k๋ฒˆ์งธ์˜ ํ•ด๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋”ฑ๋ด๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•„์„œ
k๋ฅผ ์ฐพ๋Š” ์‹์„ ์ฐพ์•„์•ผ ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.







๊ทธ๋ž˜์„œ ํŠน์ • ์ •์ˆ˜๋ก ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋ชป์ฐพ๊ฒ ์–ด์„œ ์•Œ์•„๋ณธ ๋ถ€๋ถ„์ด

M๊ณผ N์˜ ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜ ์ฐพ๊ธฐ ์˜€์Šต๋‹ˆ๋‹ค!!





์œ„ ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋‚˜์˜จ ๋ถ€๋ถ„์ด ์•„๋ž˜ ์ฝ”๋“œ์ด๊ณ 

  1. (k-x) % m = 0
  2. (k-y) % n = 0

๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ while ๋ฌธ์— m์„ ๊ณ„์† ๋”ํ•ด์ฃผ๋ฉด์„œ if ๋ฌธ์œผ๋กœ ๋‘๊ฐ€์ง€ ๋‹ค ๋งŒ์กฑํ•˜๋ฉด break๋กœ ํƒˆ์ถœํ•˜๊ฒŒ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.




๐Ÿ’ก ์˜ˆ

์œ„์˜ ์‹์„ ๋Œ€์ž…ํ•ด๋ณด๋ฉด
k = 33์ธ ๊ฒฝ์šฐ, m,n,x,y -> 10, 12, 3, 9
(33 - 3) % 10 = 0
(33 - 9) % 12 = 0

์ด๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!!!

๋งŒ์•ฝ์— ์—†์œผ๋ฉด -1์ธ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋˜๊ณ !!!







๐Ÿง ์ฝ”๋“œ

import sys

input = sys.stdin.readline

t = int(input())

for _ in range(t):
    m, n, x, y = map(int, input().split())

    k = x  # k๋ฅผ x๋กœ ์ดˆ๊ธฐํ™”
    result = -1  
    while k <= m * n:  # k์˜ ๋ฒ”์œ„๋Š” m*n์„ ๋„˜์„ ์ˆ˜ ์—†๊ฒŒ
        if (k - x) % m == 0 and (k - y) % n == 0: 
            result = k  
            break  
        k += m 

    print(result)  # ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ

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

์‚ฌ์‹ค ์ฒ˜์Œ์— ๋ฌธ์ œ๊ฐ€ ์–ด๋–ค์‹์ธ์ง€ ์ดํ•ด๋ฅผ ๋ชปํ•ด์„œ ๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ๊นŒ ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ๋˜๊ตฌ๋‚˜!
ํ•˜๊ณ  ํ’€์–ด์„œ ์กฐ๊ธˆ ์•„์‰ฌ์› ์Šต๋‹ˆ๋‹ค.. ์ˆ˜ํ•™๋ฌธ์ œ๋„ ์ฐธ ์–ด๋ ต๋‹ค๊ณ  ๋Š๊ผ‡์–ด์š”..

์ค‘๊ตญ์ธ์˜ ๋‚˜๋จธ์ง€ ์ •๋ฆฌ ๋ผ๋Š”๊ฑธ ์•Œ์•„์„œ ํ•œ๋ฒˆ ๋‹ค๋ฅธ ๋ฌธ์ œ๋„ ํ’€์–ด๋ณผ๊นŒ ์ƒ๊ฐ ์ค‘์ž…๋‹ˆ๋‹ค!

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

์Œ.. ์•ˆ๋ณด๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•ด ๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์‹คํŒจํ–ˆ๋„ค์š”. ๋ฏผ์„๋‹˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter

private lateinit var br: BufferedReader
private lateinit var bw: BufferedWriter

fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    bw = BufferedWriter(OutputStreamWriter(System.out))

    val T = br.readLine().toInt()
    repeat(T) {
        val (m, n, x, y) = br.readLine().split(" ").map { it.toInt() }
        bw.write(program(m, n, x, y).toString())
        bw.newLine()
    }
    bw.flush()
    bw.close()
}

private fun program(m: Int, n: Int, x: Int, y: Int): Int {
    var number = x
    while (true) {
        if ((number - x) % m == 0 && (number - y) % n == 0) return number
        number += m
        if (number > m * n) return -1
    }
}

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ์–ด์ฐจํ”ผ M๊ณผ N์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ์ด์ƒ์€ ํƒ์ƒ‰ํ• ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•œ ๋’ค,

M์˜ ๋ฐฐ์ˆ˜ + x ๊ฐ’์„ N์˜ ๋ฐฐ์ˆ˜ + y๊ฐ’์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ง€ ํƒ์ƒ‰ํ•ด์„œ ํ’€์—ˆ์–ด์š”!

import math
import sys

def input(): return sys.stdin.readline().rstrip()

T = int(input())
for _ in range(T):
    M, N, x, y = map(int, input().split())

    gcd = math.gcd(M, N)
    lcm = M*N//gcd

    for can_number in range(x, lcm+1, M):
        if (can_number-y)%N==0:
            print(can_number)
            break
        
    else: print(-1)

ํŒŒ์ด์ฌ์—์„œ๋Š” math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ์ง€์›ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๊ฟ€์ž…๋‹ˆ๋‘ฅ

์ด์ œ ์ด ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ ์ˆ˜์˜ ๊ณฑ // ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ํ•˜๋ฉด ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๊ฐ€ ๋‚˜์™€์š”!

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

LCM ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์€๋ฐ? ํ•ด์„œ ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ํ•ด๊ฒฐ ๋ชปํ•ด์„œ PR์„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹น..

์ •์ˆ˜๋ก  ๋ฌธ์ œ์˜€๊ตฐ์šฉ ์ค‘๊ตญ์ธ์˜ ๋‚˜๋จธ์ง€ ์ •๋ฆฌ๋ผ.. ํ—ˆํ—ˆ ์ฐธ๊ณ ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค !!
์ •์ˆ˜๋ก ๋„ ํ•œ๋ฒˆ ๋ฌธ์ œ ์ญ‰ ๋‘˜๋Ÿฌ๋ด์•ผ๊ฒ ๋„ค์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€ํ• ๊ฑฐ +1 ์ ๋ฆฝํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!!

import sys
import math

def input() : return sys.stdin.readline().rstrip()

T = int(input())
arr = [list(map(int, input().split())) for _ in range(T)]
result = [-1] * T
# M N x y

for i in range(T):
    M, N, x, y = arr[i]
    m = 1
    n = 1
    tmp = 0
    check = False

    while x <= int(M*N/math.gcd(M,N)):
        if (x - 1) % N + 1 == y:
            result[i] = x
            check = True
            break
        x += M 

print(' '.join(map(str, result)))

@alstjr7437 alstjr7437 merged commit 40c3d2e into main Jun 14, 2024
6 checks passed
@alstjr7437 alstjr7437 deleted the 28-alstjr7437 branch June 14, 2024 11:44
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.

4 participants