pku1189 钉子和小球

http://poj.org/problem?id=1189

DP,“数塔”

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int a[56][56];
 5 long long dp[56][56];
 6 
 7 long long gcd(long long x, long long y)
 8 {
 9     return y? gcd(y, x%y): x;
10 }
11 
12 
13 int main()
14 {
15     int i, j, k, n, m;
16     long long r;
17     char s[1234] = "\0";
18     memset(dp, 0, sizeof(dp));
19     scanf("%d%d%*c", &n, &m);
20     for(i=1; i<=n; i++)
21     {
22         gets(s);
23         k = 1;
24         for(j=0; s[j]; j++)
25         {
26             if(s[j]=='*')
27             {
28                 a[i][k] = 1;
29                 k ++;
30                 continue;
31             }
32             if(s[j]=='.')
33             {
34                 a[i][k] = 0;
35                 k ++;
36                 continue;
37             }
38         }
39     }
40     dp[1][1] = (long long)1<<56;
41     //printf("%lld\n", dp[1][1]);
42     for(i=1; i<=n; i++)
43     {
44         for(j=1; j<=i; j++)
45         {
46             if(a[i][j])
47             {
48                 dp[i+1][j] += (dp[i][j]>>1);
49                 dp[i+1][j+1] += (dp[i][j]>>1);
50             }
51             else
52             {
53                 dp[i+2][j+1] += dp[i][j];
54             }
55             //printf("%lld ", dp[i][j]);
56         }
57         //printf("\n");
58     }
59     r = dp[i][m+1];
60     if(r == 0)
61     {
62         printf("0/1\n");
63         return 0;
64     }
65     if(gcd(dp[1][1], r)==1)
66     {
67         printf("%lld\n", r);
68         return 0;
69     }
70     printf("%lld/%lld\n", (long long)r/gcd(dp[1][1], r), (long long)dp[1][1]/gcd(dp[1][1], r));
71     return 0;
72 }

 

posted @ 2013-01-20 23:52  Yuan1991  阅读(144)  评论(0编辑  收藏  举报