Advertisement
Malinovsky239

Untitled

Nov 29th, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. const int N = int(3e5), M = 900, K = int(120);
  5.  
  6. using namespace std;
  7.  
  8. long long t, m, ur[M + 1], pir[K + 1],dp[N + 1];
  9.  
  10. int main() {   
  11.     ur[1] = 1;
  12.     for (int i = 2; i <= M; i++)
  13.         ur[i] = ur[i - 1] + i;         
  14.  
  15.     pir[1] = 1;
  16.     for (int i = 2; i <= M; i++)
  17.         pir[i] = pir[i - 1] + ur[i];
  18.  
  19.     dp[1] = 1;
  20.    
  21.     for (int i = 2; i <= N; i++)
  22.         dp[i] = -1;
  23.  
  24.     for (int i = 2; i <= K; i++)  
  25.         dp[ pir[i] ] = 1;              
  26.  
  27.     for (int i = 2; i <= N; i++)   {
  28.         for (int j = 1; j <= K; j++) {
  29.             if (dp[i] != -1 && i + pir[j] <= N && ( dp[i + pir[j] ] == -1 || dp[i + pir[j] ] > dp[i] + 1 ) )
  30.                 dp[i + pir[j] ] = dp[i] + 1;
  31.         }
  32.     }
  33.                                                              
  34.     cin >> t;
  35.     for (int i = 0; i < t; i++) {
  36.         cin >> m;
  37.         cout << dp[m] << endl;
  38.     }                      
  39.                                                                  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement