ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1024๋ฒˆ: ์ˆ˜์—ด์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— N๊ณผ L์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1,000,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , L์€ 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1024
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, l int
	fmt.Fscanln(reader, &n, &l)

	start := n/l - l/2
	var sum int
	for {
		if start < 0 {
			start++
		}
		sum = 0
		for i := start; i < start+l; i++ {
			sum += i
		}
		if sum < n {
			start++
		} else if sum == n {
			if start < 0 {
				fmt.Fprintln(writer, -1)
				return
			}
			break
		} else if sum > n {
			l++
			if l > 100 {
				fmt.Fprintln(writer, -1)
				return
			}
			start = n/l - l/2
		}
	}
	for i := start; i < start+l; i++ {
		fmt.Fprintf(writer, "%d ", i)
	}
	fmt.Fprintln(writer, "")
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1024
import sys

if __name__ == "__main__":
    n, l = list(map(int, sys.stdin.readline().split()))

    start = n//l - l//2
    total = 0
    is_valid = True
    while True:
        if start < 0:
            start += 1
        total = 0
        for i in range(start, start+l):
            total += i
        if total < n:
            start += 1
        elif total == n:
            if start < 0:
                is_valid = False
                break
            break
        elif total > n:
            l += 1
            if l > 100:
                is_valid = False
                break
            start = n//l - l//2
    if is_valid:
        for i in range(start, start+l):
            print("{} ".format(i), end='')
        print()
    else:
        print(-1)
728x90
๋Œ“๊ธ€