1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 #include <ext/rope> 15 16 using namespace std; 17 using namespace __gnu_cxx; 18 19 #define REP(i, n) for (int i = 0; i < (n); ++i) 20 #define eps 1e-9 21 #define SZ(x) ((int)x.size()) 22 #define PI acos(-1.0) 23 24 typedef long long ll; 25 typedef unsigned long long ull; 26 typedef pair<int, int> pii; 27 const int maxn = 1e4 + 10; 28 const int mod = 1e4 + 7; 29 int n, prime_cnt = 0, Case = 0; 30 int prime[maxn], mu[maxn], sum[maxn], cnt[maxn]; 31 bool is_prime[maxn]; 32 33 void init() { 34 memset(is_prime, true, sizeof(is_prime)); 35 is_prime[0] = is_prime[1] = false; mu[1] = 1; 36 for (int i = 2; i < maxn; ++i) { 37 if (is_prime[i]) { prime[prime_cnt++] = i; mu[i] = -1; } 38 for (int j = 0; j < prime_cnt; ++j) { 39 if (i * prime[j] >= maxn) { break; } 40 is_prime[i * prime[j]] = false; 41 if (i % prime[j] == 0) { 42 mu[i * prime[j]] = 0; break; 43 } 44 mu[i * prime[j]] = -mu[i]; 45 } 46 } 47 } 48 49 int main() { 50 #ifdef __AiR_H 51 freopen("in.txt", "r", stdin); 52 // freopen("out.txt", "w", stdout); 53 #endif // __AiR_H 54 init(); int a; 55 while (scanf("%d", &n) != EOF) { 56 memset(cnt, 0, sizeof(cnt)); memset(sum, 0, sizeof(sum)); 57 int Max = 0; ll ans = 0, t; 58 for (int i = 0; i < n; ++i) { 59 scanf("%d", &a); ++cnt[a]; Max = max(Max, a); 60 } 61 for (int i = 2; i <= Max; ++i) { 62 for (int j = i; j <= Max; j += i) { sum[i] += cnt[j]; } 63 } 64 for (int i = 2; i <= Max; ++i) { 65 t = 0; 66 for (int j = i; j <= Max; j += i) { 67 t += 1ll * mu[j / i] * sum[j] * sum[j]; t %= mod; 68 } 69 ans += t * i * (i - 1); ans %= mod; 70 } 71 printf("%lld ", ans); 72 } 73 return 0; 74 }
1 Codeforces 839D Winter is here
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 #include <ext/rope> 15 16 using namespace std; 17 using namespace __gnu_cxx; 18 19 #define REP(i, n) for (int i = 0; i < (n); ++i) 20 #define eps 1e-9 21 #define SZ(x) ((int)x.size()) 22 #define PI acos(-1.0) 23 24 typedef long long ll; 25 typedef unsigned long long ull; 26 typedef pair<int, int> pii; 27 const int maxn = 1e6 + 10; 28 const int mod = 1e9 + 7; 29 int n, prime_cnt = 0; 30 int prime[maxn], mu[maxn], sum[maxn], cnt[maxn]; 31 bool is_prime[maxn]; 32 33 void init() { 34 memset(is_prime, true, sizeof(is_prime)); 35 is_prime[0] = is_prime[1] = false; mu[1] = 1; 36 for (int i = 2; i < maxn; ++i) { 37 if (is_prime[i]) { prime[prime_cnt++] = i; mu[i] = -1; } 38 for (int j = 0; j < prime_cnt; ++j) { 39 if (i * prime[j] >= maxn) { break; } 40 is_prime[i * prime[j]] = false; 41 if (i % prime[j] == 0) { 42 mu[i * prime[j]] = 0; break; 43 } 44 mu[i * prime[j]] = -mu[i]; 45 } 46 } 47 } 48 ll my_pow(ll x, ll y) { 49 ll ret = 1, t = x; 50 while (y) { 51 if (y & 1) { ret *= t; ret %= mod; } 52 y >>= 1; t *= t; t %= mod; 53 } 54 return ret; 55 } 56 57 int main() { 58 #ifdef __AiR_H 59 freopen("in.txt", "r", stdin); 60 // freopen("out.txt", "w", stdout); 61 #endif // __AiR_H 62 init(); int a; 63 while (scanf("%d", &n) != EOF) { 64 memset(cnt, 0, sizeof(cnt)); memset(sum, 0, sizeof(sum)); 65 int Max = 0; ll ans = 0, t; 66 for (int i = 0; i < n; ++i) { 67 scanf("%d", &a); ++cnt[a]; Max = max(Max, a); 68 } 69 for (int i = 2; i <= Max; ++i) { 70 for (int j = i; j <= Max; j += i) { sum[i] += cnt[j]; } 71 } 72 for (int i = 2; i <= Max; ++i) { 73 t = 0; 74 for (int j = i; j <= Max; j += i) { 75 if (!sum[j]) { continue; } 76 t += 1ll * mu[j / i] * sum[j] * my_pow(2, sum[j] - 1); t %= mod; 77 } 78 ans += t * i; ans %= mod; 79 } 80 printf("%lld ", ans); 81 } 82 return 0; 83 }