fork download
  1. /*
  2. knowing lcm(x, y) = x * y / gcd(x, y)
  3. then we can calculate the answer for pairs with common gcd easily using modified sieve
  4. we have only one problem that we will calculate the answer for some pairs more than one time,
  5. so we need to exclude ans of multiples of our gcd so far.
  6. almost the same idea could be found here codeforces.com/blog/entry/62621?#comment-465836
  7. */
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10.  
  11. const int N = 1e5 + 55, mod = 1e9 + 7;
  12. int t, n, x, freq[N];
  13. long long ans[N];
  14. int pw(long long x, int b){
  15. if(!b) return 1;
  16. if(b&1) return (x * pw(x, b - 1)) % mod;
  17. return pw((x * x) % mod, b / 2);
  18. }
  19. int modinv(int x){
  20. return pw(x, mod - 2);
  21. }
  22. int solve(){
  23. long long res = 0;
  24. for(int i = N - 1; i; --i){
  25. vector<int> v;
  26. long long sm = 0;
  27. for(int j = i; j < N; j += i){
  28. ans[i] -= ans[j];
  29. for(int x = 0; x < freq[j]; ++x){
  30. v.push_back(j);
  31. sm += j;
  32. }
  33. }
  34.  
  35. for(int x: v){
  36. //sm -= x;
  37. ans[i] = (ans[i] + sm * x) % mod;
  38. }
  39. res = (res + ans[i] * modinv(i)) % mod;
  40. }
  41. return res;
  42. }
  43. int main(){
  44. ios::sync_with_stdio(0); cin.tie(0);
  45. freopen("lcm.in", "r", stdin);
  46. cin >> t;
  47. while(t--){
  48. cin >> n;
  49. memset(freq, 0, sizeof(freq));
  50. memset(ans, 0, sizeof(ans));
  51. for(int i = 0; i < n; ++i){
  52. cin >> x;
  53. ++freq[x];
  54. }
  55. cout << solve() << '\n';
  56. }
  57. return 0;
  58. }
Runtime error #stdin #stdout #stderr 0s 16408KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::ios_base::failure[abi:cxx11]'
  what():  basic_filebuf::underflow error reading the file: iostream error