-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10306.cpp
93 lines (65 loc) · 1.26 KB
/
uva10306.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
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
93
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
vector<pair<int, int> > coins;
int dp[325][325];
void createDP()
{
dp[0][0] = 0;
for(int i = 0; i <= 300; i++)
{
for(int j = 0; j <= 300; j++)
{
if(i > 0 || j > 0)
{
int best = INT_MAX;
for(int index = 0; index < coins.size(); index++)
{
if(i >= coins[index].first && j >= coins[index].second
&& dp[i-coins[index].first][j-coins[index].second] != -1)
best = min(best, dp[i-coins[index].first][j-coins[index].second]);
}
if(best != INT_MAX)
dp[i][j] = 1 + best;
}
}
}
}
int main()
{
int tc; cin >> tc;
int m, sum;
while(tc--)
{
cin >> m >> sum;
coins.resize(m);
for(int i = 0; i < coins.size(); i++)
{
cin >> coins[i].first;
cin >> coins[i].second;
}
memset(dp, -1, sizeof(dp));
createDP();
int rez = INT_MAX;
for(int i = 0; i <= sum; i++)
{
for(int j = 0; j <= sum; j++)
{
if(dp[i][j] != -1 && i * i + j * j == sum * sum)
rez = min(rez, dp[i][j]);
}
}
if(rez != INT_MAX)
cout << rez << endl;
else
cout << "not possible" << endl;
}
return 0;
}