Guest User

Untitled

a guest
Oct 2nd, 2011
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. double __forceinline GetMinMaxX1(double a,double b, double c, double d, double e, double f)
  6. {
  7.     return (4*c*d - 2*b*e - sqrt((-4*c*d + 2*b*e)*(-4*c*d + 2*b*e) -
  8.         4*(b*b - 4*a*c)*(e*e - 4*c*f)))/(2.*(b*b - 4*a*c));
  9. }
  10. double __forceinline GetMinMaxX2(double a,double b, double c, double d, double e, double f)
  11. {
  12.     return (4*c*d - 2*b*e + sqrt((-4*c*d + 2*b*e)*(-4*c*d + 2*b*e) -
  13.         4*(b*b - 4*a*c)*(e*e - 4*c*f)))/(2.*(b*b - 4*a*c));
  14. }
  15.  
  16. int __forceinline GetMinX(double a,double b, double c, double d, double e, double f)
  17. {
  18.     return static_cast<int>(floor(min(GetMinMaxX1(a,b,c,d,e,f),GetMinMaxX2(a,b,c,d,e,f))));
  19. }
  20.  
  21. int __forceinline GetMaxX(double a,double b, double c, double d, double e, double f)
  22. {
  23.     return static_cast<int>(ceil(max(GetMinMaxX1(a,b,c,d,e,f),GetMinMaxX2(a,b,c,d,e,f))));
  24. }
  25.  
  26. double __forceinline GetLower(double a,double b, double c, double d, double e, double f, double x)
  27. {
  28.     return (-e - b*x - sqrt((e + b*x)*(e + b*x) - 4*c*(f + d*x + a*x*x)))/(2*c);
  29. }
  30.  
  31. double __forceinline GetUpper(double a,double b, double c, double d, double e, double f, double x)
  32. {
  33.     return (-e - b*x + sqrt((e + b*x)*(e + b*x) - 4*c*(f + d*x + a*x*x)))/(2*c);
  34. }
  35.  
  36. bool __forceinline IsInt(double i)
  37. {
  38.     static double intpart;
  39.     return modf(i, &intpart) == 0.0;
  40. }
  41.  
  42. bool __forceinline IsEllipse(double a,double b, double c, double d, double e, double f)
  43. {
  44.     return (b*b-4*a*c) < 0;
  45. }
  46.  
  47. bool __forceinline IsEvaluable(double a,double b, double c, double d, double e, double f, double x)
  48. {
  49.     return ((e + b*x)*(e + b*x) - 4*c*(f + d*x + a*x*x)) >= 0;
  50. }
  51.  
  52. int __forceinline GetNumLatticePoints(double a, double b, double c, double d, double e, double f)
  53. {
  54.     if(!IsEllipse(a,b,c,d,e,f))
  55.         return 0;
  56.  
  57.     int iMin = GetMinX(a,b,c,d,e,f);
  58.     int iMax = GetMaxX(a,b,c,d,e,f);
  59.     int iLattice = 0;
  60.  
  61.     for(int i = iMin; i <= iMax; ++i)
  62.     {
  63.         double x = static_cast<double>(i);
  64.         if(IsEvaluable(a,b,c,d,e,f,x))
  65.         {
  66.             double dLower = GetLower(a,b,c,d,e,f,x);
  67.             double dUpper = GetUpper(a,b,c,d,e,f,x);
  68.             if(IsInt(dLower))
  69.                 ++iLattice;
  70.             if(dLower != dUpper && IsInt(dUpper))
  71.                 ++iLattice;
  72.         }
  73.     }
  74.     return iLattice;
  75. }
  76.  
  77. int main()
  78. {
  79.     for(double a = -20; a <= 20; ++a)
  80.     {
  81.         for(double b = -20; b <= 20; ++b)
  82.         {
  83.             for(double c = -20; c <= 20; ++c)
  84.             {
  85.                 for(double d = -20; d <= 20; ++d)
  86.                 {
  87.                     for(double e = -20; e <= 20; ++e)
  88.                     {
  89.                         for(double f = -20; f <= 20; ++f)
  90.                         {
  91.                             if(GetNumLatticePoints(a,b,c,d,e,f) == 17)
  92.                             {
  93.                                 cout << "a = " << a <<  "; b = " << b << "; c = " << c << "; d = " << d << "; e = " << e << "; f = " << f << endl;
  94.                             }
  95.                         }
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101.     system("pause");
  102. }
  103.  
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment