Advertisement
double_trouble

GenCheck

Nov 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. const long long N = 4294967295;
  10. const double a[] = {1.0000886, 0.4713941, 0.0001348028, -0.008553069, 0.00312558, -0.0008426812, 0.00009780499};
  11. const double b[] = {-0.2237368, 0.02607083, 0.01128186, -0.01153761, 0.005169654, 0.00253001, -0.001450117};
  12. const double c[] = {-0.01513904, -0.008986007, 0.02277679, -0.01323293, -0.006950356, 0.001060438, 0.001565326};
  13.  
  14. vector<int> vA;
  15. vector<vector<int> > vB;
  16. vector<vector<long long> > StirlingNumb;
  17.  
  18. double chiSquared(double alpha, double frdegr)
  19. {
  20.   double res(0);
  21.   double d;
  22.   if (alpha < 0.5) {
  23.     d = -2.0637 * pow(log(1.0 / alpha) - 0.16, 0.4274) + 1.5774;
  24.   }
  25.   else {
  26.     d = 2.0637 * pow(log(1.0 / (1.0 - alpha)) - 0.16, 0.4274) - 1.5774;
  27.   }
  28.  
  29.   for (int j(0); j < 7; ++j) {
  30.     res += pow(frdegr, (-j * 1.0) / 2.0) * pow(d, 1.0 * j) * (a[j] + b[j] / (1.0 * frdegr) + c[j] / (1.0 * frdegr * frdegr));
  31.   }
  32.  
  33.   res = frdegr * pow(res, 3.0);
  34.   return res;
  35. }
  36.  
  37. int checkType(const vector<long long> &vals)
  38. {
  39.   map<long long, int> tp;
  40.   for (int i(0); i < vals.size(); ++i) {
  41.     tp[vals[i]]++;
  42.   }
  43.  
  44.   vector<int> count(6);
  45.   for (auto it = tp.begin(); it != tp.end(); it++) {
  46.     count[it->second]++;
  47.   }
  48.  
  49.   if (count[5] == 1) {
  50.     return 0;
  51.   }
  52.   if (count[4] == 1) {
  53.     return 1;
  54.   }
  55.   if ((count[3] == 1 && count[1] == 2) || count[2] == 2) {
  56.     return 2;
  57.   }
  58.   if ((count[2] == 1 && count[1] == 3) || (count[3] == 1 && count[2] == 1)) {
  59.     return 3;
  60.   }
  61.   if (count[1] == 5) {
  62.     return 4;
  63.   }
  64. }
  65.  
  66. void countStirling(int d)
  67. {
  68.   StirlingNumb.resize(d + 1);
  69.   for (int i(0); i <= d; ++i) {
  70.     StirlingNumb[i].resize(d + 1);
  71.     StirlingNumb[i][0] = 0;
  72.     StirlingNumb[i][i] = 1;
  73.   }
  74.  
  75.   for (int i(1); i < StirlingNumb.size(); ++i) {
  76.     for (int j(1); j < StirlingNumb[i].size(); ++j) {
  77.       StirlingNumb[i][j] = j * StirlingNumb[i - 1][j] + StirlingNumb[i - 1][j - 1];
  78.     }
  79.   }
  80. }
  81.  
  82. int main()
  83. {
  84.   long long m(4294967296);
  85.   long long a(69069);
  86.   long long c(0);
  87.   long long x0(1);
  88.   long long maxX(m - 1);
  89.  
  90.   int d(5);
  91.   vA.resize(d);
  92.  
  93.   long long n(100000);
  94.   long long l(maxX / d);
  95.   double p(1.0 / d);
  96.  
  97.   long long x(x0);
  98.   for (int i(0); i < n; ++i) {
  99.     vA[x / l]++;
  100.     x = (a * x + c) % m;
  101.   }
  102.  
  103.   double V(0);
  104.   for (int i(0); i < d; ++i) {
  105.     V += pow(vA[i] - n * p, 2.0) / (1.0 * n * p);
  106.   }
  107.  
  108.   double chi(chiSquared(0.95, d - 1));
  109.  
  110.   if (V < chi) {
  111.     cout << "Satisfy criterion A\n";
  112.   }
  113.   else {
  114.     cout << "Don't satisfy criterion A\n";
  115.   }
  116.   //-----------------------------------------------------
  117.   vB.resize(d);
  118.   for (int i(0); i < d; ++i) {
  119.     vB[i].resize(d);
  120.   }
  121.  
  122.   x = x0;
  123.   long long aa, bb;
  124.   for (int i(0); i < n / 2; ++i) {
  125.     aa = x / l;
  126.     x = (a * x + c) % m;
  127.     bb = x / l;
  128.     vB[aa][bb]++;
  129.   }
  130.  
  131.   p = 1.0 / (d * d);
  132.   V = 0;
  133.   for (int i(0); i < d; ++i) {
  134.     for (int j(0); j < d; ++j) {
  135.       V += pow(vB[i][j] - n / 2 * p, 2.0) / (1.0 * n / 2 * p);
  136.     }
  137.   }
  138.  
  139.   chi = chiSquared(0.95, d * d - 1);
  140.  
  141.   if (V < chi) {
  142.     cout << "Satisfy criterion B\n";
  143.   }
  144.   else {
  145.     cout << "Don't satisfy criterion B\n";
  146.   }
  147.   //------------------------------------------------------------
  148.   x = x0;
  149.   vector<long long> vals(5);
  150.   vector<long long> vD(5);
  151.   int type;
  152.   for (int i(0); i < n / 5; i += 5) {
  153.     for (int j(0); j < 5; ++j) {
  154.       vals[j] = x;
  155.       x = (a * x + c) % m;
  156.     }
  157.     type = checkType(vals);
  158.     vD[type]++;
  159.   }
  160.  
  161.   countStirling(d);
  162.   vector<double> pr(5);
  163.   for (int i(0); i < 5; ++i) {
  164.     pr[i] = 1;
  165.     int r(i + 1);
  166.     for (int j(1); j <= r; ++j) {
  167.       pr[i] *= (d + 1 - j);
  168.     }
  169.     pr[i] /= pow(d, 5);
  170.     pr[i] *= StirlingNumb[5][r];
  171.   }
  172.  
  173.   V = 0;
  174.   for (int i(0); i < 5; ++i) {
  175.     V += pow(vD[i] - 5 * pr[i], 2.0) / (5.0 * pr[i]);
  176.   }
  177.  
  178.   chi = chiSquared(0.95, 4);
  179.  
  180.   if (V < chi) {
  181.     cout << "Satisfy criterion D\n";
  182.   }
  183.   else {
  184.     cout << "Don't satisfy criterion D\n";
  185.   }
  186.  
  187.   return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement