Guest User

Untitled

a guest
Apr 15th, 2023
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.61 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #  include <cstdio>         // fputs(), puts(), printf()
  3. #  include <cstdlib>        // size_t, malloc(), free()
  4. #  include <cstring>        // memset()
  5. #  include <cinttypes>      // uint64_t, uintmax_t
  6. #else
  7. #  include <stdio.h>        // fputs(), puts(), printf()
  8. #  include <stdlib.h>       // size_t, malloc(), free()
  9. #  include <string.h>       // memset()
  10. #  include <inttypes.h>     // uint64_t, uintmax_t
  11. #endif
  12.                 // vector word type
  13.  
  14.  
  15. #include <iostream>
  16.  
  17.  
  18.  
  19.  
  20. #include <iostream>
  21. using namespace std;
  22.  
  23.  
  24. constexpr uint32_t __high_type(uint16_t) {return uint32_t(0);}
  25. constexpr uint64_t __high_type(uint32_t) {return uint64_t(0);}
  26. constexpr __uint128_t __high_type(uint64_t) {return __uint128_t(0);}
  27.  
  28. #define high_type(x) decltype(__high_type(x(0)))
  29.  
  30. template<typename T>
  31. class fast_div{
  32. protected:
  33.     high_type(T) my_magic = 1;
  34.     unsigned char shift;
  35.  
  36. public:
  37.     fast_div(){}
  38.     fast_div(T val){
  39.         shift = (sizeof(high_type(T)) * 8 - 1);
  40.         my_magic = (my_magic << shift) / val;
  41.  
  42.         while(my_magic >= (high_type(T))(1) << (sizeof(high_type(T)) * 4) ) {
  43.             my_magic >>= 1;--shift;
  44.         }
  45.  
  46.         if (!((++my_magic) & 1)){my_magic >>= 1; --shift;}
  47.     }
  48.  
  49.  
  50.     __attribute__((always_inline)) friend T  operator/(const T& val, const fast_div<T>& fd){
  51.         return ((fd.my_magic * val) >> fd.shift);
  52.     }
  53. };
  54.  
  55. template<typename T>
  56. class fast_mod : fast_div<T>{
  57. private:
  58.     T div;
  59. public:
  60.     fast_mod(){}
  61.     fast_mod(T val) : fast_div<T>(val), div(val){}
  62.     __attribute__((always_inline)) friend T  operator%(const T& val, const fast_mod<T>& fm){
  63.         return val - val/fm * fm.div;
  64.     }
  65. };
  66.  
  67.  
  68. #include <vector>
  69. vector<int> minmax(int a, int b){
  70.     if (a < b) return {a,b};
  71.     return {b,a};
  72. }
  73.  
  74. template<typename T>
  75. class LCM{
  76. public:
  77.     T mult, old_rand, add;
  78.     fast_mod<T> fm;
  79.  
  80.     LCM(T _mult,T _old_rand,T _add,T _mod) : mult(_mult), old_rand(_old_rand), add(_add){
  81.         fm = _mod;
  82.     }
  83.  
  84.     __attribute__((always_inline)) T get(){
  85.         return old_rand = (mult * old_rand + add) % fm;
  86.     }
  87.  
  88. };
  89.  
  90. #include <iostream>
  91. using namespace std;
  92. // fastmod computes (n mod d) given precomputed c
  93.  
  94. typedef unsigned int u128 __attribute__((mode(TI)));
  95.  
  96.  
  97.  
  98.  
  99.  
  100. #include <cmath>
  101. bool isPrimeSimple(int n){
  102.     if (n < 2) return false;
  103.     int limit = sqrt(n) + 1;
  104.     for(int i = 2;i<limit;++i){
  105.         if (!(n % i)) return false;
  106.     }
  107.     return true;
  108. }
  109.  
  110. uint16_t bases[]={15591,2018,166,7429,8064,16045,10503,4399,1949,1295,2776,3620,560,3128,5212,
  111. 2657,2300,2021,4652,1471,9336,4018,2398,20462,10277,8028,2213,6219,620,3763,4852,5012,3185,
  112. 1333,6227,5298,1074,2391,5113,7061,803,1269,3875,422,751,580,4729,10239,746,2951,556,2206,
  113. 3778,481,1522,3476,481,2487,3266,5633,488,3373,6441,3344,17,15105,1490,4154,2036,1882,1813,
  114. 467,3307,14042,6371,658,1005,903,737,1887,7447,1888,2848,1784,7559,3400,951,13969,4304,177,41,
  115. 19875,3110,13221,8726,571,7043,6943,1199,352,6435,165,1169,3315,978,233,3003,2562,2994,10587,
  116. 10030,2377,1902,5354,4447,1555,263,27027,2283,305,669,1912,601,6186,429,1930,14873,1784,1661,
  117. 524,3577,236,2360,6146,2850,55637,1753,4178,8466,222,2579,2743,2031,2226,2276,374,2132,813,
  118. 23788,1610,4422,5159,1725,3597,3366,14336,579,165,1375,10018,12616,9816,1371,536,1867,10864,
  119. 857,2206,5788,434,8085,17618,727,3639,1595,4944,2129,2029,8195,8344,6232,9183,8126,1870,3296,
  120. 7455,8947,25017,541,19115,368,566,5674,411,522,1027,8215,2050,6544,10049,614,774,2333,3007,
  121. 35201,4706,1152,1785,1028,1540,3743,493,4474,2521,26845,8354,864,18915,5465,2447,42,4511,
  122. 1660,166,1249,6259,2553,304,272,7286,73,6554,899,2816,5197,13330,7054,2818,3199,811,922,350,
  123. 7514,4452,3449,2663,4708,418,1621,1171,3471,88,11345,412,1559,194};
  124.  
  125. /// http://ceur-ws.org/Vol-1326/020-Forisek.pdf
  126. bool is_SPRP(uint32_t n, uint32_t a) {
  127.     uint32_t d = n-1, s = 0;
  128.     while ((d&1)==0) ++s, d>>=1;
  129.     uint64_t cur = 1, pw = d;
  130.     while (pw) {
  131.         if (pw & 1) cur = (cur*a) % n;
  132.         a = ((uint64_t)a*a) % n;
  133.         pw >>= 1;
  134.     }
  135.     if (cur == 1) return true;
  136.     for (uint32_t r=0; r<s; r++) {
  137.         if (cur == n-1) return true;
  138.         cur = (cur*cur) % n;
  139.     }
  140. return false;
  141. }
  142.  
  143. /// check factor
  144. /// check factor
  145. #define cf(t) if (!(x%t)){ return (x==t);}
  146.  
  147.  
  148. #define _5c(a,b,c,d,e) cf(a) cf(b) cf(c) cf(d) cf(e)
  149.  
  150. bool is_prime_WTF(uint32_t x) {
  151.     _5c(2,3,5,7,11)
  152.     _5c(13,17,19,23,29)
  153.     _5c(31,37,41,43,47)
  154.     _5c(53,59,61,67,71)
  155.     _5c(73,79,83,89,97)
  156.     if (x<121) return (x>1);
  157.     uint64_t h = x;
  158.     h = ((h >> 16) ^ h) * 0x45d9f3b;
  159.     h = ((h >> 16) ^ h) * 0x45d9f3b;
  160.     h = ((h >> 16) ^ h) & 255;
  161.     return is_SPRP(x,bases[h]);
  162. }
  163.  
  164.  
  165.  
  166. constexpr uint8_t type_log_2(uint32_t) {return 5;}
  167. constexpr uint8_t type_log_2(uint64_t) {return 6;}
  168. constexpr uint8_t type_log_2(__uint128_t) {return 7;}
  169.  
  170.  
  171. template < typename block_type >
  172. class Sieve{
  173.  
  174. public:
  175.     block_type *prime;
  176.  
  177.     const static uint8_t VEC_BIT =  sizeof(block_type) * 8;
  178.     const static uint8_t VEC_LOG = type_log_2(block_type(0));
  179.  
  180.     static inline unsigned get(block_type *vec, uintmax_t k) {
  181.         return (vec[k >> VEC_LOG] >> (k & (VEC_BIT - 1))) & 1;
  182.     }
  183.     // Clear bit k in the bit vector vec.
  184.     static inline void clear(block_type *vec, uintmax_t k) {
  185.         vec[k >> VEC_LOG] &= ~((block_type)1 << (k & (VEC_BIT - 1)));
  186.     }
  187.  
  188. public:
  189.  
  190.     Sieve(size_t n){
  191.  
  192.  
  193.         n = n ? (n - 1) >> 1 : 0;   // half of n bits for just the odds
  194.         n |= VEC_BIT - 1;           // round up to fill the last word
  195.         size_t len = (n >> 3) + 1;
  196.  
  197.         prime = (block_type *)malloc(len);
  198.  
  199.         len >>= VEC_LOG - 3;
  200.         prime[0] = ~1;                      // 1 is not prime
  201.         for (unsigned k = 1, p = 3, q = 1; q < VEC_BIT/4;
  202.                       k++, p += 2, q += k)
  203.             if (get(prime, k)) {
  204.                 unsigned next = q << 2;     // start at p squared
  205.                 do {
  206.                     clear(prime, next);
  207.                     next += p;
  208.                 } while (next < VEC_BIT);
  209.             }
  210.         if (len == 1)
  211.             return;
  212.         // sum the odd primes in the first word, in order to allocate
  213.         // space for p masks for each odd prime p -- initialize the
  214.         // masks to all ones
  215.         unsigned sum = 0;
  216.         for (unsigned k = 1; k < VEC_BIT; k++)
  217.             if (get(prime, k))
  218.                 sum += 2 * k + 1;
  219.         sum <<= VEC_LOG - 3;        // bytes for s vec_t's
  220.         block_type *mask = (block_type *)malloc(sum);
  221.  
  222.         memset(mask, ~0, sum);
  223.         // build a set of repeating masks for the primes from 3 to
  224.         // 2*VEC_BIT, for which one or more bits are reset in every
  225.         // word, where off is the offset of the current p masks in
  226.         // mask[]
  227.         for (unsigned k = 1, p = 3, off = 0; k < VEC_BIT;
  228.                       ++k, p += 2)
  229.             if (get(prime, k)) {
  230.                 size_t next = (k + VEC_BIT * (p - 1)) % p;
  231.                 size_t end = (p > len - 1 ? len - 1 :
  232.                                             (size_t)p) << VEC_LOG;
  233.                 while (next < end) {
  234.                     clear(mask + off, next);
  235.                     next += p;
  236.                 }
  237.                 off += p;
  238.             }
  239.         // now sieve the entire bit vector after the first word using
  240.         // the repeating masks -- do a chunk at a time to take
  241.         // advantage of the processor cache (the chunk size was
  242.         // determined empirically, buying half a second over going
  243.         // through the entire bit vector for each prime's set of masks)
  244.  
  245.         size_t start = 1;
  246.         do {
  247.             // last is the last vec_t index of this chunk, not past
  248.             // the end
  249.             size_t end = start + ((size_t)1 << 12);
  250.             if (end > len || end < start)
  251.                 end = len;
  252.             // initialize this chunk to all 1's
  253.             memset(prime + start, ~0,
  254.                   (end - start) << (VEC_LOG - 3));
  255.             // apply masks for each prime to the indices
  256.             // start..end-1 -- for speed, handle the edge cases
  257.             // outside of the center loop, which does the vast
  258.             // majority of the work
  259.             for (unsigned k = 1, p = 3, off = 0;
  260.                           k < VEC_BIT; k++, p += 2)
  261.                 if (get(prime, k)) {
  262.                     size_t next = start;
  263.                     size_t i = (start - 1) % p;
  264.                     do {
  265.                         prime[next++] &= mask[off + i++];
  266.                     } while (i < p && next < end);
  267.                     while (end - next >= p) {
  268.                         i = 0;
  269.                         do {
  270.                             prime[next + i] &= mask[off + i];
  271.                         } while (++i < p);
  272.                         next += p;
  273.                     }
  274.                     i = 0;
  275.                     while (next < end)
  276.                         prime[next++] &= mask[off + i++];
  277.                     off += p;
  278.                 }
  279.             start = end;
  280.         } while (start < len);
  281.  
  282.         // done with masks
  283.         free(mask);
  284.         // sieve from the smallest prime greater than twice VEC_BIT to
  285.         // the square root of largest integer represented in the bit
  286.         // vector, for which zero or one bits are reset in each word
  287.         // for each prime -- break up the job into chunks of the
  288.         // vector, going through all of the primes for each chunk (the
  289.         // chunk size was determined empirically -- this is about three
  290.         // times as fast as going through the entire bit vector for
  291.         // each prime)
  292.  
  293.         start = 0;
  294.         do {
  295.             // last is the last bit location for this chunk, not
  296.             // past the end
  297.             uintmax_t last = start + (((uintmax_t)1 << 19) - 1);
  298.             if (last > n || last < start)
  299.                 last = n;
  300.             // clear composites at bit locations start..last
  301.             uintmax_t k = VEC_BIT;
  302.             for (;;) {
  303.                 if (get(prime, k)) {
  304.                     // start at the location of p squared (before
  305.                     // there, primes less than p already cleared
  306.                     // multiples of p)
  307.                     uintmax_t next = 2 * k * (k + 1);
  308.                     // p is the prime that corresponds to bit
  309.                     // location k
  310.                     uintmax_t p = 2 * k + 1;
  311.                     // if the square of p is past the end of this
  312.                     // chunk, then all of the subsequent primes
  313.                     // will be too
  314.                     if (next > last)
  315.                         break;
  316.                     // add just enough p's to the bit location to
  317.                     // put it in this chunk (this effectively adds
  318.                     // just enough 2*p's to the corresponding odd
  319.                     // integer)
  320.                     if (next < start) {
  321.                         next = k + p - start % p;
  322.                         if (next >= p)
  323.                             next -= p;
  324.                         next += start;
  325.                     }
  326.                     // clear composites, effectively adding 2*p to
  327.                     // the current odd integer in order to get to
  328.                     // the next odd integer
  329.                     while (next <= last) {
  330.                         clear(prime, next);
  331.                         next += p;
  332.                     }
  333.                 }
  334.                 k++;
  335.             }
  336.             // go to the next chunk
  337.             start = last + 1;
  338.         } while (start <= n);
  339.  
  340.     }
  341.  
  342.     bool isPrime(int num){
  343.         if (num < 3) return num == 2;
  344.         if (num % 2 == 0) return false;
  345.         num /= 2;
  346.         return prime[num / 64] & (1ull << (num % 64));
  347.     }
  348.  
  349.     ~Sieve(){
  350.         free(prime);
  351.     }
  352. };
  353.  
  354. #include <vector>
  355. uint64_t factorize(int number){
  356.     uint64_t ret = 0;
  357.     while (!(number % 2)){
  358.         number /= 2;
  359.         ret += 2;
  360.     }
  361.     for(int factor = 3; factor * factor <= number; factor += 2 ){
  362.         while ( !(number % factor) ){
  363.             number /= factor;
  364.             ret += factor;
  365.         }
  366.     }
  367.     if(number != 1) ret += number;
  368.     return ret;
  369. }
  370.  
  371. using namespace std;
  372. int main(void) {
  373.     // generate the primes vector
  374.  
  375.  
  376.  
  377.     Sieve<uint64_t> S(1001 * 1000 * 1000);
  378.  
  379.     int a,ol, b,c, n;
  380.     cin >> a >> ol >> b >> c >> n;
  381.     LCM<uint64_t> myl(a,ol,b,c);
  382.     uint64_t ans = 0;
  383.     for(int i = 0;i< n;++i){
  384.         ans += S.isPrime( myl.get());
  385.     }
  386.     cout << ans;
  387.  
  388.  
  389.     // clean up
  390.  
  391.     return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment