Guest User

Untitled

a guest
Apr 15th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. constexpr uint32_t __high_type(uint16_t) {return uint32_t(0);}
  6. constexpr uint64_t __high_type(uint32_t) {return uint64_t(0);}
  7. constexpr __uint128_t __high_type(uint64_t) {return __uint128_t(0);}
  8.  
  9. #define high_type(x) decltype(__high_type(x(0)))
  10. #define FORCE_INLINE __attribute__((always_inline)) inline
  11.  
  12. template<typename T, typename H = high_type(T)>
  13. class fast_div{
  14. protected:
  15.     H my_magic = 1;
  16.     unsigned char shift;
  17.  
  18. public:
  19.     fast_div(){}
  20.     fast_div(T val){
  21.         shift = (sizeof(H) * 8 - 1);
  22.         my_magic = (my_magic << shift) / val;
  23.  
  24.         while(my_magic >= H(1) << (sizeof(H) * 4) ) {
  25.             my_magic >>= 1;--shift;
  26.         }
  27.  
  28.         if (!((++my_magic) & 1)){my_magic >>= 1; --shift;}
  29.     }
  30.  
  31.  
  32.     FORCE_INLINE friend T  operator/(const T& val, const fast_div<T>& fd){
  33.         return ((fd.my_magic * val) >> fd.shift);
  34.     }
  35. };
  36.  
  37. uint64_t ret, low;
  38. /// Возвращает большую часть от 128: 64x64
  39. FORCE_INLINE uint64_t hmul64(const uint64_t& a,const uint64_t& b){
  40.     asm ("mulq %3" : "=a"(low), "=d"(ret) : "a"(a), "g"(b) : "cc");
  41.     return ret;
  42. }
  43.  
  44. class fast_div64{
  45. protected:
  46.     uint64_t my_magic = 1;
  47.     uint_fast8_t shift;
  48.  
  49. public:
  50.     fast_div64(){}
  51.     fast_div64(const uint64_t& val){
  52.         shift = (sizeof(high_type(uint64_t)) * 8 - 1);
  53.  
  54.         __uint128_t temp = (__uint128_t(1) << 127) / val;
  55.         shift = __builtin_clzll(temp  >> 64); /// zeros
  56.         my_magic = (temp >> (64-shift));
  57.         --shift;
  58.  
  59.         if (!((++my_magic) & 1)){my_magic >>= 1; --shift;}
  60.         ///cout << my_magic << " " << (int)shift << endl;
  61.     }
  62.  
  63.  
  64.     __attribute__((always_inline)) friend uint64_t operator/(const uint64_t& val, const fast_div64& fd){
  65.  
  66.  
  67.  
  68.         return (hmul64(val,fd.my_magic) >> fd.shift ) ;
  69.     }
  70. };
  71.  
  72. template<typename T>
  73. class fast_mod : fast_div<T>{
  74. private:
  75.     T div;
  76. public:
  77.     fast_mod(){}
  78.     fast_mod(T val) : fast_div<T>(val), div(val){}
  79.     FORCE_INLINE friend T  operator%(const T& val, const fast_mod<T>& fm){
  80.         return val - val/fm * fm.div;
  81.     }
  82. };
  83.  
  84. class fast_mod64 : fast_div64{
  85. private:
  86.     uint64_t div;
  87. public:
  88.     fast_mod64(){}
  89.     fast_mod64(uint64_t val) : fast_div64(val), div(val){}
  90.     FORCE_INLINE friend uint64_t operator%(const uint64_t& val, const fast_mod64& fm){
  91.         return val - val/fm * fm.div;
  92.     }
  93. };
  94.  
  95.  
  96. template<typename T, typename quick_mod = fast_mod<T>>
  97. class LCM{
  98. public:
  99.     T mult, old_rand, add;
  100.     quick_mod fm;
  101.     LCM(T _mult,T _old_rand,T _add,T _mod) : mult(_mult), old_rand(_old_rand), add(_add), fm(_mod){}
  102.     FORCE_INLINE T get(){
  103.         return old_rand = (mult * old_rand + add) % fm;
  104.     }
  105.  
  106. };
  107.  
  108. #include <iostream>
  109. using namespace std;
  110. // fastmod computes (n mod d) given precomputed c
  111.  
  112. typedef unsigned int u128 __attribute__((mode(TI)));
  113.  
  114.  
  115.  
  116.  
  117.  
  118. #include <cmath>
  119. bool isPrimeSimple(int n){
  120.     if (n < 2) return false;
  121.     int limit = sqrt(n) + 1;
  122.     for(int i = 2;i<limit;++i){
  123.         if (!(n % i)) return false;
  124.     }
  125.     return true;
  126. }
  127.  
  128. uint16_t bases[]={15591,2018,166,7429,8064,16045,10503,4399,1949,1295,2776,3620,560,3128,5212,
  129. 2657,2300,2021,4652,1471,9336,4018,2398,20462,10277,8028,2213,6219,620,3763,4852,5012,3185,
  130. 1333,6227,5298,1074,2391,5113,7061,803,1269,3875,422,751,580,4729,10239,746,2951,556,2206,
  131. 3778,481,1522,3476,481,2487,3266,5633,488,3373,6441,3344,17,15105,1490,4154,2036,1882,1813,
  132. 467,3307,14042,6371,658,1005,903,737,1887,7447,1888,2848,1784,7559,3400,951,13969,4304,177,41,
  133. 19875,3110,13221,8726,571,7043,6943,1199,352,6435,165,1169,3315,978,233,3003,2562,2994,10587,
  134. 10030,2377,1902,5354,4447,1555,263,27027,2283,305,669,1912,601,6186,429,1930,14873,1784,1661,
  135. 524,3577,236,2360,6146,2850,55637,1753,4178,8466,222,2579,2743,2031,2226,2276,374,2132,813,
  136. 23788,1610,4422,5159,1725,3597,3366,14336,579,165,1375,10018,12616,9816,1371,536,1867,10864,
  137. 857,2206,5788,434,8085,17618,727,3639,1595,4944,2129,2029,8195,8344,6232,9183,8126,1870,3296,
  138. 7455,8947,25017,541,19115,368,566,5674,411,522,1027,8215,2050,6544,10049,614,774,2333,3007,
  139. 35201,4706,1152,1785,1028,1540,3743,493,4474,2521,26845,8354,864,18915,5465,2447,42,4511,
  140. 1660,166,1249,6259,2553,304,272,7286,73,6554,899,2816,5197,13330,7054,2818,3199,811,922,350,
  141. 7514,4452,3449,2663,4708,418,1621,1171,3471,88,11345,412,1559,194};
  142.  
  143. FORCE_INLINE auto ctz(const uint64_t& num){
  144.     return __builtin_ctzll(num);
  145. }
  146.  
  147. FORCE_INLINE auto ctz(const uint32_t& num){
  148.     return __builtin_ctz(num);
  149. }
  150.  
  151. template<typename unsign>
  152. FORCE_INLINE unsign sqr(const unsign& num){
  153.     return num*num;
  154. }
  155.  
  156. /// важно, чтобы тип unsign позволял вместить в себя произведение mod*mod. aka избавляйтесь от переполнения
  157. template<typename unsign, typename power_type, typename modulus_obj>
  158. FORCE_INLINE unsign modul_pow(unsign num, power_type power, const modulus_obj& mod){
  159.     unsign cur = 1;
  160.     for(;power;power >>= 1) {
  161.         if (power & 1) cur = (cur*num) % mod;
  162.         num = sqr(num) % mod;
  163.     }
  164.     return cur;
  165. }
  166.  
  167. /// http://ceur-ws.org/Vol-1326/020-Forisek.pdf
  168. bool is_SPRP(uint32_t n, const uint64_t& a) {
  169.  
  170.     fast_mod64 fm_n(n);
  171.     uint_fast8_t s = ctz(--n);
  172.     uint64_t cur = modul_pow(a, n>>s, fm_n);
  173.     if (cur == 1) return true;
  174.     for (uint_fast8_t r=0; r<s; ++r) {
  175.         if (cur == n) return true;
  176.         cur = sqr(cur) % fm_n;
  177.     }
  178.     return false;
  179. }
  180.  
  181. /// check factor
  182. /// check factor
  183. #define cf(t) if (!(x%t)){ return (x==t);}
  184.  
  185.  
  186. #define _5c(a,b,c,d,e) cf(a) cf(b) cf(c) cf(d) cf(e)
  187.  
  188. bool is_prime_WTF(uint32_t x) {
  189.     _5c(2,3,5,7,11)
  190.     _5c(13,17,19,23,29)
  191.     _5c(31,37,41,43,47)
  192.     _5c(53,59,61,67,71)
  193.     _5c(73,79,83,89,97)
  194.     if (x<121) return (x>1);
  195.     uint64_t h = x;
  196.     h = ((h >> 16) ^ h) * 0x45d9f3b;
  197.     h = ((h >> 16) ^ h) * 0x45d9f3b;
  198.     h = ((h >> 16) ^ h) & 255;
  199.     return is_SPRP(x,bases[h]);
  200. }
  201.  
  202.  
  203.  
  204.  
  205. /// возвращает большее, можно дать ссылку для меньшего
  206.  
  207.  
  208. using namespace std;
  209. int main(void) {
  210.  
  211.     int a,ol, b,c, n;
  212.     cin >> a >> ol >> b >> c >> n;
  213.     LCM<uint64_t, fast_mod64> myl(a,ol,b,c);
  214.     uint64_t ans = 0;
  215.     for(int i = 0;i< n;++i){
  216.         ans += is_prime_WTF( myl.get());
  217.     }
  218.     cout << ans;
  219.  
  220.  
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment