Advertisement
Guest User

Problem I

a guest
Jul 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1000010;
  5.  
  6. int t;
  7. int n;
  8. int freq[N];
  9. int num;
  10. int arr[10000];
  11.  
  12. long long sum(int x)
  13. {
  14.     if(x == 0)
  15.         return 0;
  16.     return (x*(x+1))/2;
  17. }
  18.  
  19. int main()
  20. {
  21.     ios::sync_with_stdio(0);
  22.     cin.tie(NULL);
  23.     cout.tie(NULL);
  24.  
  25.     #ifndef ONLINE_JUDGE
  26.         //freopen("input.txt", "r", stdin);
  27.         //freopen("out.txt", "w", stdout);
  28.     #endif // ONLINE_JUDGE
  29.  
  30.     cin >> t;
  31.     while(t--)
  32.     {
  33.         scanf("%d",&n);
  34.         int idx = 0;
  35.         for(int i = 0; i < n; i++)
  36.         {
  37.             scanf("%d",&num);
  38.             if(!freq[num])
  39.                 arr[idx++] = num;
  40.             freq[num]++;
  41.         }
  42.         long long ans = 0;
  43.         for(int i = 0; i < idx;i++)
  44.         {
  45.             for(int j = 0; j<idx; j++)
  46.             {
  47.                 if(arr[j]%arr[i] == 0)
  48.                 {
  49.                     if(arr[i] == arr[j])
  50.                         ans+=sum(freq[arr[i]]-1);
  51.                     else
  52.                         ans+= (freq[arr[i]]*freq[arr[j]]);
  53.                 }
  54.             }
  55.         }
  56.         printf("%I64d\n",ans);
  57.         memset(freq,0,sizeof freq);
  58.  
  59.     }
  60.  
  61.  
  62.  
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement