Flea Circus(Project Euler 213)

Posted vb4896

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flea Circus(Project Euler 213)相关的知识,希望对你有一定的参考价值。

original version

hackerrank programming version

 

题目大意是N*N的格子,每个格子一开始有1个跳蚤,每过单位时间跳蚤会等概率向四周跳,问M秒后空格子的期望个数。

 

题解:

对于每个跳蚤暴力模拟每一秒,算出M秒后它到各个格子的概率最后统计就好了,hackerrank上的版本需要常数优化,比如根据对称性只考虑左上四分之一块矩形.

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 typedef long long LL;
  5 const int mod = 1e9 + 7;
  6 #define MAXN 42
  7 #define MP make_pair
  8 const int INF = 1e9 + 10;
  9 
 10 int n, m;
 11 int s[MAXN][MAXN], tmp[MAXN][MAXN], cnt[MAXN][MAXN];
 12 LL p[MAXN][MAXN];
 13 int dx[] = {0, 0, -1, 1};
 14 int dy[] = {1, -1, 0, 0};
 15 int inv[5];
 16 
 17 
 18 int power(int x, int p)
 19 {
 20     int res = 1;
 21     for (; p; p >>= 1)
 22     {
 23         if (p & 1) res = 1LL * res * x % mod;
 24         x = 1LL * x * x % mod;
 25     }
 26     return res;
 27 }
 28 
 29 void work(int sx, int sy)
 30 {
 31     for (int i = 1; i <= n; ++i)
 32         for (int j = 1; j <= n; ++j)
 33             s[i][j] = 0;
 34     s[sx][sy] = 1;
 35      
 36     int x, y;
 37     for (int _m = 1; _m <= m; ++_m)
 38     {
 39         for (int i = 1; i <= n; ++i)
 40             for (int j = 1; j <= n; ++j)
 41                 tmp[i][j] = 0;
 42         for (int i = 1; i <= n; ++i)
 43         {
 44             for (int j = 1; j <= n; ++j)
 45             {
 46                 for (int d = 0; d < 4; ++d)
 47                 {
 48                     x = i + dx[d];
 49                     y = j + dy[d];
 50                     (tmp[x][y] += 1LL * cnt[i][j] * s[i][j] % mod) %= mod;
 51                 }
 52             }
 53         }
 54         for (int i = 1; i <= n; ++i)
 55             for (int j = 1; j <= n; ++j)
 56                 s[i][j] = tmp[i][j];
 57     }
 58     int t;
 59     for (int i = 1; i <= n; ++i)
 60     {
 61         for (int j = 1; j <= n; ++j)
 62         {
 63             t = 1 - s[i][j] + mod;
 64             (p[i][j] *= t) %= mod;
 65             (p[n + 1 - i][j] *= t) %= mod;
 66             (p[i][n + 1 - j] *= t) %= mod;
 67             (p[n + 1 - i][n + 1 - j] *= t) %= mod;
 68         }
 69     }
 70 }
 71 
 72 
 73 
 74 int main() 
 75 {
 76     //freopen("in.txt", "r", stdin);
 77     //freopen("out.txt", "w", stdout);
 78 
 79     inv[1] = 1;
 80     inv[2] = power(2, mod - 2);
 81     inv[3] = power(3, mod - 2);
 82     inv[4] = power(4, mod - 2);
 83     
 84     int T, x, y;
 85     scanf("%d", &T);
 86     while (T--)
 87     {
 88         scanf("%d %d", &n, &m);
 89         for (int i = 1; i <= n; ++i)
 90         {
 91             for (int j = 1; j <= n; ++j)
 92             {
 93                 cnt[i][j] = 0;
 94                 for (int d = 0; d < 4; ++d)
 95                 {
 96                     x = i + dx[d];
 97                     y = j + dy[d];
 98                     if (x >= 1 && x <= n && y >= 1 && y <= n)
 99                         ++cnt[i][j];
100                 }
101                 cnt[i][j] = inv[cnt[i][j]];
102             }
103             
104         }
105         for (int i = 1; i <= n; ++i)
106             for (int j = 1; j <= n; ++j)
107                 p[i][j] = 1;
108         LL ans = 0;
109         for (int i = 1; i <= n / 2; ++i)
110             for (int j = 1; j <= n / 2; ++j)
111                 work(i, j);
112         for (int i = 1; i <= n; ++i)
113             for (int j = 1; j <= n; ++j)
114                 ans += p[i][j];
115         ans %= mod;
116         cout << ans << endl;
117      }
118     
119     return 0; 
120 }

 

以上是关于Flea Circus(Project Euler 213)的主要内容,如果未能解决你的问题,请参考以下文章

Project Euler 109 :Darts 飞镖

Project Euler 5

Project Euler

project euler 169

Project Euler

Project Euler 3