-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP5868.cpp
67 lines (62 loc) · 1.42 KB
/
P5868.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "header.h"
int gcd(int a, int b)
{
return a%b?gcd(b,a%b):b;
}
struct hasher
{
size_t operator() (const pair<int, int> &k) const
{
return hash<long>{}(k.first*1e5+k.second);
}
};
struct comparer
{
bool operator() (const pair<int, int> &a, const pair<int, int> &b) const
{
return a.first == b.first && a.second == b.second;
}
};
class Solution {
public:
long long interchangeableRectangles(vector<vector<int>>& rectangles) {
using int2 = pair<int, int>;
unordered_map<int2, int, hasher, comparer> table;
int n = rectangles.size();
for (int i=0; i < n; i++)
{
int w,h;
w = rectangles[i][0];
h = rectangles[i][1];
int g = gcd(w, h);
while (g>1)
{
w /= g;
h /= g;
g = gcd(w,h);
}
int2 key(w,h);
if (table.count(key)==0)
{
table[key] = 1;
}
else
{
table[key]+=1;
}
}
long long ans = 0;
for (auto &ele : table)
{
long long val = ele.second;
ans += val*(val-1)/2;
}
return ans;
}
};
int main()
{
vector<vector<int>> inp = {{4,8}, {3,6}, {10,20}, {15,30}};
cout << Solution().interchangeableRectangles(inp) << "\n";
return 0;
}