Advertisement
Madiyar

Untitled

Jan 17th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <ctime>
  11. #include <cassert>
  12. #include <queue>
  13.  
  14. using namespace std;
  15.  
  16. #define f first
  17. #define s second
  18. #define mp make_pair
  19. #define pb push_back
  20. #define forit(it,con) for (typeof(con.begin()) it = con.begin(); it != con.end(); ++it)
  21. #define f0(a) memset(a, 0, sizeof(a))
  22. #define all(v) v.begin(), v.end()
  23. #define pii pair<int,int>
  24. #define vi vector<int>
  25. #define ll long long
  26. #ifdef WIN32
  27.     #define I64 "%I64d"
  28. #else
  29.     #define I64 "%lld"
  30. #endif
  31. int X, Y, Z, n;
  32.  
  33. struct Mat {
  34.     double a[2][2];
  35.     Mat() {
  36.         memset(a, 0, sizeof(a));
  37.     }
  38.     Mat operator * (Mat other) {
  39.         Mat c;
  40.         for (int i = 0; i < 2; ++i)
  41.             for (int j = 0; j < 2; ++j)
  42.                 for (int k = 0; k < 2; ++k)
  43.                     c.a[i][j] += a[i][k] * other.a[k][j];
  44.         return c;
  45.     }
  46.    
  47. }a;
  48.  
  49. inline double getProb(double i, double n) {
  50.     return ((i + 1) * (n - i) * 2 - 1) / (n * n);
  51. }
  52.  
  53. inline double Pow(int n, double P) {
  54.     Mat a, res;
  55.     res.a[0][0] = res.a[1][1] = 1;
  56.     a.a[0][0] = 1 - P;
  57.     a.a[0][1] = P;
  58.     a.a[1][0] = P;
  59.     a.a[1][1] = 1 - P;
  60.  
  61.     while (n > 0) {
  62.         if (n & 1) res = res * a;
  63.         a = a * a;
  64.         n >>= 1;
  65.     }
  66.     return res.a[1][0];
  67. }
  68.  
  69. void Solve() {
  70.     double ans = 0;
  71.     for (int x = 0; x < X; ++x)
  72.         for (int y = 0; y < Y; ++y)
  73.             for (int z = 0; z < Z; ++z) {
  74.                 ans += Pow(n, getProb(x, X) * getProb(y, Y) * getProb(z, Z));
  75.                                        
  76.             }
  77.     printf("%.6lf\n", ans);
  78. }
  79.  
  80.  
  81. int main() {
  82.     #ifdef LOCAL
  83.         freopen("in","r",stdin);
  84.         freopen("out","w",stdout);
  85.     #endif  
  86.     int tests;
  87.     scanf("%d", &tests);
  88.        
  89.     for (int test = 1; test <= tests; ++test) {
  90.         scanf("%d%d%d%d", &X, &Y, &Z, &n);
  91.         printf("Case %d: ", test);
  92.         Solve();
  93.     }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement