-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPythagoreanTriplets.cpp
More file actions
65 lines (56 loc) · 1.04 KB
/
PythagoreanTriplets.cpp
File metadata and controls
65 lines (56 loc) · 1.04 KB
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
#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
struct triplet{
int a,b,c;
};
int countTriplets(int n){
int ans=0;
stack<struct triplet> s;
struct triplet temp;
temp.a=3;
temp.b=4;
temp.c=5;
s.push(temp);
while(!s.empty())
{
int a,b,c;
a = s.top().a;
b= s.top().b;
c=s.top().c;
s.pop();
ans+=n/c;
//Case 1: 1-22 2-12 2-23
temp.a = a -2*b + 2*c;
temp.b = 2*a - b + 2*c;
temp.c = 2*a - 2*b + 3*c;
if(temp.a<=n&&temp.b<=n&&temp.c<=n){
s.push(temp);
}
//Case 2: 122 212 223
temp.a = a + 2*b + 2*c;
temp.b = 2*a + b + 2*c;
temp.c = 2*a + 2*b + 3*c;
if(temp.a<=n&&temp.b<=n&&temp.c<=n){
s.push(temp);
}
//Case 3: -122 -212 -223
temp.a = -a + 2*b + 2*c;
temp.b = -2*a + b + 2*c;
temp.c = -2*a + 2*b + 3*c;
if(temp.a<=n&&temp.b<=n&&temp.c<=n){
s.push(temp);
}
}
return ans;
}
int main(){
int t,n;
cin>>t;
while(t--){
scanf("%d",&n);
printf("%d\n",countTriplets(n));
}
return 0;
}