Advertisement
Guest User

subset, solution in rational numbers

a guest
Oct 8th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. /**
  2.  * Author: Sergey Kopeliovich (Burunduk30@gmail.com5
  3.  * Date: 2013.10.04
  4.  *
  5.  * Status: Accepted, Time = 1.4
  6.  */
  7.  
  8. #include <cstdio>
  9. #include <cassert>
  10. #include <cstring>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. #define forn(i, n) for (int i = 0; i < (int)(n); i++)
  16.  
  17. typedef long long ll;
  18.  
  19. const ll P = (int)1e9 + 7;
  20. const int N = 1000;
  21. const int D = 10;
  22.  
  23. struct num
  24. {
  25.   ll a, b;
  26.  
  27.   num() { }
  28.   num( ll a, ll b ) : a(a), b(b) { assert(b != 0); }
  29.  
  30.   void norm()
  31.   {
  32.     if (b == 1)
  33.       return;
  34.     ll g = __gcd(a, b);
  35.     a /= g, b /= g;
  36.     if (b < 0)
  37.       a = -a, b = -b;
  38.   }
  39.  
  40.   num operator + ( num x ) { return num(a * x.b + b * x.a, x.b * b); }
  41.   num operator - ( num x ) { return num(a * x.b - b * x.a, x.b * b); }
  42.   num operator * ( num x ) { return num(a * x.a, b * x.b); }
  43.   num operator / ( num x ) { return num(a * x.b, b * x.a); }
  44.   num operator ~ () { return num(b, a); }
  45.   bool operator ! () { return a == 0; }
  46.  
  47.   bool operator == ( num x ) { return a * x.b - b * x.a == 0; }
  48.   bool operator != ( num x ) { return a * x.b - b * x.a != 0; }
  49.   bool operator < ( num x ) { return a * x.b - b * x.a < 0; }
  50.   bool operator > ( num x ) { return a * x.b - b * x.a > 0; }
  51.  
  52.   #define DECLARE(s) \
  53.     num& operator s##= ( num x ) { return *this = (*this s x); }
  54.   DECLARE(+)
  55.   DECLARE(-)
  56.   DECLARE(*)
  57.   DECLARE(/)
  58. };
  59.  
  60. int n, d, a[N][D];
  61. int pn, p[N];      
  62. int rn, rp[N];
  63.  
  64. int cn, id[3], ind[N], o[N];
  65. ll c[N];
  66. num b[D];
  67.  
  68. inline bool vless( int i, int j )
  69. {
  70.   return c[i] < c[j];
  71. }
  72.  
  73. inline ll hash()
  74. {
  75.   int pos = 0;
  76.   while (pos < d && !b[pos])
  77.     pos++;
  78.   num t = b[pos];
  79.   forn(i, d)
  80.     if (!!b[i])
  81.       b[i] /= t;
  82.   forn(i, d)
  83.     b[i].norm();
  84.   ll h = 0;
  85.   forn(i, d)
  86.   {
  87.     h = h * P + b[i].a;
  88.     h = h * P + b[i].b;
  89.   }
  90.   return h;
  91. }
  92.  
  93. int main()
  94. {
  95.   #define NAME "subset"
  96.   assert(freopen(NAME ".in", "r", stdin));
  97.   assert(freopen(NAME ".out", "w", stdout));
  98.  
  99.   assert(scanf("%d%d", &n, &d) == 2 && n <= N && d <= D);
  100.   forn(i, n)
  101.     forn(j, d)
  102.       assert(scanf("%d", &a[i][j]) == 1);
  103.   forn(i, n)
  104.   {
  105.     pn = cn = 0;
  106.  
  107.     int pos = 0;
  108.     while (!a[i][pos])
  109.       pos++;
  110.     forn(j, i)
  111.     {
  112.       int good = 1;
  113.       forn(k, d)
  114.         b[k] = num(a[j][k], 1);
  115.       if (a[j][pos])
  116.       {
  117.         num koef(a[j][pos], a[i][pos]);
  118.         good = 0;
  119.         forn(k, d)
  120.           if (!!(b[k] -= koef * num(a[i][k], 1)))
  121.             good = 1;
  122.       }
  123.       if (!good)
  124.         p[pn++] = j;
  125.       else
  126.       {
  127.         c[cn] = hash();
  128.         o[cn] = cn, ind[cn++] = j;
  129.       }
  130.     }
  131.     sort(o, o + cn, vless);
  132.  
  133.     int ma = 0, rj = 0;
  134.     for (int j = 0, k; j < cn; j = k)
  135.     {
  136.       for (k = j + 1; k < cn && !vless(o[j], o[k]); k++)
  137.         ;
  138.       if (k - j > ma)
  139.         ma = k - j, rj = j;
  140.     }
  141.     if (ma + pn + 1 > rn)
  142.     {
  143.       rn = ma + pn + 1;
  144.       forn(j, pn)
  145.         rp[j] = p[j];
  146.       forn(j, ma)
  147.         rp[j + pn] = ind[o[rj + j]];
  148.       rp[ma + pn] = i;
  149.     }
  150.   }
  151.   printf("%d\n", rn);
  152.   forn(i, rn)
  153.     printf("%d%c", rp[i] + 1, " \n"[i == rn - 1]);
  154.   return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement