POJ1189钉子和小球(DP)
对钉子DP,如果钉子存在DP[i+1][j]+=DP[i][j];
DP[i+1][j+1]+=DP[i][j];
如果不存在DP[i+2][j+1]+=4*DP[i][j];
见代码:(有一个比较坑爹的就是要用__Int64)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <math.h> 5 #include <iostream> 6 #include <stack> 7 #include <set> 8 #include <queue> 9 #define MAX(a,b) (a) > (b)? (a):(b) 10 #define MIN(a,b) (a) < (b)? (a):(b) 11 #define mem(a) memset(a,0,sizeof(a)) 12 #define INF 1000000007 13 #define MAXN 20005 14 using namespace std; 15 16 __int64 dp[55][55]; 17 int N,M; 18 bool map[55][55]; 19 20 __int64 gcd(__int64 a,__int64 b)//GCD求最大公约数 21 { 22 return a%b==0?b:gcd(b,a%b); 23 } 24 25 int main() 26 { 27 while(~scanf("%d%d%*c",&N,&M)) 28 { 29 mem(dp);mem(map); 30 char str[1000]={0}; 31 int i,j; 32 dp[0][0]=1; 33 for(i=0;i<N;i++) 34 { 35 j=0; 36 int t=0; 37 gets(str); 38 while(str[j]) 39 { 40 if(str[j] != ' ')map[i][t++] = str[j]=='*'; 41 j++; 42 } 43 for(j=0;j<t;j++) 44 { 45 if(map[i][j]) 46 { 47 dp[i+1][j]+=dp[i][j]; 48 dp[i+1][j+1]+=dp[i][j]; 49 } 50 else 51 { 52 dp[i+2][j+1]+=dp[i][j]*4; 53 } 54 } 55 } 56 __int64 a = dp[N][M],b = (__int64)pow((double)2,N); 57 __int64 c = gcd(a,b); 58 printf("%I64d/%I64d\n",a/c,b/c); 59 60 } 61 return 0; 62 }