Advertisement
Guest User

Untitled

a guest
May 11th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bitset>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. inline bitset<15> make_pattern()
  9. {
  10.   char field;
  11.   bitset<15> pattern;
  12.  
  13.   for( uint32_t offset = 0; offset < 3; offset++ )
  14.   {
  15.     cin >> field;
  16.     if( field == 'x' ) pattern.set(offset);
  17.  
  18.     cin >> field;
  19.     if( field == 'x' ) pattern.set(offset + 5);
  20.  
  21.     cin >> field;
  22.     if( field == 'x' ) pattern.set(offset + 10);
  23.   }
  24.   return pattern;
  25. }
  26.  
  27. int main()
  28. {
  29.   uint32_t n, m, p;
  30.   cin >> n;
  31.   cin >> p;
  32.   cin >> m;
  33.  
  34.   vector<bool> top_forbidden_patterns( 32768 ); // top_forbidden patterns can only be compared to 3 column patterns that have the top 2 rows zeroed.
  35.   vector<bool> mid_forbidden_patterns( 32768 ); // mid_forbidden patterns can only be compared to 3 column patterns that have the top and bottom row zeroed.
  36.   vector<bool> bottom_forbidden_patterns( 32768 ); // bottom_forbidden patterns can only be compared to 3 column patterns that have the bottom 2 rows zeroed.
  37.  
  38.   for( uint32_t i = 0; i < p; i++ )
  39.   {
  40.     auto pattern = make_pattern();
  41.     bottom_forbidden_patterns[pattern.to_ulong()] = true;  // true := forbidden; false := allowed.
  42.     mid_forbidden_patterns[(pattern << 1).to_ulong()] = true;
  43.     top_forbidden_patterns[(pattern << 2).to_ulong()] = true;
  44.   }
  45.  
  46.   //bitmasks for setting 2 rows of a 3 column pattern to 0;
  47.   bitset<15> bottom_rows_reset_mask;
  48.   bottom_rows_reset_mask.set(3); bottom_rows_reset_mask.set(8); bottom_rows_reset_mask.set(13);
  49.   bottom_rows_reset_mask.set(4); bottom_rows_reset_mask.set(9); bottom_rows_reset_mask.set(14);
  50.   bottom_rows_reset_mask = ~bottom_rows_reset_mask;
  51.  
  52.   bitset<15> top_rows_reset_mask;
  53.   top_rows_reset_mask.set(0); top_rows_reset_mask.set(5); top_rows_reset_mask.set(10);
  54.   top_rows_reset_mask.set(1); top_rows_reset_mask.set(6); top_rows_reset_mask.set(11);
  55.   top_rows_reset_mask = ~top_rows_reset_mask;
  56.  
  57.   bitset<15> top_and_bottom_reset_mask;
  58.   top_and_bottom_reset_mask.set(0); top_and_bottom_reset_mask.set(5); top_and_bottom_reset_mask.set(10);
  59.   top_and_bottom_reset_mask.set(4); top_and_bottom_reset_mask.set(9); top_and_bottom_reset_mask.set(14);
  60.   top_and_bottom_reset_mask = ~top_and_bottom_reset_mask;
  61.  
  62.   vector<uint32_t> left( 1024, 1 );
  63.   vector<uint32_t> right( 1024, 0 );
  64.  
  65.   for( uint32_t column = 3; column <= n; column++ )
  66.   {
  67.     for( uint32_t first_2_columns = 0; first_2_columns < 1024; first_2_columns++ )
  68.     {
  69.       if( left[first_2_columns] == 0 ) continue;
  70.       for( uint32_t third_column = 0; third_column < 32; third_column++ )
  71.       {
  72.         bitset<15> t_patt = (first_2_columns | third_column << 10) & top_and_bottom_reset_mask.to_ulong();
  73.         if( mid_forbidden_patterns[t_patt.to_ulong()] == true )
  74.           continue;
  75.  
  76.         t_patt = (first_2_columns | third_column << 10) & bottom_rows_reset_mask.to_ulong();
  77.         if( bottom_forbidden_patterns[t_patt.to_ulong()] == true )
  78.           continue;
  79.  
  80.         t_patt = (first_2_columns | third_column << 10) & top_rows_reset_mask.to_ulong();
  81.         if( top_forbidden_patterns[t_patt.to_ulong()] == true )
  82.           continue;
  83.  
  84.         t_patt = first_2_columns | third_column << 10;
  85.         auto next_2_column_pattern = (t_patt >> 5).to_ulong();
  86.         right[next_2_column_pattern] = (right[next_2_column_pattern] + left[first_2_columns]) % m;
  87.       }
  88.     }
  89.     left.swap(right);
  90.     right.assign(1024, 0u);
  91.   }
  92.   uint32_t sum = 0;
  93.   for( auto el : left )
  94.     sum = (sum + el) % m;
  95.  
  96.   cout << sum;
  97.   return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement