Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 1.69 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <cstdio>
  2. #include <algorithm>
  3.  
  4. using std::swap;
  5.  
  6. const int maxN = 50;
  7.  
  8. typedef unsigned int uint;
  9. typedef unsigned long long ull;
  10.  
  11. int n;
  12. uint target;
  13. uint m[maxN];
  14. ull total;
  15.  
  16. void reorderFirstMax()
  17. {
  18.     for (int i = 1; i < n; ++i)
  19.         if (m[i] > m[0])
  20.             swap(m[0], m[i]);
  21. }
  22.  
  23. uint flp2(uint x)
  24. {
  25.     x = x | (x >> 1);
  26.     x = x | (x >> 2);
  27.     x = x | (x >> 4);
  28.     x = x | (x >> 8);
  29.     x = x | (x >> 16);
  30.     return x - ( x >> 1);
  31. }
  32.  
  33. void countSomePossibilites(uint high)
  34. {
  35.     ull withHigh = 0;
  36.     ull withoutHigh = 1;
  37.     for (int i = 1; i < n; ++i)
  38.     {
  39.         ull optHigh;
  40.         ull optLow;
  41.         if (m[i] >= high)
  42.         {
  43.             optHigh = (m[i] ^ high) + 1;
  44.             optLow = high;
  45.         }
  46.         else
  47.         {
  48.             optHigh = 0;
  49.             optLow = m[i] + 1;
  50.         }
  51.         ull newWithHigh = withHigh * optLow + withoutHigh * optHigh;
  52.         ull newWithoutHigh = withHigh * optHigh + withoutHigh * optLow;
  53.         withHigh = newWithHigh;
  54.         withoutHigh = newWithoutHigh;
  55.     }
  56.     if (target == 0)
  57.         total += withoutHigh;
  58.     else
  59.         total += withHigh;
  60. }
  61.  
  62. bool phase()
  63. {
  64.     reorderFirstMax();
  65.     if (m[0] == 0 || m[0] < target)
  66.     {
  67.         if (target == 0)
  68.             ++total;
  69.         return false;
  70.     }
  71.     uint high = flp2(m[0]);
  72.     countSomePossibilites(high);
  73.     m[0] ^= high;
  74.     target ^= high;
  75.     return true;
  76. }
  77.  
  78. void readData()
  79. {
  80.     scanf("%d", &n);
  81.     for (int i = 0; i < n; ++i)
  82.         scanf("%u", &m[i]);
  83. }
  84.  
  85. int main()
  86. {
  87.     readData();
  88.     while(phase())
  89.         ;
  90.     --total; //policzylismy pusty krysztal
  91.     printf("%Lu\n", total);
  92. }