Advertisement
wery00

Untitled

Jan 28th, 2023
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. // #pragma GCC target("avx,avx2,fma")
  3.  
  4. #include "bits/stdc++.h"
  5.  
  6. //#define NDEBUG
  7. #define F first
  8. #define S second
  9. #define vec vector
  10. #define pb push_back
  11. #define pll pair<ll, ll>
  12. #define pdd pair<ld, ld>
  13. #define pii pair<int, int>
  14. #define all(m) m.begin(), m.end()
  15. #define rall(m) m.rbegin(), m.rend()
  16. #define uid uniform_int_distribution
  17. #define timeStamp() std::chrono::steady_clock::now()
  18. #define unify(m) sort(all(m)); m.erase(unique(all(m)), m.end());
  19. #define duration_micro(a) chrono::duration_cast<chrono::microseconds>(a).count()
  20. #define duration_milli(a) chrono::duration_cast<chrono::milliseconds>(a).count()
  21. #define fast cin.tie(0); cout.tie(0); cin.sync_with_stdio(0); cout.sync_with_stdio(0);
  22. using namespace std;
  23. using str = string;
  24. using ll = long long;
  25. using ld = long double;
  26. using uint = unsigned int;
  27. using ull = unsigned long long;
  28. mt19937 rnd(timeStamp().time_since_epoch().count());
  29. mt19937_64 rndll(timeStamp().time_since_epoch().count());
  30. template<typename T> constexpr inline int sign(T x) {return x < 0 ? -1 : x > 0 ? 1 : 0;}
  31. template<typename T, typename U> bool chmin(T& a, const U& b) {return (T)b < a ? a = b, 1 : 0;}
  32. template<typename T, typename U> bool chmax(T& a, const U& b) {return (T)b > a ? a = b, 1 : 0;}
  33. constexpr inline uint leq_pow2(const uint x) {return 1u << __lg(x);}
  34. constexpr inline ull leq_pow2ll(const ull x) {return 1ull << __lg(x);}
  35. constexpr inline uint geq_pow2(const uint x) {return x & (x - 1) ? 2u << __lg(x) : x;}
  36. constexpr inline ull geq_pow2ll(const ull x) {return x & (x - 1) ? 2ull << __lg(x) : x;}
  37. constexpr inline ll sqd(const pll p1, const pll p2) {return (p1.F - p2.F) * (p1.F - p2.F) + (p1.S - p2.S) * (p1.S - p2.S);}
  38. constexpr inline ll sqd(const ll x1, const ll y1, const ll x2, const ll y2) {return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);}
  39. struct custom_hash {static uint64_t xs(uint64_t x) {x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);} template<typename T> size_t operator()(T x) const {static const uint64_t C = timeStamp().time_since_epoch().count(); return xs(hash<T> {}(x) + C);}};
  40. template<typename K> using uset = unordered_set<K, custom_hash>;
  41. template<typename K, typename V> using umap = unordered_map<K, V, custom_hash>;
  42. template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.F << ' ' << x.S;}
  43. template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.F >> x.S;}
  44. template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {for (auto &x : a) in >> x; return in;};
  45. template<typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {for (int q = 0; q < a.size(); ++q) {out << a[q]; if (q + 1 < a.size()) out << ' ';} return out;};
  46. template<typename T> istream& operator>>(istream& in, vector<T>& a) {for (auto &x : a) in >> x; return in;};
  47. template<typename T> ostream& operator<<(ostream& out, const vector<T>& a) {for (int q = 0; q < a.size(); ++q) {out << a[q]; if (q + 1 < a.size()) out << ' ';} return out;};
  48.  
  49. //Fast Sieve of Eratosthenes with const optimizations.
  50. //Returns vector of all primes in range [1, n].
  51. vector<int> gen_primes(int n) {
  52.     if (n <= 2) {if (n == 2) return {2}; return {};}
  53.     vector<int> o = {2, 3}; o.reserve(n / log(n));
  54.     vector<bool> is_prime(n + 1, true);
  55.     is_prime[0] = is_prime[1] = 0;
  56.     for (int w = 4; w <= n; w += 2) is_prime[w] = 0;
  57.     for (int w = 3; w <= n; w += 6) is_prime[w] = 0;
  58.     const int gr = sqrtl(n) + 1;
  59.     for (int w = 6; w <= gr; w += 6) {
  60.         for (int df : { -1, 1}) {
  61.             const int q = w + df;
  62.             if (q > n) break;
  63.             if (!is_prime[q]) continue;
  64.             for (int w = q * q; w <= n; w += q * 2) {
  65.                 is_prime[w] = 0;
  66.             }
  67.         }
  68.     }
  69.     for (int w = 6; w - 1 <= n; w += 6) {
  70.         for (int df : { -1, 1}) {
  71.             const int q = w + df;
  72.             if (q <= n && is_prime[q]) o.push_back(q);
  73.         }
  74.     }
  75.     return o;
  76. }
  77.  
  78. int main() {
  79.     fast;
  80.     auto primes = gen_primes(1000);
  81.     for (int p : primes) {
  82.         int c = 0;
  83.         auto f = [&](auto && f, int x) -> int{
  84.             if (x < 0) return x;
  85.             int nxt = x / 10 + c * (x % 10);
  86.             return nxt >= x ? x : f(f, nxt);
  87.         };
  88.         vec<int> good;
  89.         for (int l = -p; l <= p; ++l) {
  90.             c = l;
  91.             int fl = 1;
  92.             for (int q = 0; q < 100000 && fl; ++q) {
  93.                 int res = f(f, q);
  94.                 fl &= (res % p == 0) == (q % p == 0);
  95.             }
  96.             if (fl) good.push_back(c);
  97.         }
  98.         assert(count(all(good), 0) == 0);
  99.         for (; good.size() > 1 && end(good)[-2] > 0;) good.pop_back();
  100.         if (good.size() > 2) good.erase(good.begin(), good.end() - 2);
  101.         cout << p << ": " << good << endl;
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement