Advertisement
am1x

erd001.cxx

Aug 16th, 2022 (edited)
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <inttypes.h>
  5. #include <endian.h>
  6. #include <vector>
  7.  
  8. typedef unsigned int uint_t;
  9. typedef long double real;
  10.  
  11. template<typename T> class Adder
  12. {
  13.     std::vector<T> v;
  14.  
  15.  public:
  16.     void clear() {
  17.         v.clear();
  18.         v.push_back(0);
  19.     }
  20.  
  21.     Adder() {
  22.         clear();       
  23.     }
  24.  
  25.     void add(T x) {
  26.         size_t i = 0;
  27.         for (i = 0; v[i]; i++) {
  28.             x += v[i];
  29.             v[i] = 0;
  30.         }
  31.         v[i] = x;
  32.         if (i + 1 == v.size())
  33.             v.push_back(0);
  34.     }
  35.  
  36.     T value() {
  37.         T res = v[0];
  38.         size_t l = v.size();
  39.         for (size_t i = 1; i < l; i++)
  40.             res += v[i];
  41.         return res;
  42.     }
  43. };
  44.  
  45.  
  46. static void work(uint64_t n0, uint64_t n1)
  47. {
  48.     assert (n0 < n1);
  49.     assert ((n1 - n0) % 2 != 0);
  50.  
  51.     Adder<real> s;
  52.     uint64_t buf[2];
  53.  
  54.     for (uint64_t i = n0; i <= n1; i += 2) {
  55.         size_t cnt = fread(buf, sizeof(buf[0]), 2, stdin);
  56.         assert (cnt == 2);
  57.         buf[0] = be64toh(buf[0]);
  58.         buf[1] = be64toh(buf[1]);
  59. //      fprintf(stderr, "p0=%" PRIu64 ", p1=%" PRIu64 "\n", buf[0], buf[1]);
  60.         real p0 = buf[0], p1 = buf[1];
  61.         real d = (p0 - (p1 - p0) * i) / (p0 * p1);
  62.         s.add(d);
  63.     }
  64.     printf("%.24Lf\n", s.value());
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. int main(int ac, char **av)
  72. {
  73.     uint64_t as[2] = {0, 0};
  74.     assert (ac == 3);
  75.     for (uint_t i = 0; i < 2; i++) {
  76.         char *eptr = 0;
  77.         as[i] = strtoull(av[1 + i], &eptr, 0);
  78.         if (as[i] == 0 || eptr == av[1 + i] || eptr == 0 || *eptr != 0) {
  79.             fprintf(stderr, "wrong argument %u: '%s' \n", i, av[1 + i]);
  80.         }
  81.         fprintf(stderr, "as[%u] = %" PRIu64 " \n", i, as[i]);
  82.     }
  83.  
  84.     assert (as[0] < as[1]);
  85.     assert ((as[1] - as[0]) % 2 != 0);
  86.  
  87.     work(as[0], as[1]);
  88.    
  89.  
  90.  
  91.     return 0;
  92. }
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement