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-kokeunho #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

4-kokeunho #16

wants to merge 1 commit into from

Conversation

kokeunho
Copy link
Collaborator

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

[BOJ] ํ”ผ๋ณด๋‚˜์น˜์˜ ์ˆ˜ 3 https://www.acmicpc.net/problem/2749

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

1h

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

ํ”ผ๋ณด๋‚˜์น˜์˜ ์ˆ˜๋ฅผ ์ž„์˜์˜ ์ˆซ์ž๋กœ ๋‚˜๋จธ์ง€ ๊ณ„์‚ฐ์„ ํ•˜๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ ์ฃผ๊ธฐ๋ฅผ ๊ฐ–๊ณ  ๋ฐ˜๋ณต๋˜๋Š”๋ฐ์š”.
์ด ์ฃผ๊ธฐ๋ฅผ ํ”ผ์‚ฌ๋…ธ ์ฃผ๊ธฐ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ”ผ์‚ฌ๋…ธ ์ฃผ๊ธฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด(=์ตœ์•…์˜ ๊ฒฝ์šฐ)๋Š” m^2๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ํ”ผ์‚ฌ๋…ธ ์ฃผ๊ธฐ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ period ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์ธ 0, 1์„ ๋„ฃ๊ณ  ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
2~m^2๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋‹ค์Œ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ์ฐพ๊ณ  ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
(๋ฌธ์ œ์—์„œ ๊ฒฐ๊ณผ๋Š” m์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ํ•˜์˜€์œผ๋‹ˆ new_value = (์•ž์ˆ˜ + ๋’ท์ˆ˜) % m ์ž…๋‹ˆ๋‹ค.)
๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค ๊ทธ ์ „์˜ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์™€ ํ˜„์žฌ์˜ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๊ฐ€ 0, 1์ด ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
์ด ๊ฒฝ์šฐ ์ฃผ๊ธฐ ํ•œ ๋ฒˆ ๋Œ๊ณ  ์ƒˆ ์ฃผ๊ธฐ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ฃผ๊ธฐ๋ฅผ ๋„˜์–ด์„  ๋‘ ๊ฐœ์˜ ๊ฐ’์„ ์ง€์›Œ์ฃผ๋ฉด period ๋ฆฌ์ŠคํŠธ๋Š” ์ฃผ๊ธฐ ๋‚ด์˜ ํ”ผ๋ณด๋‚˜์น˜์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ทธ ์ค‘ ๋งˆ์ง€๋ง‰ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

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

ํ”ผ๋ณด๋‚˜์น˜์˜ ์ˆ˜๋Š” ์•Œ๊ณ  ์žˆ์—ˆ์œผ๋‚˜ ์ด๋ ‡๊ฒŒ ํฐ ๋ฒ”์œ„์˜ ๊ฐ’์ด ์ž…๋ ฅ๋  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์ ์ธ
ํ”ผ์‚ฌ๋…ธ ์ฃผ๊ธฐ๋ฅผ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a 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;
}

์ €๋Š” ํ”ผ์‚ฌ๋…ธ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ์ฃผ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ์™€
ํ”ผ๋ณด๋‚˜์น˜๋ฅผ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌ๋ถ„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค

Comment on lines +3 to +12
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)
Copy link
Collaborator

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)์ด ํ”ผ์‚ฌ๋…ธ ์ฃผ๊ธฐ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋‹ˆ๊นŒ ์ฃผ๊ธฐ๊ฐ€ ๋‚˜ํƒ€๋‚  ๋•Œ ๊นŒ์ง€ ์ถฉ๋ถ„ํžˆ ๋ฐ˜๋ณตํ•˜๊ฒ ๋„ค์š”

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ค์šด ๋ฌธ์ œ์˜€๋„ค์š”.. ๊ทผ๋ฐ ์ด ์ฝ”๋“œ๋ฅผ ํ”ผ๋ณด๋‚˜์น˜2 ๋ฌธ์ œ์— ์ ์šฉํ•˜๋‹ˆ ํ‹€๋ฆฌ๋”๊ตฐ์š”?!?! ๋Œ€์ฒด ์™œ ๊ทธ๋Ÿฐ๊ฑธ๊นŒ์š”?

Copy link
Collaborator

@g0rnn g0rnn Oct 29, 2024

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;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ์ฝ”๋“œ๋Š” ํ”ผ๋ณด๋‚˜์น˜์ˆ˜์— mod ์—ฐ์‚ฐ์„ ํ•œ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ฑฐ๋ผ ๊ทธ๋Ÿฐ๊ฑฐ ์•„๋‹๊นŒ์š”??

Copy link
Collaborator

Choose a reason for hiding this comment

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

์•„ํ•˜!

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.

3 participants