peltorator

!ratio template

May 19th, 2019
156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef long long ll;
  2. typedef long double ld;
  3.  
  4. ll gcd(ll a, ll b)
  5. {
  6.     while (a)
  7.     {
  8.         b %= a;
  9.         swap(a, b);
  10.     }
  11.     return b;
  12. }
  13.  
  14. /*--------------------------------------BEGIN OF RAT----------------------------------*/
  15.  
  16. struct rat
  17. {
  18.     ll x, y;
  19.  
  20.     rat(ll xx, ll yy = 1)
  21.     {
  22.         x = xx;
  23.         y = yy;
  24.         if (y < 0)
  25.         {
  26.             x = -x;
  27.             y = -y;
  28.         }
  29.     }
  30.  
  31.     rat()
  32.     {
  33.         x = 0;
  34.         y = 1;
  35.     }
  36.  
  37.     void norm()
  38.     {
  39.         ll t = abs(gcd(x, y));
  40.         x /= t;
  41.         y /= t;
  42.     }
  43.  
  44.     ld to_ld()
  45.     {
  46.         return (ld)x / (ld)y;
  47.     }
  48.  
  49.     ll to_ll()
  50.     {
  51.         return x / y;
  52.     }
  53.  
  54. /*----------------------------------------cmp with rat---------------------------------*/
  55.  
  56.     bool operator<(const rat &other) const
  57.     {
  58.         return x * other.y < y * other.x;
  59.     }
  60.    
  61.     bool operator>(const rat &other) const
  62.     {
  63.         return x * other.y > y * other.x;
  64.     }
  65.  
  66.     bool operator<=(const rat &other) const
  67.     {
  68.         return x * other.y <= y * other.x;
  69.     }
  70.  
  71.     bool operator>=(const rat &other) const
  72.     {
  73.         return x * other.y >= y * other.x;
  74.     }
  75.  
  76.     bool operator==(const rat &other) const
  77.     {
  78.         return x * other.y == y * other.x;
  79.     }
  80.  
  81.     bool operator!=(const rat &other) const
  82.     {
  83.         return x * other.y != y * other.x;
  84.     }
  85.  
  86. /*---------------------------------------ops with rat----------------------------*/
  87.  
  88.     rat operator*(const rat &other) const
  89.     {
  90.         return rat(x * other.x, y * other.y);
  91.     }
  92.  
  93.     void operator*=(const rat &other)
  94.     {
  95.         x *= other.x;
  96.         y *= other.y;
  97.     }
  98.  
  99.     rat operator/(const rat &other) const
  100.     {
  101.         if (other.x > 0)
  102.             return rat(x * other.y, y * other.x);
  103.         else
  104.             return rat(-x * other.y, -y * other.x);
  105.     }
  106.  
  107.     void operator/=(const rat &other)
  108.     {
  109.         x *= other.y;
  110.         y *= other.x;
  111.         if (y < 0)
  112.         {
  113.             y = -y;
  114.             x = -x;
  115.         }
  116.     }
  117.  
  118.     rat operator+(const rat &other) const
  119.     {
  120.         return rat(x * other.y + y * other.x, y * other.y);
  121.     }
  122.  
  123.     void operator+=(const rat &other)
  124.     {
  125.         x = x * other.y + y * other.x;
  126.         y *= other.y;
  127.     }
  128.  
  129.     rat operator-(const rat &other) const
  130.     {
  131.         return rat(x * other.y - y * other.x, y * other.y);
  132.     }
  133.  
  134.     void operator-=(const rat &other)
  135.     {
  136.         x = x * other.y - y * other.x;
  137.         y *= other.y;
  138.     }
  139.  
  140. /*----------------------------------------cmp with ll--------------------------------*/
  141.  
  142.     bool operator<(const ll &k) const
  143.     {
  144.         return x < y * k;
  145.     }
  146.    
  147.     bool operator>(const ll &k) const
  148.     {
  149.         return x > y * k;
  150.     }
  151.  
  152.     bool operator<=(const ll &k) const
  153.     {
  154.         return x <= y * k;
  155.     }
  156.  
  157.     bool operator>=(const ll &k) const
  158.     {
  159.         return x >= y * k;
  160.     }
  161.  
  162.     bool operator==(const ll &k) const
  163.     {
  164.         return x == y * k;
  165.     }
  166.  
  167.     bool operator!=(const ll &k) const
  168.     {
  169.         return x != y * k;
  170.     }
  171.    
  172. /*----------------------------------------ops with ll--------------------------------*/
  173.  
  174.     rat operator*(const ll &k) const
  175.     {
  176.         return rat(x * k, y);
  177.     }
  178.  
  179.     void operator*=(const ll &k)
  180.     {
  181.         x *= k;
  182.     }
  183.  
  184.     rat operator/(const ll &k) const
  185.     {
  186.         if (k > 0)
  187.             return rat(x, y * k);
  188.         else
  189.             return rat(-x, -y * k);
  190.     }
  191.  
  192.     void operator/=(const ll &k)
  193.     {
  194.         y *= k;
  195.         if (y < 0)
  196.         {
  197.             y = -y;
  198.             x = -x;
  199.         }
  200.     }
  201.  
  202.     rat operator+(const ll &k) const
  203.     {
  204.         return rat(x + y * k, y);
  205.     }
  206.  
  207.     void operator+=(const ll &k)
  208.     {
  209.         x += y * k;
  210.     }
  211.  
  212.     rat operator-(const ll &k) const
  213.     {
  214.         return rat(x - y * k, y);
  215.     }
  216.  
  217.     void operator-=(const ll &k)
  218.     {
  219.         x -= y * k;
  220.     }
  221. };
  222.  
  223. /*----------------------------------------cmp with ll--------------------------------*/
  224.  
  225. bool operator<(const ll &k, const rat &v)
  226. {
  227.     return k * v.y < v.x;
  228. }
  229.  
  230. bool operator>(const ll &k, const rat &v)
  231. {
  232.     return k * v.y > v.x;
  233. }
  234.  
  235. bool operator<=(const ll &k, const rat &v)
  236. {
  237.     return k * v.y <= v.x;
  238. }
  239.  
  240. bool operator>=(const ll &k, const rat &v)
  241. {
  242.     return k * v.y >= v.x;
  243. }
  244.  
  245. bool operator==(const ll &k, const rat &v)
  246. {
  247.     return k * v.y == v.x;
  248. }
  249.  
  250. bool operator!=(const ll &k, const rat &v)
  251. {
  252.     return k * v.y != v.x;
  253. }
  254.  
  255. /*----------------------------------------ops with ll--------------------------------*/
  256.  
  257. rat operator*(const ll &k, const rat &v)
  258. {
  259.     return rat(v.x * k, v.y);
  260. }
  261.  
  262. rat operator/(const ll &k, const rat &v)
  263. {
  264.     if (v.x > 0)
  265.         return rat(k * v.y, v.x);
  266.     else
  267.         return rat(-k * v.y, -v.x);
  268. }
  269.  
  270. rat operator+(const ll &k, const rat &v)
  271. {
  272.     return rat(v.x + v.y * k, v.y);
  273. }
  274.  
  275. rat operator-(const ll &k, const rat &v)
  276. {
  277.     return rat(v.y * k - v.x, v.y);
  278. }
  279.  
  280. /*--------------------------------------END OF RAT-----------------------------------*/
  281.  
  282. /* rat(x) // x
  283.  * rat(x, y) // x/y
  284.  * rat() // 0/1
  285.  * .norm() // /gcd
  286.  * .to_ld
  287.  * .to_ll
  288.  * < > <= >= == != (also for ll and in reverse order)
  289.  * * *= / /= + += - -= (also for ll and in reverse order)
  290.  */
RAW Paste Data