Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::sort;
  6. using std::max;
  7. using std::vector;
  8.  
  9. long long gcd(long long a, long long b)
  10. {
  11.     if (b == 0)
  12.     {
  13.         return a;
  14.     }
  15.     return gcd(b, a%b);
  16. }
  17.  
  18. void add_triple(long long a, long long b, long long c, vector< vector<long long> >& result)
  19. {
  20.     vector<long long> triple(3, 0);
  21.     triple[0] = c;
  22.     triple[1] = a;
  23.     triple[2] = b;
  24.     result.push_back(triple);
  25. }
  26.  
  27. bool triples_comp(const vector<long long> first_triple, const vector<long long> second_triple)
  28. {
  29.     if (first_triple[0] == second_triple[0] && first_triple[1] == second_triple[1])
  30.     {
  31.         return first_triple[2] < second_triple[2];
  32.     }
  33.     if (first_triple[0] == second_triple[0])
  34.     {
  35.         return first_triple[1] < second_triple[1];
  36.     }
  37.     return first_triple[0] < second_triple[0];
  38. }
  39.  
  40. int main(void)
  41. {
  42.     int length;
  43.     std::cin >> length;
  44.    
  45.     vector< vector<long long> > triples;
  46.     int threshold = (int) sqrt(length)  + 1;
  47.     for (int n = 1; n <= threshold; ++n)
  48.     {
  49.         for (int m = n + 1; m <= threshold; ++m)
  50.         {
  51.             long long a = m*m - n*n;
  52.             long long b = 2*m*n;
  53.             long long c = m*m + n*n;
  54.            
  55.             if (c > length)
  56.             {
  57.                 break;
  58.             }
  59.                
  60.             if (gcd(m, n) == 1)
  61.             {
  62.                 add_triple(a, b, c, triples);
  63.                 int k = 2;
  64.                 while (k * c <= length)
  65.                 {
  66.                     add_triple(k*a, k*b, k*c, triples);
  67.                     ++k;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.    
  73.     sort(triples.begin(), triples.end(), triples_comp);
  74.    
  75.     vector<long long> answer(length + 1, 0);
  76.    
  77.     for(auto i = triples.begin(); i != triples.end(); ++i)
  78.     {
  79.         answer[(*i)[0]] = max(answer[(*i)[0]], max(answer[(*i)[1]], answer[(*i)[2]]) + 1);
  80.     }
  81.    
  82.     std::cout << answer[length] << std::endl;
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement