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

7-wnsmir #29

Merged
merged 1 commit into from
Nov 28, 2024
Merged

7-wnsmir #29

merged 1 commit into from
Nov 28, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Nov 19, 2024

πŸ”— 문제 링크

https://www.acmicpc.net/problem/1793

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

40min

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

DPλŠ” κ²°κ΅­ 점화식을 μ°ΎλŠ” μ‹Έμ›€μž…λ‹ˆλ‹€.

주둜 λ§ˆμ§€λ§‰μ„ μ‚΄νŽ΄λ³΄λŠ”κ²ƒμ΄ DPλ¬Έμ œλ“€μ„ 연달아 풀어보며 λŠλ‚€ μ μž…λ‹ˆλ‹€.

n=0μΌλ•Œ κ°€λ§Œνžˆ μžˆλŠ”κ±° ν•œκ°€μ§€λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
즉 dp[0] = 1

n=1μΌλ•ŒλŠ” 2*1인 타일 ν•˜λ‚˜λ‘œ μ±„μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€
λ§ˆμ°¬κ°€μ§€λ‘œ dp[1]=1

n=2μΌλ•ŒλŠ”
μ„Έλ‘œλ‘œ λ‘κ°œλ₯Ό λ„£κ±°λ‚˜, κ°€λ‘œλ‘œ λ‘κ°œλ₯Ό λ„£κ±°λ‚˜ 2*2ν•˜λ‚˜λ₯Ό λ„£λŠ” 방법 3가지가 μžˆμŠ΅λ‹ˆλ‹€.
dp[2]=3

n=3이라면?
λ§ˆμ§€λ§‰μ΄ μ„Έλ‘œμΈ 2*1μΌλ•Œ, 남은건 n=2μΌλ•Œμ™€ λ§ˆμ°¬κ°€μ§€

  • λ§ˆμ§€λ§‰ 두열을 2*1 κ°€λ‘œλ‘œ λ°°μΉ˜ν• λ•Œ, 남은건 n=1μΌλ•Œ
  • 2*2 νƒ€μΌμΌλ•Œ 남은건 n=1일경우

즉 dp[3] = dp[2] + dp[1} + dp[1] = dp[2] + 2dp[1]

κ°€λ‘œλ‘œ λ‘κ°œλ₯Ό λ°°μΉ˜ν•˜λŠ”κ²ƒκ³Ό 2*2λ₯Ό ν•˜λ‚˜ λ°°μΉ˜ν•˜λŠ”κ²ƒμ€ λ‹€λ₯Έ 경우의 μˆ˜μ§€λ§Œ dp[1]을 λΆˆλŸ¬μ˜€λŠ”κ²ƒμ€ κ°™κΈ° λ•Œλ¬Έμ— 2λ°°λ₯Ό ν•΄μ£ΌλŠ”κ²ƒμ΄ ν‚₯μž…λ‹ˆλ‹€.

μ΅œλŒ€ νƒ€μΌμ˜ 크기가 2*2라 κ·Έ 이상은 κ³ λ €ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€.

점화식을 λ°”νƒ•μœΌλ‘œ μ½”λ“œκ΅¬ν˜„μ€ μ‰¬μš΄νŽΈμž…λ‹ˆλ‹€.

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.

점화식은 그리 어렡지 μ•Šκ²Œ κ΅¬ν–ˆμœΌλ‚˜.. cλ‚˜ cppμ—μ„œλŠ” μ²˜λ¦¬ν•  수 μ—†λŠ” 큰 μˆ˜λ“€ λ•Œλ¬Έμ— string으둜 수λ₯Ό λ”ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. 덕뢄에 μ½”λ”© μŠ€ν‚¬μ΄ λŠ˜μ—ˆλ„€μš” :)

cpp code
#include <iostream>	
using namespace std;

string add(string large, string small) {
	if (large.length() < small.length()) {
		string tmp = small;
		small = large;
		large = tmp;
	}

	string sum = "";
	int carry = 0;

	// large와 small이 μ‹€μ œ μ—°μ‚°λ˜λŠ” 둜직
	for (int i = small.length() - 1; i >= 0; i--) {
		int c = large[i] - '0' + small[i] - '0' + carry;
		carry = c / 10;
		sum = (char)(c % 10 + '0') + sum;
	}

	// large와 small의 길이가 λ‹€λ₯Ό λ•Œ large의 μ•žλΆ€λΆ„μ„ sum에 μΆ”κ°€
	for (int i = large.length() - small.length() - 1; i >= 0; i--) {
		int c = large[i] - '0' + carry;
		carry = c / 10;
		sum = (char)(c % 10 + '0') + sum;
	}

	if (carry > 0) {
		sum = (char)(carry + '0') + sum;
	}

	return sum;
}

string countTile(int n) {
	string dp[251] = { "0", };
	dp[0] = "1";
	dp[1] = "1";
	dp[2] = "3";
	for (int i = 3; i <= n; i++) 
		//dp[i] += dp[i - 1] + dp[i - 2] * 2; μ•„λž˜μ™€ 같은 μ˜λ―Έμž…λ‹ˆλ‹€ :)
		dp[i] = add(dp[i], add(dp[i-1], add(dp[i-2], dp[i-2])));
	return dp[n];
}

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

	int n;
	while (cin >> n) {
		cout << countTile(n) << '\n';
	}

	return 0;
}

Comment on lines +11 to +12
for i in range(2, max_n + 1):
dp[i] = dp[i-1] + dp[i-2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

νŒŒμ΄μ¬μ€ μ§„μ§œ 신인가.. μ΄λ ‡κ²Œ 큰수λ₯Ό μ–΄λ–»κ²Œ λ”ν•˜λŠ”μ§€ 정말 λŒ€λ‹¨ν•˜λ„€μš”!

  • 근데 여기에 dp[i-2] * 2λ₯Ό ν•˜μ§€ μ•Šμ•„λ„ λ˜λ‚˜μš”?

Copy link
Collaborator

Choose a reason for hiding this comment

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

γ…‹γ…‹γ…‹γ…‹ μ§„μ§œ νŒŒμ΄μ¬μ€ κ·Έλƒ₯ λ”ν•΄λ²„λ¦¬λ„€μš”...

그리고 저도 dp[i-2] *2 λ₯Ό ν•˜μ§€ μ•ŠλŠ” μ΄μœ κ°€ κΆκΈˆν•©λ‹ˆλ‹€!

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.

점화식은 생각보닀 λΉ λ₯΄κ²Œ μƒκ°”λ‚¬μ—ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ λΉ λ₯΄κ²Œ κ΅¬ν˜„ν•΄λ³΄λ €κ³  ν–ˆλŠλ°...

μ˜ˆμ‹œ 좜λ ₯을 λ³΄λ‹ˆκΉŒ 거의 40자리수 이상이 λ‚˜μ˜€λŠ”κ±Έ 보고
μ•„ 이건 λ¬Έμžμ—΄λ‘œ λ°›μœΌλ©΄μ„œ κ³„μ‚°ν•΄μ•Όκ² κ΅¬λ‚˜... λΌλŠ” 생각을 ν–ˆμŠ΅λ‹ˆλ‹€.

λ¬Έμžμ—΄μ„ κ³„μ‚°ν•˜λŠ” 뢀뢄을 순쑰둭게 μ™„μ„±ν–ˆλ‹€κ³  μƒκ°ν•˜κ³  싀행을 μ‹œμΌœλ΄€μŠ΅λ‹ˆλ‹€.
근데 .... 342 + 242 κ°€ 자꾸 485둜 λ‚˜μ˜€λŠ” κ²λ‹ˆλ‹€.
그리고 λ§ˆμ§€λ§‰κ³„μ‚°μ—μ„œ 올림이 된 경우λ₯Ό ν™•μΈν•˜μ§€ λͺ»ν•˜λŠ” λ¬Έμ œλ„ μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

그런데 584κ°€.... 485κ°€ λ‚˜μ˜¨λ‹€? μ™œ 거꾸둜 λ‚˜μ˜€μ§€...?
μ΄λ•Œ 생각 λ‚¬μŠ΅λ‹ˆλ‹€.
λ°°μ—΄μ—μ„œ 0λΆ€ν„° μ ‘κ·Όν•˜λ©΄ 제일 μ•žλΆ€λΆ„ λΆ€ν„° λ‹€λ£¨κ²Œ λ˜λŠ”λ°
μ €λŠ” λ„ˆλ¬΄ λ‹Ήμ—°ν•˜κ²Œλ„ 1μ˜μžλ¦¬λΆ€ν„° 닀룬닀고 μƒκ°ν–ˆμ—ˆλ˜ κ²ƒμž…λ‹ˆλ‹€.

κ·Έλž˜μ„œ μ €λŠ” 배열을 reverseν•˜λŠ” μ½”λ“œλ₯Ό μΆ”κ°€ν–ˆκ³  ,,, μ •λ‹΅μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

쀑간에 문제λ₯Ό ν’€λ©΄μ„œ dp[i-2]에 κ³±ν•˜κΈ° 2λ₯Ό ν•˜λŠ” λ‘œμ§λ„ ν•„μš”ν•˜λ‹€λŠ” 것을 μ•Œμ•˜μŠ΅λ‹ˆλ‹€.
그리고 κΈ°λŠ₯은 μ™„μ„±λ˜μ—ˆλŠ”λ°
string을 λ”ν•˜λŠ” ν•¨μˆ˜λ₯Ό μ»΄νŒ©νŠΈν•˜κ²Œ μˆ˜μ •ν•˜κ³ ,
μž…λ ₯λ°›λŠ” 뢀뢄을 μƒκ°ν•œλ‹€κ³  1μ‹œκ°„ 반 κ±Έλ ΈμŠ΅λ‹ˆλ‹€ γ…‹γ…‹γ…‹γ…‹

μ‹€μ „μ—μ„œ 이런 문제λ₯Ό λ§Œλ‚˜λ©΄ 파이썬으둜 ν’€μ–΄λ³΄λŠ” 것도 λ‚˜μ˜μ§€ μ•Šμ„ 것 κ°™λ„€μš”.

CPP CODE
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

string sumString(string a, string b) {
    string result = "";
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());

    int ten = 0;
    for (int i = 0; i < a.length(); i++) {
        int temp = (a[i] - '0') + (b[i] - '0') + ten;
        if (temp >= 10) {
            ten = 1;
            temp %= 10;
        } 
        else ten = 0;

        result = to_string(temp) + result;
    }
    
    if (ten == 1) result = to_string(ten) + result;
    return result;
}

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);

    vector<string> dp(251);
    dp[0] = "1";
    dp[1] = "1";
    dp[2] = "3";

    for (int i = 3; i <= 250; i++) {
        string mul = sumString(dp[i - 2], dp[i - 2]);
        dp[i] = sumString(dp[i - 1], mul);
    }

    int N;
    while(cin >> N){
        cout << dp[N] << '\n';
    }
    
    return 0;
}

Comment on lines +11 to +12
for i in range(2, max_n + 1):
dp[i] = dp[i-1] + dp[i-2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

γ…‹γ…‹γ…‹γ…‹ μ§„μ§œ νŒŒμ΄μ¬μ€ κ·Έλƒ₯ λ”ν•΄λ²„λ¦¬λ„€μš”...

그리고 저도 dp[i-2] *2 λ₯Ό ν•˜μ§€ μ•ŠλŠ” μ΄μœ κ°€ κΆκΈˆν•©λ‹ˆλ‹€!

@kokeunho
Copy link
Collaborator

μ €λŠ” 점화식도 혼자 μƒκ°ν•˜κΈ° κ½€ μ–΄λ €μ› λŠ”λ°μš” 흠흠;
λΉ„μŠ·ν•œ μœ ν˜•μ„ 많이 풀어보닀보면 늘겠죠?
μ €λŠ” κ²€μƒ‰ν•΄μ„œ ν’€μ—ˆλŠ”λ° μ΄λ ‡κ²Œ 큰 수λ₯Ό 만질 λ•ŒλŠ” μžλ°”μ—μ„œλŠ”
ν‰μ†Œμ— 많이 μ“΄ int 배열을 μ‚¬μš©ν•΄μ„œ μž…λ ₯λ°›λŠ”κ²Œ μ•„λ‹ˆλΌ BigInteger 객체λ₯Ό μ”λ‹ˆλ‹€. λΆˆλ³€ 객체라고 ν•˜λ˜λ° μ“°λŠ” 법이 ꡉμž₯히 λ‚―μ„€μ—ˆμŠ΅λ‹ˆλ‹€.
μƒˆλ‘œμš΄ λ°°μ›€μ΄μ—ˆμŠ΅λ‹ˆλ‹€.
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!

@g0rnn g0rnn merged commit b9f5e94 into main Nov 28, 2024
1 check passed
@g0rnn g0rnn deleted the 7-wnsmir branch November 28, 2024 15:03
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.

4 participants