From 60991f8e72c4fb2cfa7440fdaf92a849ab15a0ff Mon Sep 17 00:00:00 2001 From: Linkus000 Date: Thu, 16 Dec 2021 22:20:40 +0100 Subject: [PATCH] Primitive root --- primitive_root.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 primitive_root.cpp diff --git a/primitive_root.cpp b/primitive_root.cpp new file mode 100644 index 0000000..c762527 --- /dev/null +++ b/primitive_root.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#define pb push_back +#define mp make_pair +#define all(a) begin(a),end(a) +#define FOR(x,val,to) for(int x=(val);x pi; +typedef std::vector vi; +typedef std::vector vs; +typedef int64_t ll; +typedef uint64_t ull; +#define umap unordered_map +#define uset unordered_set +using namespace std; +using namespace __gnu_pbds; + +#ifdef ONLINE_JUDGE +#define whatis(x) ; +#endif +#ifdef _WIN32 +#define getchar_unlocked() _getchar_nolock() +#define _CRT_DISABLE_PERFCRIT_LOCKS +#endif +template ostream& operator<<(ostream &os, map P) { for(auto const &vv: P)os<<"("< ostream& operator<<(ostream &os, set V) { os<<"[";for(auto const &vv:V)os< ostream& operator<<(ostream &os, vector V) { os<<"[";for(auto const &vv:V)os< ostream& operator<<(ostream &os, pair P) { os<<"("<32){str+=cur;cur=getchar_unlocked();}} +template inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } +template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound +template using ordered_map = tree, rb_tree_tag, tree_order_statistics_node_update>; +#define N 1000001 + + +// Primitive root modulo n exists if and only if: +// n is 1, 2, 4, or +// n is power of an odd prime number (n=p^k), or +// n is twice power of an odd prime number (n=2*p^k). +// https://cp-algorithms.com/algebra/primitive-root.html +// The following code assumes that the modulo p is a prime number. To make it works for any value of p, we must add calculation of ϕ(p). +int powmod (int a, int b, int p) { + int res = 1; + while (b) + if (b & 1) + res = int (res * 1ll * a % p), --b; + else + a = int (a * 1ll * a % p), b >>= 1; + return res; +} + +int generator (int p) { + vector fact; + int phi = p-1, n = phi; + for (int i=2; i*i<=n; ++i) + if (n % i == 0) { + fact.push_back (i); + while (n % i == 0) + n /= i; + } + if (n > 1) + fact.push_back (n); + + for (int res=2; res<=p; ++res) { + bool ok = true; + for (size_t i=0; i