Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  5. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
  6. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  7. #define FILL(a,value) memset(a, value, sizeof(a))
  8.  
  9. #define SZ(a) (int)a.size()
  10. #define ALL(a) a.begin(), a.end()
  11. #define PB push_back
  12. #define MP make_pair
  13.  
  14. typedef long long LL;
  15. typedef vector<int> VI;
  16. typedef pair<int, int> PII;
  17.  
  18. const double PI = acos(-1.0);
  19. const int INF = 1000 * 1000 * 1000 + 7;
  20. const LL LINF = INF * (LL) INF;
  21.  
  22. const int MAX = 1010;
  23.  
  24. vector<pair<int, PII> > P[MAX];
  25.  
  26. void init()
  27. {
  28.     FOR (i, 1, MAX+1)
  29.     {
  30.         FOR (j, i, MAX+1)
  31.         {
  32.             if (i * j > MAX) break;
  33.             P[i*j].PB(MP(2*(i+j), MP(i, j)));
  34.         }
  35.     }
  36.    
  37.     P[0].PB(MP(0, MP(0, 0)));
  38.    
  39.     FOR (i, 0, MAX)
  40.     {
  41.         sort(ALL(P[i]));
  42.     }
  43. }
  44.  
  45. vector<PII> go(int s, int p, int ind)
  46. {
  47.     if (s == 0 && p == 0) return vector<PII>();
  48.    
  49.     int mn = P[s][0].first;
  50.     int mx = s * 4;
  51.    
  52. //  cout<<s<<' '<<p<<' '<<mn<<' '<<mx<<' '<<ind<<endl;
  53.        
  54.     if ((p & 1) || p < mn || p > mx)
  55.     {
  56.         cout<<"*"<<endl;
  57.         vector<PII> r;
  58.         r.PB(MP(-1, -1));
  59.         return r;
  60.     }
  61.    
  62.     vector<pair<int, PII> >& cur = P[s];
  63.    
  64. /*  cout<<"!!! ";
  65.  
  66.     FOR (i, 0, SZ(cur))
  67.     {
  68.         cout<<cur[i].first<<' ';
  69.     }
  70.     cout<<endl;*/
  71.    
  72.    
  73.     RFOR(i, ind+1, 1)
  74.     {
  75.         int ns = s - i;
  76.         if (ns < 0) continue;
  77.        
  78.         vector<pair<int, PII> >& v = P[ns];
  79.        
  80.         mn = v[0].first;
  81.         mx = ns * 4;
  82.        
  83.         vector<pair<int, PII> >& cur = P[i];
  84.        
  85.         int val = p - mn;
  86.         int pos = upper_bound(ALL(cur), MP(val, MP(INF, INF))) - cur.begin() - 1;
  87.        
  88.     //  cout<<i<<' '<<val<<' '<<pos<<endl;
  89.        
  90.         if (pos >= 0)
  91.         {
  92.             int x = cur[pos].first;
  93.             x = p - x;
  94.             //cout<<cur[pos].first<<endl;
  95.             //cout<<x<<endl;
  96.            
  97.             //cout<<mn<<' '<<mx<<endl;
  98.            
  99.             if (x >= mn && x <= mx)
  100.             {
  101.                 vector<PII> res = go(ns, x, i);
  102.                 cout<<cur[pos].second.first<<' '<<cur[pos].second.second<<endl;
  103.                 res.PB(cur[pos].second);
  104.                 return res;
  105.             }
  106.             //cout<<"*"<<endl;
  107.         }
  108.        
  109.     }
  110.    
  111.     throw -1;
  112.    
  113.    
  114. }
  115.  
  116. int main()
  117. {
  118.     //freopen("in.txt", "r", stdin);
  119.     //ios::sync_with_stdio(false); cin.tie(0);
  120.  
  121.     init();
  122.    
  123.     FOR (i, 0, 10)
  124.     {
  125.         cout<<i<<": ";
  126.         FOR (j, 0, SZ(P[i]))
  127.         {
  128.             cout<<P[i][j].first<<' ';
  129.         }
  130.         cout<<endl;
  131.     }
  132.    
  133.     FOR (i, 1, 10)
  134.     {
  135.         FOR (j, 1, 40)
  136.         {
  137.             vector<PII> r = go(i, j, MAX);
  138.             cout<<i<<' '<<j<<":  ";
  139.             FOR (k, 0, SZ(r))
  140.             {
  141.                 cout<<r[k].first<<' '<<r[k].second<<", ";
  142.             }
  143.             cout<<endl;
  144.         }
  145.     }
  146.  
  147. }
  148.  
  149.  
  150.  
  151. 0: 0
  152. 1: 4
  153. 2: 6
  154. 3: 8
  155. 4: 8 10
  156. 5: 12
  157. 6: 10 14
  158. 7: 16
  159. 8: 12 18
  160. 9: 12 20
  161.  
  162. 6 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement