From 06f6eab4f244db011de77cc72d361e8dd87af96e Mon Sep 17 00:00:00 2001 From: HoaNP Date: Fri, 27 Oct 2017 00:07:02 +0700 Subject: [PATCH 1/2] calculate sum of GCD(i,j) of all pairs --- GreatestCommonDivisor/C++/gcd_sum.cpp | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 GreatestCommonDivisor/C++/gcd_sum.cpp diff --git a/GreatestCommonDivisor/C++/gcd_sum.cpp b/GreatestCommonDivisor/C++/gcd_sum.cpp new file mode 100644 index 000000000..fe7b28220 --- /dev/null +++ b/GreatestCommonDivisor/C++/gcd_sum.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +#define ms(s, n) memset(s, n, sizeof(s)) +#define FOR(i, a, b) for (int i = (a); i < (b); i++) +#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--) +#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) +#define sz(a) int((a).size()) +#define present(t, x) (t.find(x) != t.end()) +#define all(a) (a).begin(), (a).end() +#define uni(a) (a).erase(unique(all(a)), (a).end()) +#define pb push_back +#define pf push_front +#define mp make_pair +#define fi first +#define se second +#define prec(n) fixed<> (i)) & 1) +#define bitcount(n) __builtin_popcountll(n) +typedef long long ll; +typedef unsigned long long ull; +typedef long double ld; +typedef pair pi; +typedef vector vi; +typedef vector vii; +const int MOD = (int) 1e9 + 7; +const int MOD2 = (int) 1e8 + 7; +const int INF = (int) 1e9; +const ll LINF = (ll) 1e18; +const ld PI = acos((ld) -1); +const ld EPS = 1e-9; +inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} +inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} +inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} +template inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} +template inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} +inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;} +inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} +inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} +inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} +inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} +inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} +inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} +inline int sign(ld x, ld y) {return sign(x - y);} +#define db(x) cerr << #x << " = " << (x) << ", "; +#define endln cerr << "\n"; + +//Given a number N, find sum of all GCDs that can be formed by selecting all the pairs from 1 to N. + +const int N = 12; +int phi[N + 1]; +int prime[N + 1]; +long long G[N + 1]; + +int main() { + // Compute phi[n] + for (int p = 1; p <= N; ++ p) phi[p] = p; + for (int p = 2; p <= N; ++ p) + if (phi[p] == p) + for (int n = p; n <= N; n += p) + phi[n] = phi[n]/p*(p-1); + + // Compute prime[n] -> Largest prime that divides n + for (int p = 1; p <= N; ++ p) prime[p] = 0; + for (int p = 2; p <= N; ++ p) + if (prime[p] == 0) + for (int n = p; n <= N; n += p) prime[n] = max(prime[n], p); + + G[1] = 1; + for (int n = 2; n <= N; ++ n) { + int p = 1; + int k = 0; + while (p <= n/prime[n] && n % (p * prime[n]) == 0) { + p *= prime[n]; + ++ k; + } + G[n] = ((k+1)*p - k*p/prime[n]) * G[n/p]; + } + + G[1] = G[1] - 1; + for (int n = 2; n <= N; ++ n) G[n] += G[n-1] - n; + cout << G[N] << endl; +} + + From 009070f5f957e40f71f9778b904373e089b14132 Mon Sep 17 00:00:00 2001 From: HoaNP <7907509+HoaNP@users.noreply.github.com> Date: Thu, 4 Oct 2018 00:19:56 +0700 Subject: [PATCH 2/2] remove redundant --- GreatestCommonDivisor/C++/gcd_sum.cpp | 43 --------------------------- 1 file changed, 43 deletions(-) diff --git a/GreatestCommonDivisor/C++/gcd_sum.cpp b/GreatestCommonDivisor/C++/gcd_sum.cpp index fe7b28220..e3a8f776a 100644 --- a/GreatestCommonDivisor/C++/gcd_sum.cpp +++ b/GreatestCommonDivisor/C++/gcd_sum.cpp @@ -1,49 +1,6 @@ #include using namespace std; -#define ms(s, n) memset(s, n, sizeof(s)) -#define FOR(i, a, b) for (int i = (a); i < (b); i++) -#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--) -#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) -#define sz(a) int((a).size()) -#define present(t, x) (t.find(x) != t.end()) -#define all(a) (a).begin(), (a).end() -#define uni(a) (a).erase(unique(all(a)), (a).end()) -#define pb push_back -#define pf push_front -#define mp make_pair -#define fi first -#define se second -#define prec(n) fixed<> (i)) & 1) -#define bitcount(n) __builtin_popcountll(n) -typedef long long ll; -typedef unsigned long long ull; -typedef long double ld; -typedef pair pi; -typedef vector vi; -typedef vector vii; -const int MOD = (int) 1e9 + 7; -const int MOD2 = (int) 1e8 + 7; -const int INF = (int) 1e9; -const ll LINF = (ll) 1e18; -const ld PI = acos((ld) -1); -const ld EPS = 1e-9; -inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} -inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} -inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} -template inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} -template inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} -inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;} -inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} -inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} -inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} -inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} -inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} -inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} -inline int sign(ld x, ld y) {return sign(x - y);} -#define db(x) cerr << #x << " = " << (x) << ", "; -#define endln cerr << "\n"; //Given a number N, find sum of all GCDs that can be formed by selecting all the pairs from 1 to N.