Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
SSAFY13th-algorithm
/
Algorithm-study-2
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
1
Pull requests
82
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
db02dbf7a3e60fbb746d306412d4a079bfab2d76
Breadcrumbs
Algorithm-study-2
/
한종욱:10주차
/
boj1947.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
41 lines (36 loc) · 1.99 KB
db02dbf
Breadcrumbs
Algorithm-study-2
/
한종욱:10주차
/
boj1947.java
Copy path
Top
File metadata and controls
Code
Blame
41 lines (36 loc) · 1.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
public class boj1947 {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private static final int MOD = 1000000000; // 나머지 연산을 위한 모듈러 값
private static long[] dp; // 완전 순열의 경우의 수를 저장할 배열
private static int n; // 사람의 수
public static void main(String[] args) throws IOException {
init(); // 초기 설정 및 DP 계산
bw.write(String.valueOf(dp[n])); // 결과 출력
bw.flush();
bw.close();
br.close();
}
// 완전 순열(어느 누구도 자신의 선물을 받지 않는 순열)의 경우의 수 계산
private static void init() throws IOException {
n = Integer.parseInt(br.readLine()); // 사람 수 입력
dp = new long[n + 1]; // DP 배열 초기화
// 기본 케이스 설정
dp[0] = 1; // 0명일 때는 1가지 경우 (아무도 없는 경우)
dp[1] = 0; // 1명일 때는 0가지 경우 (자신의 선물을 받지 않을 방법 없음)
// 점화식을 이용한 DP 계산
// 완전 순열의 점화식: D(n) = (n-1) * (D(n-1) + D(n-2))
for (int i = 2; i <= n; i++) {
/*
* 이 점화식의 의미:
* 1. i번째 사람이 j번째 사람에게 선물을 줄 때(j != i)
* 2. 두 가지 경우가 발생:
* a. j번째 사람이 i번째 사람에게 선물을 줌 -> 나머지 n-2명에 대한 완전 순열 문제(dp[i-2])
* b. j번째 사람이 i번째 사람에게 선물을 주지 않음 -> i와 j를 제외한 나머지에 대한 완전 순열(dp[i-1])
* 3. 이를 j의 n-1가지 경우에 대해 모두 계산
*/
dp[i] = (i-1)*(dp[i-1] + dp[i-2]) % MOD; // 오버플로우 방지를 위한 모듈러 연산
}
}
}
You can’t perform that action at this time.