Advertisement
nguyentien281006

VMMTFIVE(SPOJ)

Sep 6th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. int d[6], c[6], a[6][6];
  6. void output(){
  7.     for(int i = 1; i <= 5; i++){
  8.         for(int j = 1; j <= 5; j++)
  9.             cout << a[i][j] << " ";
  10.         cout << "\n";
  11.     }
  12.     exit(0);
  13. }
  14.  
  15. void dq(int i, int j) {
  16.     if (i == 6) {
  17.         if (c[5] == 0) output();
  18.         return ;
  19.     }
  20.     int l = max(1, max(d[i]-25*(5-j), c[j]-25*(5-i)));
  21.     int r = min(25, min(d[i]-5+j, c[j]-5+i));
  22.     if (l > 25 || r < 1 || l > r) return ;
  23.     if (j == 5) l = r = d[i];
  24.     for(int x = l; x <= r; x++) {
  25.         a[i][j] = x;
  26.         d[i] -= x;
  27.         c[j] -= x;
  28.         if (j < 5) dq(i, j+1);
  29.         else dq(i+1, 1);
  30.         d[i] += x;
  31.         c[j] += x;
  32.         a[i][j] = 0;
  33.     }
  34. }
  35.  
  36.  
  37. int main(){
  38.     ios::sync_with_stdio(false);
  39.     cin.tie(nullptr);
  40.     for(int i = 1; i <= 5; i++)
  41.         cin >> d[i];
  42.     for(int i = 1; i <= 5; i++)
  43.         cin >> c[i];
  44.     dq(1,1);
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement