Guest User

Untitled

a guest
Sep 20th, 2014
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <map>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. const int mod = 1000000007;
  9.  
  10. int t;
  11. int n;
  12. map <int, int> M;
  13. int res;
  14.  
  15. int toPower(int a, int p)
  16. {
  17.     int r = 1;
  18.     while (p) {
  19.         if (p & 1) r = ll(r) * ll(a) % ll(mod);
  20.         p >>= 1; a = ll(a) * ll(a) % ll(mod);
  21.     }
  22.     return r;
  23. }
  24.  
  25. int main()
  26. {
  27.     scanf("%d", &t);
  28.     for (int tc = 1; tc <= t; tc++) {
  29.         M.clear();
  30.         scanf("%d", &n);
  31.         while (n--) {
  32.             int a; scanf("%d", &a);
  33.             M[a]++;
  34.         }
  35.         res = 0;
  36.         for (map <int, int>::iterator it = M.begin(); it != M.end(); it++) if (it->second) {
  37.             int inv = toPower(it->first, mod - 2);
  38.             int lft = it->first == inv? it->second / 2: M.count(inv)? M[inv]: 0;
  39.             if (!lft) continue;
  40.             int tk = min(it->second, lft);
  41.             it->second -= tk; M[inv] -= tk; res += tk;
  42.         }
  43.         printf("Case #%d: %d\n", tc, res);
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment