-
Notifications
You must be signed in to change notification settings - Fork 1
11-hadongun #44
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
11-hadongun #44
Conversation
|
μ λ DPμ νλ ¬μ μ΄μ©ν΄μ νμ΄λ΄€μ΅λλ€. λ¨Όμ κ°μ₯ μΌμͺ½μ μ΄λ€ νμΌμ λλμ λ°λΌμ λκ°μ§ μΌμ΄μ€λ‘ λλ©λλ€. κ°λ‘κ° 1μ΄κ³ μΈλ‘κ° 2μΈ νμΌμ λλ κ²½μ°λ μ€λ₯Έμͺ½μ κ°λ‘ 2Γn μ§μ¬κ°νμ 1Γ2, 2Γ1 νμΌλ‘ μ±μ°λ λ°©λ²μ μλ₯Ό import sys
input = sys.stdin.readline
def fibonacci(n):
mat = [[1, 1], [1, 0]]
vec = [1, 0]
while n > 0:
if n & 1:
temp = (mat[0][0] * vec[0] + mat[0][1] * vec[1]) % 10007
vec[1] = (mat[1][0] * vec[0] + mat[1][1] * vec[1]) % 10007
vec[0] = temp
mat00 = (mat[0][0] * mat[0][0] + mat[0][1] * mat[1][0]) % 10007
mat01 = (mat[0][0] * mat[0][1] + mat[0][1] * mat[1][1]) % 10007
mat10 = (mat[1][0] * mat[0][0] + mat[1][1] * mat[1][0]) % 10007
mat11 = (mat[1][0] * mat[0][1] + mat[1][1] * mat[1][1]) % 10007
mat[0][0], mat[0][1] = mat00, mat01
mat[1][0], mat[1][1] = mat10, mat11
n >>= 1
return vec[1]
print(fibonacci(int(input())+1)%10007)+μμ μ DPλ₯Ό μ΄μ©ν νΌλ³΄λμΉ ν¨μ μ½λκ° μμ΄μ, μ΄μ§ μμ ν΄μ κ°μ Έμλ΄€μ΅λλ€ γ γ DP μ½λ
|
dohyeondol1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈ...μ‘°ν©μ νμ©ν νμ΄λΌλ μλ‘λ€μ..
μ λ DPλ‘ ν΄κ²°νμ΅λλ€.
μμ μ
λ ₯μμ 9κ° 55λΌλ μΆλ ₯μ κ°μ§μ νμΈνκ³ dpλ¬Έμ μΌκΉ μκ°ν΄λ΄€μλλ€.
(55λ 10λ²μ§Έ νΌλ³΄λμΉ μμ
λλ€. νΌλ³΄λμΉ μμ΄μμλ 첫λ²μ§Έ, λλ²μ§Έ μκ° 1μ κ°μ κ°μ§μ§λ§
μ΄ λ¬Έμ μμλ κ°μ κ²½μ°κ° λμ¬ μ μκΈ° λλ¬Έμ νμΉΈ μλΉκ²¨ μκ°νμ΅λλ€.)
n = iμΌ λμ κ²½μ°μ μλ
μ΄ μ νμμμ
λ¨ λ¬Έμ μμλ μκ° λ무 컀μ§λ κ²μ μ°λ €ν΄ 10007λ‘ λλ λλ¨Έμ§λ₯Ό μΆλ ₯νλλ‘ νλ
κ° dp[i]μ %10007λ§ μΆκ°ν΄μ£Όλ©΄ λ¬Έμ λ₯Ό ν΄κ²° ν μ μμ΅λλ€.
C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> dp(n+1);
dp[1] = 1;
dp[2] = 2;
for(int i = 3; i <= n; i++)
dp[i] = (dp[i-1]+dp[i-2])%10007;
cout << dp[n] << '\n';
return 0;
}μ‘°ν©μ νμ©ν νμ΄λ μ λ°ν΄μ μ‘°κΈ κ³΅λΆν΄λ³Ό κ°μΉκ° μλκ² κ°λ€μ γ γ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ κ° μΌλ§μ μ νμλ λ¬Έμ λ₯Ό 보λ λ°κ°λ€μ! μ λ λνλμ΄λ μμ λκ°μ΄ νμμ΅λλ΄. 맨 λ§μ§λ§μ κ°λ‘ 2κ°κ° μ€λκ±°λ μΈλ‘ 1κ°κ° μ€λκ±°λ μΌμ΄μ€ λΆλ₯ν΄μ νμμ΄μ¬
#include <stdlib.h>
int main() {
int n = 0;
scanf("%d", &n);
int* dp = (int*)malloc(sizeof(int) * (n + 1));
dp[0] = 0;
if (n >= 1) dp[1] = 1;
if (n >= 2) dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = (dp[i - 1] + dp[i - 2]) % 10007;
}
printf("%d", dp[n] % 10007);
free(dp);
return 0;
}```
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/11726
βοΈ μμλ μκ°
1h
β¨ μλ μ½λ
λνλμ΄ μΆμ²ν΄μ£Όμ 골λ μ€ν λ¬Έμ νλ€κ° μ€ν¨ν΄μ λ€λ₯Έ λ¬Έμ λ₯Ό κ°μ Έμλ΄€μ΅λλ΅
μ΄ λ¬Έμ λ 2 x n ν¬κΈ°μ μ§μ¬κ°νμ 1x2 μ§μ¬κ°νκ³Ό 2x1 μ§μ¬κ°νμΌλ‘ μ±μ°λ λ°©λ²μ μλ₯Ό ꡬνλ λ¬Έμ μ λλΉ
μ.. μ²μμ μκ°νλ€ λ μ€λ₯Έ κ² μ‘°ν©(Combination)μ νμ©ν΄λ³΄μ..! λΌλ μκ°μ νμ΄μ
1x2 νμΌμ μΈλ‘ νμΌ, 2x1νμΌμ κ°λ‘ νμΌμ΄λΌκ³ λΆλ₯Όκ²μ!..
κ°λ‘ νμΌμ κΉλ©΄ 1ν, 2νμ λμμ κΉμμΌ νλκΉ -> λμ μΈνΈλ‘ λ³΄κ³ νλλΌκ³ νννκ² μ΅λλ€(λ°©λ²μ μ ꡬν λ μ§μ₯ x)
μ λ κΈ°λ³Έμ μΌλ‘ μΈλ‘ νμΌμ΄ λͺ¨λ κΉλ €μλ€κ³ κ°μ ν μνμμ κ°λ‘ νμΌμ κ°μλ₯Ό iλ‘ λμμ΅λλ€.
forλ¬Έμ νμ©ν΄ κ°λ‘ νμΌμ 1κ°μ© λλ €κ°λλ°, μ΄λ₯Ό μ‘°ν©μ νμ©ν΄ λ°°μΉ κ°λ₯ν λ°©λ²μ μλ₯Ό κ΅¬ν΄ λͺ¨λ λν΄μ£Όμμ΅λλ€.!
μ§κΈ 보λ μ½λ μ£Όμμ΄ μλͺ»λλ€μ© νλ€ ν·κ°λ €μ μμ μ λ¬μλ¨λ μ£ΌμμΈλ° λ³κ²½ν λ²μ μλμ½λμ μ¬λ €λμκ²μ
νμ΄ν