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

4 yuyu0830 #18

Merged
merged 2 commits into from
Mar 29, 2024
Merged

4 yuyu0830 #18

merged 2 commits into from
Mar 29, 2024

Conversation

yuyu0830
Copy link
Collaborator

@yuyu0830 yuyu0830 commented Mar 26, 2024

πŸ”— 문제 링크

ν–‰λ ¬ 제곱

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

λΈ”λ‘œκ·Έ 풀이

μ •λ³΄λ³΄ν˜Έλ‘ μ—μ„œ μ•”ν˜Έλ₯Ό κ΅¬ν˜„ν•  λ•Œ μ‚¬μš©ν•˜λŠ” Double-and-Add Method λΌλŠ” 방식을 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

숫자 n을 b번 μ œκ³±ν•  λ•Œ, n을 b번 직접 λ‹€ κ³±ν•˜λŠ” 방법도 μžˆμŠ΅λ‹ˆλ‹€.

ν•˜μ§€λ§Œ (n*n)^2 μ΄λ ‡κ²Œ μ§€κΈˆ κ³±ν•˜κ³  μžˆλŠ” 수 자체λ₯Ό μ œκ³±ν•΄ 연산을 쀄일 수 μžˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ 기쀀이 λ˜λŠ” λ°°μ—΄ crit, ν˜„μž¬ 수λ₯Ό μ €μž₯ν•˜λŠ” λ°°μ—΄ arr λ‘κ°œλ₯Ό 두어 n을 κ³±ν•  λ•ŒλŠ” arr * crit, 즉 Addingλ₯Ό ν•˜κ³  , 자체λ₯Ό κ³±ν•  λ•ŒλŠ” arr * arr, 즉 Doublingλ₯Ό μ‹€ν–‰ν•΄ μ—°μ‚°ν–ˆμŠ΅λ‹ˆλ‹€.

Double-and-Add MethodλŠ” 2μ§„μˆ˜κ°€ 제일 μ•žμ— bitλ₯Ό μ œμ™Έν•œ λ’€, μ•žμ—μ„œλΆ€ν„° 1이면 * 2 + 1, 0이면 * 2 λ₯Ό μˆ˜ν–‰ν•΄ ν•΄λ‹Ή 수λ₯Ό λ§Œλ“€ 수 μžˆλ‹€λŠ” 점에 μ°©μ•ˆν–ˆμŠ΅λ‹ˆλ‹€.

image
26을 κ³„μ‚°ν•œ μ˜ˆμ‹œ

bit wise 연산을 톡해 각 bit별 수λ₯Ό 확인해 Doubling와 Adding μ—°μ‚° μ—¬λΆ€λ₯Ό κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€. μ΄ν›„λŠ” 1000 λ‚˜λ¨Έμ§€ μ—°μ‚°λ§Œ ν•΄μ£Όλ©΄ 끝!

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

bit shiftλŠ” ν•œ λ²ˆμ— 16 이상 λͺ»ν•©λ‹ˆλ‹€! b 번 μ œκ³±ν•΄μ•Όν•˜κ³ , bκ°€ μ΅œλŒ€ 100,000,000,000 λΌμ„œ 1 << 37 둜 μ“°λ €κ³  ν–ˆλŠ”λ° 자꾸 였λ₯˜κ°€ λœ¨λ„€μš”....

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

풀이가 μ–΄λ ΅μ§€λ§Œ bitλ‘œλ„ ν’€ 수 있ꡰ용...
ν•΄λ‹Ή 문제λ₯Ό μ²˜μŒλ΄€μ„λ•Œ ν–‰λ ¬ 제곱으둜만 λ²…μ°ΌλŠ”λ° μ‹œκ°„ 초과둜 λΈ”λ‘œκ·Έλ₯Ό μ°Έκ³ ν•΄μ„œ 제곱 μƒλž΅μ΄ κ°€λŠ₯ν•˜λ‹¨κ±Έ κΉ¨λ‹¬μ•˜μ„λ•Œ μ‹ μ„ ν–ˆλ˜ λŠλ‚Œμ΄ κΈ°μ–΅λ‚˜λ„€μš”.
μ €λŠ” bκ°€ ν™€μˆ˜ μΌλ•Œ λ‹¨μœ„ν–‰λ ¬μ— μ œκ³±μ„ ν•΄μ€€λ’€ ν–‰λ ¬ 제곱으둜 ν’€μ–΄λ‚˜κ°”μ—ˆλŠ”λ° μ‹ κΈ°ν•˜λ„€μš”!

}
}

long long int pointer = 137438953472; // 2^38
Copy link
Collaborator

Choose a reason for hiding this comment

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

pointer λ³€μˆ˜μ— μ™œ ν•΄λ‹Ή 값이 λ“€μ–΄κ°€λŠ”μ§€ μ•Œ 수 μžˆμ„κΉŒμš”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

졜고 값이 100,000,000,000 이고, ν•΄λ‹Ή 값보닀 더 큰 2의 제곱 수 쀑에 κ°€μž₯ μž‘μ€ μˆ˜κ°€ 2^37? 38? 인 137,438,953,472 μ˜€μŠ΅λ‹ˆλ‹€! 1 << 63 ν•΄μ„œ μ™„μ „ μ²˜μŒλΆ€ν„° 비ꡐ해도 λ˜λŠ”λ° μ–΄μ°¨ν”Ό 졜고 값이 100,000,000,000μ΄λ‹ˆκΉŒ 쑰건보닀 큰 수 쀑에 κ°€μž₯ μž‘μ€ 수인 2^37? 38? 을 κ³¨λžμŠ΅λ‹ˆλ‹€ :)

long long int pointer = 137438953472; // 2^38

// b와 비ꡐ할 수 μžˆλŠ” μœ„μΉ˜λ‘œ 포인터 μ„ΈνŒ…
while (!(b & pointer)) pointer = pointer >> 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 뢀뢄도 κΆκΈˆν•©λ‹ˆλ‹€! μ„€λͺ… λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

image

image

이런 μ‹μœΌλ‘œ pointer을 1μ”© bit shift ν•΄λ‚˜κ°€λ©° b와 bit AND 연산을 ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€!
그런데 b의 값이 μ–΄μ°¨ν”Ό μ΅œλŒ€κ°€ 100,000,000,000 이기 λ•Œλ¬Έμ— ν•΄λ‹Ή 값인 2^37? 38? 인 137,438,953,472 이 κ°’λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€!

image

κ·Έλ ‡κ²Œ ν•˜λ©΄ 2^37? 38? λΆ€ν„° bit shiftλ₯Ό ν•˜λ©° bit AND 비ꡐλ₯Ό ν•  텐데 μ§€κΈˆ λ‚˜μ˜€λŠ” 0이 b의 크기 상 영ν–₯이 μ—†λŠ” 0인지, μ•„λ‹ˆλ©΄ bλ₯Ό μ΄λ£¨λŠ” 2μ§„μˆ˜μ— ν¬ν•¨λœ 0인지 μ•Œ 방도가 μ—†μŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ 처음 1이 λ‚˜μ˜¬ λ•Œ κΉŒμ§€ pointerλ₯Ό >> 1 shiftν•΄μ„œ 당겨와 연산을 μ‹œμž‘ν•©λ‹ˆλ‹€

image

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

cn = []
while True:
  if b==1:
    break
  elif b%2==0: # 짝수
    b = b//2
    cn.append(1)
  else:
    b = (b-1)//2
    cn.append(0)

t = A
for i in range(len(cn)-1,-1,-1):
  if cn[i] == 1:
    A = matrixmult(A,A)
  else:
    C = matrixmult(A,A)
    A = matrixmult(t,C)

μ˜ˆμ „μ— ν’€μ—ˆλ˜ 문제인데 bκ°€ 짝수일 λ•Œμ™€ ν™€μˆ˜μΌ λ•Œλ₯Ό λ‚˜λˆ„μ–΄μ„œ ν’€μ—ˆλ˜ 기얡이 λ‚˜λ„€μš”. λΉ„νŠΈλ₯Ό μ΄μš©ν•œ μ°Έμ‹ ν•œ 풀이 잘 λ΄€μŠ΅λ‹ˆλ‹€ !

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

int fac(int b) {
	if (b==1) return a % c;
	long long int m = fac(b / 2);
	if (b % 2 == 0) return m * m % c;
	else  return (m * m %c * a % c) % c;
}

저도 μœ μ‚¬ν•œ 뢄할정볡 κ³±μ…ˆλ¬Έμ œλ₯Ό ν‘Όμ μ΄μžˆλŠ”λ° μ €λŠ” μ§€μˆ˜λΆ€λΆ„μ„ 계속 2둜 λ‚˜λˆ μ€˜μ„œ μž¬κ·€ν˜ΈμΆœμ„ ν†΅ν•œ λ°©μ‹μœΌλ‘œ ν’€μ—ˆλŠ”λ° μ΄λ ‡κ²Œ λΉ„νŠΈμ—°μ‚°μœΌλ‘œλ„ ν’€μˆ˜μžˆκ΅°μš” 쒀더 κ³΅λΆ€ν•΄μ•Όκ² μŠ΅λ‹ˆλ‹€,,

@yuyu0830 yuyu0830 merged commit 4fcb1b7 into main Mar 29, 2024
1 check passed
@yuyu0830 yuyu0830 deleted the 4-yuyu0830 branch March 29, 2024 01:09
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.

5 participants