Advertisement
pasholnahuy

Лагранж

Jun 14th, 2023
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. bool GetPartition(int n, vector<int> &partition) {
  8.     if (n == 0 && partition.size() <= 4) {
  9.         return true;
  10.     }
  11.     if (n > 0 && partition.size() == 4) {
  12.         return false;
  13.     }
  14.     for (int i = 1; i * i <= n; ++i) {
  15.         partition.push_back(i);
  16.         if (GetPartition(n - i * i, partition)) {
  17.             return true;
  18.         }
  19.         partition.pop_back();
  20.     }
  21.     return false;
  22. }
  23.  
  24. int main() {
  25.     int n;
  26.     cin >> n;
  27.     vector<int> partition;
  28.     bool ans = GetPartition(n, partition);
  29.     for (auto el: partition) {
  30.         cout << el << " ";
  31.     }
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement