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

47-tgyuuAn #167

Merged
merged 2 commits into from
Apr 22, 2024
Merged

47-tgyuuAn #167

merged 2 commits into from
Apr 22, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Mar 22, 2024

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

๋„ˆ ๋ด„์—๋Š” ์บก์‚ฌ์ด์‹ ์ด ๋ง›์žˆ๋‹จ๋‹ค

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

40๋ถ„

(50์  ๊นŒ์ง€ ๋งž๊ณ  Large ์ผ€์ด์Šค๋Š” ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ )

  • ํ•ด๋‹น ๋ฌธ์ œ์— ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ, ๋ถ„ํ•  ์ •๋ณต ์ œ๊ณฑ ๊ณผ ๊ฐ™์€ ์žก๊ธฐ์ˆ ์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • ๋ชจ๋ฅผ ๊ฒฝ์šฐ ๋น ๋ฅธ PR ์ •๋… ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

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

์ผ๋‹จ ์ด ์ฝ”๋“œ๋กœ ๋ชจ๋“  ์กฐํ•ฉ์˜ ๊ฒฝ์šฐ๋ฅผ ์™„์ „ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ง๋„ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๋„ํ•ด๋ณด์ง€๋„ ์•Š์•˜๋‹ค.







๋ฌธ์ œ๋ฅผ ๊ณฐ๊ณฐํžˆ ์ฝ์–ด๋ณด๋‹ˆ, ๊ฒฐ๊ตญ ์ตœ์†Œ ๊ฐ’๊ณผ ์ตœ๋Œ€ ๊ฐ’๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์‚ฌ์ด์— ์žˆ๋Š” ์ˆ˜๋Š” ๋ฌด์—‡์ด ๋“ค์–ด์žˆ๋“  ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค๋ผ๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ฌ์•˜๋‹ค.







์ฆ‰, ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ˆ์ œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ,

6
1 4 5 5 6 10

์ตœ์†Œ๊ฐ’ 1 ์ตœ๋Œ€๊ฐ’ 10์ผ ๊ฒฝ์šฐ,

  • ๋‚ด๋ถ€์— 4๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ,

  • ์ฒซ ๋ฒˆ์งธ 5๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ,

  • ๋‘ ๋ฒˆ์งธ 5๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ,

  • 6์ด ์žˆ๋Š” ๊ฒฝ์šฐ์™€ ์—†๋Š” ๊ฒฝ์šฐ

์ด 2์˜ 4์Šน๊ฐœ ๋งŒํผ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ตœ๋Œ€๊ฐ’ 10 - ์ตœ์†Œ๊ฐ’ 1 = 9 ๊ฐ€ 16๊ฐœ (2์˜ 4์Šน๊ฐœ) ์žˆ์œผ๋ฏ€๋กœ 144๊ฐ€ ์Œ“์ธ๋‹ค.

image







์ฆ‰, ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 2๊ฐœ์˜ for๋ฌธ์œผ๋กœ ์™„ํƒ ๋Œ๋ฆฌ๋ฉด Small ์ผ€์ด์Šค๋Š” ํ†ต๊ณผ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

for start_idx in range(N-1):
    for end_idx in range(start_idx+1, N):
        # ์ƒ์„ธ ๋กœ์ง







ํ•˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด ํ’€ ๊ฒฝ์šฐ $O(n^2)$ ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋กœ ํ’€๋ฆฌ๊ฒŒ ๋˜๋Š”๋ฐ,

Large ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ n์ด 30๋งŒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋‚ด๋กœ ๋“ค์–ด์˜ค์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค.







์—ฌ๊ธฐ์„œ 30๋ถ„ ์ •๋„ ๊ณ ๋ฏผํ•˜๋ฉฐ ๋‡Œ์‚ฌ๊ฐ€ ์™”๋Š”๋ฐ,

๋ง๋„ ์•ˆ๋˜๋Š” ์ธ์‚ฌ์ดํŠธ๋ฅผ ๋ฐœ๊ฒฌํ•œ๋‹ค.







image

https://0902.tistory.com/60







ํ™€๋ฆฌ๋ชฐ๋ฆฌ..

๊ทธ๋ ‡๋‹ค.. ์• ์ดˆ์— ์ € ์ตœ์†Œ๊ฐ’ ์ตœ๋Œ€๊ฐ’์ด ์Œ์œผ๋กœ ์žˆ์„ ํ•„์š”๊ฐ€ ์—†๊ณ ,

์–ด์ฐจํ”ผ ์ตœ์†Œ ๊ฐ’์€ ๋นผ์งˆ๊ฑฐ๊ณ  ์ตœ๋Œ€ ๊ฐ’์€ ๋”ํ•ด์งˆ ๊ฒƒ์ด๋ฏ€๋กœ ํ•˜๋‚˜์˜ for๋ฌธ ๋งŒ์œผ๋กœ๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ญ”๊ฐ€ ๋ง์ด ์–ด๋ ค์šด๋ฐ...

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” 1์€ ๋’ค์— ์–ด๋–ค ์ˆ˜๊ฐ€ ์žˆ๋˜ ๊ฐ„์— ์ด $2^5 - 1$๊ฐœ ๋งŒํผ ์ตœ์†Œ ๊ฐ’์œผ๋กœ ๋™์ž‘ํ•  ๊ฒƒ์ด๋‹ค.

image







๋ฐ˜๋ฉด 10์€ $2^5-1$๊ฐœ์˜ ๊ฒฝ์šฐ ๋™์•ˆ ์ตœ๋Œ€ ๊ฐ’์œผ๋กœ ๋™์ž‘ํ•  ๊ฒƒ์ด๋‹ค.

์ฆ‰, ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•œ ๋ฒˆ์˜ ์ˆœํšŒ๋งŒ์œผ๋กœ ์ตœ์†Œ ๊ฐ’์€ ๋นผ๊ณ  ์ตœ๋Œ€ ๊ฐ’์€ ๋”ํ•ด์ฃผ๋ฉด Large ์ผ€์ด์Šค๋„ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ์ด๋‹ค.







์ถ”๊ฐ€์ ์œผ๋กœ ์ด ๋ฌธ์ œ๋Š” ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ ์ตœ๋Œ€ $2^31$ ๊นŒ์ง€ ๊ฐ€๋ฏ€๋กœ,

์ˆ˜๊ฐ€ ๋งค์šฐ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ์‚ฌ์น™์—ฐ์‚ฐ์ด๋”๋ผ๋„ ์‹œ๊ฐ„์ด ๋งค์šฐ ์ปค์ง„๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„์ค‘๊ฐ„ ์ˆ˜๋ฅผ ์ค„์—ฌ์ฃผ๋Š” ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ์ ์šฉ์‹œ์ผœ์ค˜์•ผ ํ•˜๋Š”๋ฐ,







์•„๋งˆ ํ™์ฃผ๋‹˜์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ถ„๋“ค์€ ์ด๋ฏธ ์•„์‹คํ…Œ๊ณ ,

ํ™์ฃผ๋‹˜์„ ์œ„ํ•ด์„œ ์งง๊ฒŒ๋‚˜๋งˆ ๋งํฌ๋ฅผ ๋‚จ๊ฒจ๋“œ๋ฆฌ๋ฆฌ๋‹ค.

์ด๊ฑด ์™ธ์šธ ์ˆ˜ ๋ฐ–์— ์—†์–ด์š”.

๊ทผ์˜ ๊ณต์‹์ฒ˜๋Ÿผ...

https://sskl660.tistory.com/75







๋˜ ์ œ๊ณฑ์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ปค์งˆ ๊ฒฝ์šฐ ์ œ๊ณฑ ์—ฐ์‚ฐ์—์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ํ„ฐ์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ,

์ด๊ฒƒ ๋˜ํ•œ ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์œผ๋กœ ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œ์ผœ์•ผํ•˜๋Š”๋ฐ,,,







๋ง๋กœ ์„ค๋ช…ํ•˜๋ฉด PR์ด ๋„ˆ๋ฌด ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™๋‹ค.

์ผ๋‹จ ํ•ต์‹ฌ์€ 2^1024๋ฅผ ํ•œ๋‹ค ํ–ˆ์„ ๋•Œ,

๊ทธ๋ƒฅ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ด๋ฅผ ๊ณ„์‹ผํ•˜๋ฉด 1024๋ฒˆ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜์ง€๋งŒ,







1024
= 512 * 512
= 256 * 256 * 256 * 256
= ...

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ณ„์† ๋ฐ˜์”ฉ ์ชผ๊ฐœ๋ฉด ๋‹จ 10๋ฒˆ๋งŒ์œผ๋กœ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์›๋ฆฌ์ด๋‹ค.

image

https://devlibrary00108.tistory.com/328







ํ™์ฃผ๋‹˜ ๋จธ๋ฆฌ๊ฐ€ ํ„ฐ์งˆ ๊ฒƒ ๊ฐ™๋‹ค.

๋„ˆ๋ฌด ์ฃ„์†กํ•˜๋‹ค.

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

๊ทผ๋ฐ ํŒŒ์ด์ฌ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•  ๊ฒฝ์šฐ ํฐ ์ˆ˜ ์ œ๊ณฑ ์—ฐ์‚ฐ์— ํ•จ์ˆ˜๋ฅผ ๊ตณ์ด ์ง์ ‘ ๋งŒ๋“ค ํ•„์š” ์—†๊ณ ,

# N์„ K ์ œ๊ณฑํ•  ๋•Œ Q๋กœ ๋‚˜๋ˆˆ ์ˆ˜
pow(N, K, Q)

๋กœ ์ด๋ฏธ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค.







์ฆ‰, ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋ณด๋‹ค ์•„๋ž˜์™€ ๊ฐ™์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ด ๋ฐ˜์ ˆ์ด๋‚˜ ์ค„์–ด๋“ ๋‹ค.

import sys

DIV = 1_000_000_007

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

N = int(input())
numbers = sorted(list(map(int,input().split())))
DP = [-1 for _ in range(N)]
answer = 0

for start_idx in range(N):
    start_num = numbers[start_idx]
    end_num = numbers[N-start_idx-1]

    # ๋งŒ์•ฝ ์บ์‹ฑ์ด ๋˜์–ด์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ง์ ‘ ๊ณ„์‚ฐ
    if DP[N-start_idx-1] == -1: DP[N-start_idx-1] = pow(2, N-start_idx-1, DIV)
    
    # ํ•œ๋ฒˆ์ด๋ผ๋„ ๊ณ„์‚ฐ  ํ–ˆ์œผ๋ฉด ๋ฐ”๋กœ ์ด์šฉ
    answer += ((end_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV
    answer -= ((start_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV

print(answer % DIV)

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Mar 22, 2024
@tgyuuAn tgyuuAn self-assigned this Mar 22, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review March 22, 2024 16:18
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.

์นœ์ ˆํ•œ ์ˆ˜๋„์ฝ”๋“œ ์„ค๋ช…๊ณผ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ์กฐ๊นŒ์ง€ .. ๐Ÿ˜ญ
์ดํ•ดํ•˜๋Š”๋ฐ ์•„์ฃผ ๋„์›€์ด ์ •~๋ง ๋งŽ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..!!!!!!>!>!>

๋ฌธ์ œ ์žฌ๋ฐŒ๋„ค์š” ใ„ทใ„ท ์ด๋Ÿฐ ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฌ๊ธฐ๊นŒ์ง€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผํ• ์ง€ ..ใ…Ž

๋ฌธ์ œํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šด๋‹ค !!!๐Ÿ’ช๐Ÿ’ช

Comment on lines +30 to +31
answer += ((end_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV
answer -= ((start_num % DIV) * (DP[N-start_idx-1] % DIV)) % DIV
Copy link
Collaborator

Choose a reason for hiding this comment

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

ํƒœ๊ทœ๋‹˜ ์ด ์ฝ”๋“œ๊ฐ€ end_num๊ณผ start_num์— ๋Œ€ํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ํ•ฉ๊ณผ ์ตœ์†Ÿ๊ฐ’์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ํ•ฉ์„ ๊ฐ๊ฐ answer์—์„œ ๋”ํ•˜๊ณ  ๋นผ๋Š” ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ ์™œ %DIV๋ฅผ ๊ณ„์† ํ•˜๋Š”๊ฑด์ง€ ์„ค๋ช…ํ•ด ์ฃผ์‹ค ์ˆ˜ ์žˆ์„๊นŒ์š”ใ… ? ์ดํ•ด๊ฐ€ ๋ ๋ž‘ ๋ง๋ž‘ํ—ˆ๋„ค์š” ..๐Ÿ˜…๐Ÿค”

๋ผ๊ณ  ์งˆ๋ฌธํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ฒจ๋ถ€ํ•ด์ฃผ์‹  mod ์—ฐ์‚ฐ๋ณด๊ณ  ์ดํ•ดํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค. ํ—ˆํ—ˆ

(end_num * DP[N-start_idx - 1]) % DIV๋ฅผ ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๊ฐ€ end_num*DP๊ฐ€ ํ‘œํ˜„ ๊ฐ€๋Šฅํ•œ ์ˆ˜๋ฅผ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— !! ๋งž์„๊นŒ์š”?

Copy link
Member Author

Choose a reason for hiding this comment

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

image

ํ™์ฃผ๋‹˜๊ณผ ์›๋งŒํ•œ ๋Œ€ํ™”๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜์˜€์”๋‹ˆ๋‹ค -

@pknujsp
Copy link
Collaborator

pknujsp commented Mar 30, 2024

๋ถ„ํ• ์ •๋ณต์ œ๊ณฑ์€ ์‚ด๋ฉด์„œ ์ด ๋ฌธ์ œ ํ†ตํ•ด์„œ ์ฒ˜์Œ ๋ดค๋„ค์š”
์˜ค๋ž˜ ์ƒ๊ฐํ•ด๋ดค์ง€๋งŒ ๋„์ €ํžˆ ๋‹ต์ด ์•ˆ๋‚˜์™€์„œ ํƒœ๊ทœ๋‹˜ ํ’€์ด๋ž‘ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ๋‚ด์šฉ ๋ณด๋ฉด์„œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค
ํ‘ธ๋Š” ์•„์ด๋””์–ด๊ฐ€ ์ฒœ์žฌ๋“ค์ธ๊ฐ€ ์žฅ๋‚œ์•„๋‹ˆ๋„ค์š”

MOD = 1_000_000_007

N = int(input())
scovilles = list(map(int, input().split()))
scovilles.sort()

total_pain_index = 0

for i, scoville in enumerate(scovilles):
    min_contribution = pow(2, N - i - 1, MOD)
    max_contribution = pow(2, i, MOD)
    contribution = (max_contribution - min_contribution) * scoville
    
    total_pain_index = (total_pain_index + contribution) % MOD
    
print(total_pain_index)

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Mar 30, 2024

๋ถ„ํ• ์ •๋ณต์ œ๊ณฑ์€ ์‚ด๋ฉด์„œ ์ด ๋ฌธ์ œ ํ†ตํ•ด์„œ ์ฒ˜์Œ ๋ดค๋„ค์š” ์˜ค๋ž˜ ์ƒ๊ฐํ•ด๋ดค์ง€๋งŒ ๋„์ €ํžˆ ๋‹ต์ด ์•ˆ๋‚˜์™€์„œ ํƒœ๊ทœ๋‹˜ ํ’€์ด๋ž‘ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ๋‚ด์šฉ ๋ณด๋ฉด์„œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค ํ‘ธ๋Š” ์•„์ด๋””์–ด๊ฐ€ ์ฒœ์žฌ๋“ค์ธ๊ฐ€ ์žฅ๋‚œ์•„๋‹ˆ๋„ค์š”

MOD = 1_000_000_007

N = int(input())
scovilles = list(map(int, input().split()))
scovilles.sort()

total_pain_index = 0

for i, scoville in enumerate(scovilles):
    min_contribution = pow(2, N - i - 1, MOD)
    max_contribution = pow(2, i, MOD)
    contribution = (max_contribution - min_contribution) * scoville
    
    total_pain_index = (total_pain_index + contribution) % MOD
    
print(total_pain_index)

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์ €๋„ small ์ผ€์ด์Šค๋ฐ–์— ๋ชป๋งž์ท„์Šต๋‹ˆ๋‹ค. Large์ผ€์ด์Šค๋Š” ์ง„์งœ ์ฒœ์žฌ๋“ค์ธ ๊ฒƒ ๊ฐ™์•„์š”..

@MunbinLee
Copy link
Contributor

MunbinLee commented Apr 22, 2024

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

constexpr long long MOD = 1'000'000'007;
unordered_map<int, long long> memo{{0, 1}};

long long power(int x) { // NOLINT
  if (memo.contains(x))
    return memo[x];

  if (x % 2)
    return memo[x] = power(x - 1) * 2 % MOD;

  return memo[x] = power(x / 2) * power(x / 2) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;

  vector<int> foods(N);

  for (int &food : foods) {
    cin >> food;
  }

  ranges::sort(foods);

  long long answer = 0;

  for (int i = 0; i < N; i++) {
    answer += foods[i] * (power(i) - power(N - 1 - i));
    answer %= MOD;
  }

  cout << answer;

  return 0;
}

์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ณ ์ •ํ•˜๋ฉด ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜ค๋ฏ€๋กœ ์ด๋ฅผ ์‘์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค ํ–ˆ์œผ๋‚˜ ๊ทธ๋ ‡๊ฒŒ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์„œ ๋‘ ๋ฒˆ์งธ ์ผ€์ด์Šค๋Š” ํ’€๊ธฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

image

์ด ๋ถ€๋ถ„ ๋ฐœ์ƒ ์ง€๋ฆฌ๋Š”๋ฐ์š” ใ…Žใ„ทใ„ทใ„ท

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Apr 22, 2024

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

constexpr long long MOD = 1'000'000'007;
unordered_map<int, long long> memo{{0, 1}};

long long power(int x) { // NOLINT
  if (memo.contains(x))
    return memo[x];

  if (x % 2)
    return memo[x] = power(x - 1) * 2 % MOD;

  return memo[x] = power(x / 2) * power(x / 2) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;

  vector<int> foods(N);

  for (int &food : foods) {
    cin >> food;
  }

  ranges::sort(foods);

  long long answer = 0;

  for (int i = 0; i < N; i++) {
    answer += foods[i] * (power(i) - power(N - 1 - i));
    answer %= MOD;
  }

  cout << answer;

  return 0;
}

์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ณ ์ •ํ•˜๋ฉด ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜ค๋ฏ€๋กœ ์ด๋ฅผ ์‘์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค ํ–ˆ์œผ๋‚˜ ๊ทธ๋ ‡๊ฒŒ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์„œ ๋‘ ๋ฒˆ์งธ ์ผ€์ด์Šค๋Š” ํ’€๊ธฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

image

์ด ๋ถ€๋ถ„ ๋ฐœ์ƒ ์ง€๋ฆฌ๋Š”๋ฐ์š” ใ…Žใ„ทใ„ทใ„ท

์ธ์ •๋”ฐ๋ฆฌ์ž…๋‹ˆ๋‹ค...

@tgyuuAn tgyuuAn merged commit 124514a into main Apr 22, 2024
@tgyuuAn tgyuuAn deleted the 47-tgyuuAn branch April 22, 2024 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants