답안 #896728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896728 2024-01-02T02:16:23 Z LCJLY Linear Garden (IOI08_linear_garden) C++14
75 / 100
46 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

int n,mod;
int arr[1000005];
vector<int>memo[3][3][2];

int dp(int index, int lotus, int pap, bool bound){
	if(index==n) return 1;
	if(memo[lotus][pap][bound][index]!=-1) return memo[lotus][pap][bound][index];
	int ans=0;
	
	if(bound){
		for(int x=1;x<=arr[index];x++){
			int hold=lotus;
			int hold2=pap;
			if(x==1){
				hold++;
				hold2=max(0LL,hold2-1);
			}
			else{
				hold2++;
				hold=max(0LL,hold-1);
			}
			
			if(hold>2||hold2>2) continue;
			ans=(ans+dp(index+1,hold,hold2,x==arr[index]))%mod;
		}
	}
	else{
		for(int x=1;x<=2;x++){
			int hold=lotus;
			int hold2=pap;
			if(x==1){
				hold++;
				hold2=max(0LL,hold2-1);
			}
			else{
				hold2++;
				hold=max(0LL,hold-1);
			}
			
			if(hold>2||hold2>2) continue;
			ans=(ans+dp(index+1,hold,hold2,false))%mod;
		}
	}
	return memo[lotus][pap][bound][index]=ans;
}

void solve(){	
	cin >> n >> mod;
	
	string s;
	cin >> s;
	
	for(int x=0;x<n;x++){
		if(s[x]=='L') arr[x]=1;
		else arr[x]=2;
	}
	
	for(int x=0;x<3;x++){
		for(int y=0;y<3;y++){
			for(int i=0;i<2;i++){
				for(int j=0;j<=n;j++){
					memo[x][y][i].push_back(-1);
				}
			}
		}
	}
	cout << dp(0,0,0,1);
	
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("out.txt", "w", stdout);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}





		


		
		
	
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 18900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 39 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 43 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 42 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 38 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -