fork(13) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. int n;
  7. cin >> n;
  8.  
  9. int a[n][n], b[n][n];
  10. int i, j, ck2;
  11.  
  12. for (i = 0; i < n; i++)
  13. for (j = 0; j < n; j++){
  14. if (i==j){
  15. a[i][j] = 1;
  16. } else {
  17. a[i][j] = 0;
  18. }
  19. b[i][j] = 0;
  20. }
  21. for (i = 1; i < n; i++)
  22. for (j = 1; j <= i; j++){
  23. a[i][j] = a[i-1][j-1] + a[i-j][j]; // P(n,k) == a[i][j]
  24. ck2=j*(j-1)/2; // C(k,2)
  25. ck2=i-ck2; // for Q(n,k) == P(n-C(k,2),k)
  26. if(ck2>=0 && ck2<n){
  27. b[i][j]=a[ck2][j]; // Q(n,k) for positive and less than n integers
  28. } else {
  29. b[i][j]=0; // Q(n,k) otherwise
  30. }
  31. }
  32.  
  33. for (i = 0; i < n; i++){
  34. int z = 0;
  35. for (j = 0; j < n; j++){
  36. cout << b[i][j] << ' ';
  37. }
  38. cout << "\n" ;
  39. }
  40. return 0;
  41. }
Success #stdin #stdout 0s 3344KB
stdin
20
stdout
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 4 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 5 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 5 7 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 6 8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 6 10 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 7 12 6 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 7 14 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 8 16 11 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 8 19 15 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 9 21 18 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0