Skip to content

Commit e9aeec0

Browse files
authored
Create 1925. Count Square Sum Triples (#953)
2 parents 944a8b5 + d6165e4 commit e9aeec0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1925. Count Square Sum Triples

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
static int GCD(unsigned x, unsigned y){
4+
const int bx=countr_zero(x), by=countr_zero(y);
5+
const int bb=min(bx, by);
6+
x>>=bx, y>>=by;
7+
for(unsigned r; y; y=min(y, r)){
8+
r=x%y;
9+
x=exchange(y, r);
10+
}
11+
return x<<bb;
12+
}
13+
static int countTriples(int n) {
14+
int cnt=0;
15+
int nsqrt=sqrt(n);
16+
for (int s=2; s<=nsqrt; s++) {
17+
for (int t=1+(s&1); t<s; t+=2) {
18+
if (GCD(s, t)!=1) continue;
19+
20+
// int a=s*s-t*t;
21+
// int b=2*s*t;
22+
int c=s*s+t*t;
23+
24+
if (c>n) break;
25+
26+
// k multiples: ka, kb, kc
27+
int kmax=n/c;
28+
// count (a,b,c) and (b,a,c)
29+
cnt+=2*kmax;
30+
}
31+
}
32+
return cnt;
33+
}
34+
};

0 commit comments

Comments
 (0)