-
Notifications
You must be signed in to change notification settings - Fork 0
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-kokeunho #16
base: main
Are you sure you want to change the base?
4-kokeunho #16
Conversation
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.
๊ทธ๋ฅ ์ฒ์์ ์กฐ๊ฑด ์๊ฐ์์ด ํ์์๋
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
์ด๋ ๊ฒ ํ์๋๋ฐ 1,000๋ง ์
๋ ฅํด๋ ์๊ฐ์ด ํ์ฐธ ๊ฑธ๋ฆฌ๋๋ผ๊ตฌ์..
๊ทธ๋์ ๋ค์ํ๊ฒ ์๊ฐํด๋ณด๋ค๊ฐ ๊ทผํธ๋ ํ์ด๋ฅผ ๋ดค๋๋ฐ...
'ํผ์ฌ๋
ธ ์ฃผ๊ธฐ'๋ ์ฒ์ ๋ค์ด๋ดค์ต๋๋ค
์ฌ์ฉ์๊ฐ ์ ํ m์ ๋ฐ๋ผ ์ฃผ๊ธฐ๋ฅผ ์ฐพ์์
(n % ์ฃผ๊ธฐ) ๋ฅผ ์ถ๋ ฅํ๋ฉด n์ ํผ๋ณด๋์น์๋ฅผ 1,000,000์ผ๋ก ๋๋ ๋๋จธ์ง๋ฅผ ์ ์ ์๊ฒ ๋ค์
CPP CODE
#include <iostream>
using namespace std;
#define MOD 1000000
int getPisanoPeriod(int m) {
int prev = 0;
int curr = 1;
int temp;
for (int i = 0; i < m * m; i++) {
temp = (prev + curr) % m;
prev = curr;
curr = temp;
if (prev == 0 && curr == 1) return i + 1;
}
return 0;
}
int main() {
long long int n;
cin >> n;
int size = getPisanoPeriod(MOD);
int fib[size];
fib[0] = 0, fib[1] = 1;
for (int i = 2; i < size; i++)
fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
cout << fib[n % size];
return 0;
}
์ ๋ ํผ์ฌ๋
ธ ๊ณต๋ถ๋ฅผ ์ํด ์ฃผ๊ธฐ๋ฅผ ๊ตฌํ๋ ์ฝ๋์
ํผ๋ณด๋์น๋ฅผ ๊ตฌํ๋ ์ฝ๋๋ฅผ ๊ตฌ๋ถํด๋ดค์ต๋๋ค
period = [0, 1] | ||
|
||
for i in range(2, m*m+1): | ||
new_value = (period[i-1] + period[i-2]) % m | ||
period.append(new_value) | ||
|
||
if period[i] == 1 and period[i-1] == 0: | ||
period = period[:-2] | ||
break | ||
length = len(period) |
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.
ํผ์ฌ๋ ธ ์ฃผ๊ธฐ๋ฅผ ์ฐพ๋ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ฒ ์ ๋ํ๋ ๊ฒ ๊ฐ์์ ์ข์ต๋๋ค.
์ (m*m+1)์ ์ํ์ ์ผ๋ก ์ค์ ํ๋์ง๋ ์ฝ๊ฐ ์๋ฌธ์ด์๋๋ฐ,
ํผ์ฌ๋
ธ ์ฃผ๊ธฐ์ ํน์ง์ ์ฌ์ฉํ๊ฑฐ์๋ค์ฉ
(m*m)์ด ํผ์ฌ๋ ธ ์ฃผ๊ธฐ์ ์ต๋ ๊ธธ์ด๋๊น ์ฃผ๊ธฐ๊ฐ ๋ํ๋ ๋ ๊น์ง ์ถฉ๋ถํ ๋ฐ๋ณตํ๊ฒ ๋ค์
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 ๋ฌธ์ ์ ์ ์ฉํ๋ ํ๋ฆฌ๋๊ตฐ์?!?! ๋์ฒด ์ ๊ทธ๋ฐ๊ฑธ๊น์?
kokeunho/๊ตฌํ/4-kokeunho.py
Outdated
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.
์ฒ์์ for๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ตฌํํ์์ต๋๋ค.
cpp
int main() {
const int divisor = 1000000;
unsigned long long int n, prev, tmp, cur;
cin >> n;
prev = 0;
cur = 1;
if (n == 0) cout << 0;
else if (n == 1) cout << 1;
else {
for (unsigned long long i = 0; i < n - 1; i++) {
tmp = cur;
cur = cur % divisor + prev % divisor;
prev = tmp % divisor;
}
cout << cur;
}
return 0;
}
๊ทผ๋ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ ์ gptํํ ๋ฌผ์ด๋ดค๋๋ n์ด ์ ๋ ๊ฒ ํฐ ๊ฒฝ์ฐ์ ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ต 300๋ , for๋ฌธ์ ์ฌ์ฉํ๋ฉด 30๋ ์ด ๊ฑธ๋ฆฐ๋ค๋๊ตฐ์.....ใ ;
๊ทธ๋์ ๊ทผํธ๋ ์์ฝ๋ณด๋ฉด์ ์๋ก ํ์ด๋ดค์ต๋๋ค. ^^
correct answer
#include <iostream>
#include <vector>
using namespace std;
unsigned long long n;
unsigned long long divisor = 1000000;
unsigned long long fibo() {
vector<unsigned long long> period;
period.push_back(0);
period.push_back(1);
for (unsigned long i = 2; ; i++) {
unsigned long long cur_fibo = (period[i - 1] + period[i - 2]) % divisor;
period.push_back(cur_fibo);
if (period[i] == 1 && period[i - 1] == 0) break;
}
int pisano_period = period.size() - 2;
return period[n % pisano_period];
}
int main() {
cin >> n;
cout << fibo();
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.
์ด ์ฝ๋๋ ํผ๋ณด๋์น์์ mod ์ฐ์ฐ์ ํ ์๋ฅผ ์ถ๋ ฅํ๋๊ฑฐ๋ผ ๊ทธ๋ฐ๊ฑฐ ์๋๊น์??
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.
์ํ!
๐ ๋ฌธ์ ๋งํฌ
[BOJ] ํผ๋ณด๋์น์ ์ 3 https://www.acmicpc.net/problem/2749
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
ํผ๋ณด๋์น์ ์๋ฅผ ์์์ ์ซ์๋ก ๋๋จธ์ง ๊ณ์ฐ์ ํ๋ฉด ๋๋จธ์ง๊ฐ ์ฃผ๊ธฐ๋ฅผ ๊ฐ๊ณ ๋ฐ๋ณต๋๋๋ฐ์.
์ด ์ฃผ๊ธฐ๋ฅผ ํผ์ฌ๋ ธ ์ฃผ๊ธฐ๋ผ๊ณ ํฉ๋๋ค. ํผ์ฌ๋ ธ ์ฃผ๊ธฐ์ ์ต๋ ๊ธธ์ด(=์ต์ ์ ๊ฒฝ์ฐ)๋ m^2๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
์ด ํผ์ฌ๋ ธ ์ฃผ๊ธฐ๋ฅผ ์ฐพ๊ธฐ ์ํ period ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ ์ฒซ๋ฒ์งธ, ๋๋ฒ์งธ ํผ๋ณด๋์น ์์ธ 0, 1์ ๋ฃ๊ณ ์์ํฉ๋๋ค.
2~m^2๊น์ง ๋ฐ๋ณตํ๋ฉด์ ๋ค์ ํผ๋ณด๋์น ์๋ฅผ ์ฐพ๊ณ ๋ฆฌ์คํธ์ ์ถ๊ฐํฉ๋๋ค.
(๋ฌธ์ ์์ ๊ฒฐ๊ณผ๋ m์ผ๋ก ๋๋ ๊ฐ์ ์ถ๋ ฅํ๋ผ๊ณ ํ์์ผ๋ new_value = (์์ + ๋ท์) % m ์ ๋๋ค.)
๋งค ๋ฐ๋ณต๋ง๋ค ๊ทธ ์ ์ ํผ๋ณด๋์น ์์ ํ์ฌ์ ํผ๋ณด๋์น ์๊ฐ 0, 1์ด ๋์ค๋ ์ํฉ์ ์ฐพ์ต๋๋ค.
์ด ๊ฒฝ์ฐ ์ฃผ๊ธฐ ํ ๋ฒ ๋๊ณ ์ ์ฃผ๊ธฐ๋ฅผ ์์ํ๋ ๊ฒ์ ๋๋ค.
์ฃผ๊ธฐ๋ฅผ ๋์ด์ ๋ ๊ฐ์ ๊ฐ์ ์ง์์ฃผ๋ฉด period ๋ฆฌ์คํธ๋ ์ฃผ๊ธฐ ๋ด์ ํผ๋ณด๋์น์ ์๋ก ์ด๋ฃจ์ด์ ธ ์์ ๊ฒ์ ๋๋ค.
๊ทธ ์ค ๋ง์ง๋ง ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํผ๋ณด๋์น์ ์๋ ์๊ณ ์์์ผ๋ ์ด๋ ๊ฒ ํฐ ๋ฒ์์ ๊ฐ์ด ์ ๋ ฅ๋ ๋ ์ฌ์ฉํ๋ฉด ํจ์จ์ ์ธ
ํผ์ฌ๋ ธ ์ฃผ๊ธฐ๋ฅผ ์ฒ์ ์๊ฒ ๋์์ต๋๋ค.