👀 문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWz50CHK8DgDFARQ
👊 도전
1. 설계
- DP로 해결한다.
2. 구현
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
import java.util.*;
/**
*
* @author HEESOO
*
*/
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
int mod=1000000007;
long[] factorial=new long[1001];
factorial[1]=1;
factorial[2]=2;
for(int i=3;i<=1000;i++) {
factorial[i]=i*factorial[i-1];
factorial[i]%=mod;
}
long[] dp=new long[1001];
dp[1]=0;
dp[2]=1;
for(int i=3;i<=1000;i++)
dp[i]=(i*dp[i-1]+(i/2)*factorial[i-1])%mod;
for(int test_case = 1; test_case <= T; test_case++)
{
int n=sc.nextInt();
System.out.println("#"+test_case+" "+dp[n]);
}
}
}
3. 결과
🤟 성공 🤟
4. 설명
- DP를 이용한다
- a(n) = n * a(n-1) + [n/2]*(n-1)!
- 이해 불가쓰