-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10306.cpp
More file actions
executable file
·92 lines (75 loc) · 1.51 KB
/
10306.cpp
File metadata and controls
executable file
·92 lines (75 loc) · 1.51 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
int trip[301][2];
void fun(int arr[],int size,int val)
{
int dp[305]={0};
for(int i=1; i<=val; i++)
{
int q=1000000;
for(int j=0; j<size; j++)
{
if(arr[j]<=i)
{
q=min(q,1+dp[i-arr[j]]);
}
}
dp[i]=q;
}
if(dp[val]==1000000)cout<<"not possible"<<endl;
else cout<<dp[val]<<endl;
return;
}
void pythagoreanTriplets(int limit)
{
int a, b, c = 0;
int m = 2;
while (c < limit) {
for (int n = 1; n < m; ++n) {
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if (c > limit)
break;
trip[c][0]=a;
trip[c][1]=b;
}
m++;
}
}
int main()
{
for(int i=0; i<301; i++)
{
trip[i][0]=-1;
trip[i][1]=-1;
}
pythagoreanTriplets(301);
int n;
cin>>n;
int m,s;
while(n--)
{
cin>>m>>s;
int a,b;
int sum[m];
int t1=0,t2=0,sz=0;
bool check[305][305];
for(int i=0; i<305; i++)
for(int j=0; j<305; j++)check[i][j]=false;
for(int i=0; i<m; i++)
{
cin>>a>>b;
t1+=a; t2+=b;
if(check[a][b]==0)
{
sum[sz++]=a+b;
check[a][b]=1;
}
}
if(t1!=0 && t2!=0 && (trip[s][0]>0 && trip[s][1]>0))
fun(sum,sz,trip[s][0]+trip[s][1]);
else
fun(sum,sz,s);
}
}