2014-01-24 11:53:48Morris
[ZJ] a812: 2013高雄市能力競賽高中組 3. 動物排隊
ShowImage.png
[ZJ]
/**********************************************************************************/
/* Problem: a812 "2013高雄市能力競賽高中組 3. 動物排隊" from 2013高雄市資訊學科能力複賽*/
/* Language: CPP (676 Bytes) */
/* Result: AC(0.1s, 116.5MB) judge by this@ZeroJudge */
/* Author: morris1028 at 2014-01-21 22:47:31 */
/**********************************************************************************/
#include <stdio.h>
#include <string.h>
long long dp[65][65][3605];
int main() {
int x, y, M;
int i, j, k;
while(scanf("%d %d %d", &x, &y, &M) == 3) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1; // ways
int n = x + y;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++) {
for(k = 0; k < M; k++) {
if(i+1-j <= x && i+1-j >= 0)
dp[i-j+1][j][(k*2)%M] += dp[i-j][j][k];
if(j+1 <= y && j+1 <= i-j && i-j >= 0)
dp[i-j][j+1][(k*2+1)%M] += dp[i-j][j][k];
}
}
}
long long ret = 0;
for(i = 0; i <= n; i++) {
for(j = 0; j < M; j++)
ret += dp[i][n-i][j]*j;
}
printf("%lld\n", ret);
}
return 0;
}