Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using std::sort;
- using std::max;
- using std::vector;
- long long gcd(long long a, long long b)
- {
- if (b == 0)
- {
- return a;
- }
- return gcd(b, a%b);
- }
- void add_triple(long long a, long long b, long long c, vector< vector<long long> >& result)
- {
- vector<long long> triple(3, 0);
- triple[0] = c;
- triple[1] = a;
- triple[2] = b;
- result.push_back(triple);
- }
- bool triples_comp(const vector<long long> first_triple, const vector<long long> second_triple)
- {
- if (first_triple[0] == second_triple[0] && first_triple[1] == second_triple[1])
- {
- return first_triple[2] < second_triple[2];
- }
- if (first_triple[0] == second_triple[0])
- {
- return first_triple[1] < second_triple[1];
- }
- return first_triple[0] < second_triple[0];
- }
- int main(void)
- {
- int length;
- std::cin >> length;
- vector< vector<long long> > triples;
- int threshold = (int) sqrt(length) + 1;
- for (int n = 1; n <= threshold; ++n)
- {
- for (int m = n + 1; m <= threshold; ++m)
- {
- long long a = m*m - n*n;
- long long b = 2*m*n;
- long long c = m*m + n*n;
- if (c > length)
- {
- break;
- }
- if (gcd(m, n) == 1)
- {
- add_triple(a, b, c, triples);
- int k = 2;
- while (k * c <= length)
- {
- add_triple(k*a, k*b, k*c, triples);
- ++k;
- }
- }
- }
- }
- sort(triples.begin(), triples.end(), triples_comp);
- vector<long long> answer(length + 1, 0);
- for(auto i = triples.begin(); i != triples.end(); ++i)
- {
- answer[(*i)[0]] = max(answer[(*i)[0]], max(answer[(*i)[1]], answer[(*i)[2]]) + 1);
- }
- std::cout << answer[length] << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement