marat_snowbear

Untitled

Aug 17th, 2015
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include "assert.h"
  2. #include <algorithm>
  3. #include <bitset>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <deque>
  8. #include <functional>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <map>
  12. #include <queue>
  13. #include <set>
  14. #include <sstream>
  15. #include <stack>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <vector>
  22.  
  23. using namespace std;
  24.  
  25. #if LOCAL
  26.     #define DO_NOT_SEND
  27. #endif
  28.  
  29. typedef long long LL;
  30.  
  31. int IntMaxVal = (int) 1e20;
  32. int IntMinVal = (int) -1e20;
  33. LL LongMaxVal = (LL) 1e20;
  34. LL LongMinVal = (LL) -1e20;
  35.  
  36. #define FOR(i, a, b) for(int i = a; i < b ; ++i)
  37. #define FORD(i, a, b) for(int i = a; i >= b; --i)
  38.  
  39. template<typename T> inline void minimize(T &a, T b) { a = std::min(a, b); }
  40. template<typename T> inline void maximize(T &a, T b) { a = std::max(a, b); }
  41.  
  42. template<typename T> inline void swap(pair<T, T> &p) { swap(p.first , p.second ); }
  43.  
  44. #define all(v) v.begin(),v.end()
  45.  
  46. #define endl '\n'
  47. template<typename T> struct argument_type;
  48. template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
  49. #define next(t, i) argument_type<void(t)>::type i; cin >> i;
  50.  
  51. template <typename T1, typename T2> istream& operator >>(istream& is, pair<T1, T2>& s) { is >> s.first >> s.second; return is; }
  52. template <typename T> ostream& operator << (ostream& os, const vector<T> &v) { for (int i = 0 ; i < v.size() ; i++) { if (i) os << ' '; os << v[i]; } os << endl; return os; }
  53. template <typename T1, typename T2> ostream& operator << (ostream& os, const vector<pair<T1, T2>> &v) { for (int i = 0 ; i < v.size() ; i++) { os << v[i] << endl; } return os; }
  54. template <typename T1, typename T2> ostream& operator <<(ostream& s, const pair<T1, T2>& t) { s << t.first << ' ' << t.second; return s; }
  55. template <typename T> vector<T> readVector(int n) { vector<T> res(n); for (int i = 0 ; i < n ; i++) cin >> res[i]; return res; }
  56.  
  57. const int MOD = 1000 * 1000 * 1000 + 9;
  58.  
  59. struct mod_num {
  60.     int x;
  61.    
  62.     mod_num() : x(0) { }
  63.     mod_num(int x) : x(x >= MOD ? x % MOD : x) { }
  64.    
  65.     mod_num operator + (const mod_num &b) const { int res = x + b.x; if (res >= MOD) res -= MOD; return res; }
  66.     mod_num operator +=(const mod_num &b) { x += b.x; if (x >= MOD) x -= MOD; return *this; }
  67.    
  68.     mod_num operator - (const mod_num &b) const { int res = x - b.x; if (res < 0) res += MOD; return res; }
  69.     mod_num operator -=(const mod_num &b) { if (b.x > 0) *this += MOD - b.x; return *this; }
  70.    
  71.     mod_num operator * (const mod_num &b) const { return (int) (x * (LL) b.x % MOD); }
  72.     mod_num operator *=(const mod_num &b) { x = x * (LL) b.x % MOD; return *this; }
  73.        
  74.     mod_num pow(int n) const {
  75.         if (n == 0) return 1;
  76.         auto res = pow(n / 2);
  77.         res *= res;
  78.         if (n & 1) res *= *this;
  79.         return res;
  80.     }
  81.    
  82.     mod_num rev() const {
  83.         return pow(MOD - 2);
  84.     }
  85. };
  86.  
  87. mod_num operator +(int a, const mod_num &b) { return mod_num(a) + b; }
  88. mod_num operator *(int a, const mod_num &b) { return mod_num(a) * b; }
  89. istream& operator >> (istream& is, mod_num &x) { return is >> x.x; }
  90. ostream& operator << (ostream& os, const mod_num &x) { return os << x.x; }
  91.  
  92. bool can_flip(int m, vector<int> &xs, int flipped) {
  93.     if (flipped == 3 || flipped == 1) return true;
  94.     return false;
  95. }
  96.  
  97. vector<mod_num> fact;
  98. vector<mod_num> fact_rev;
  99.  
  100. mod_num c(int n, int k) {
  101.     return fact[n] * fact_rev[k] * fact_rev[n - k];
  102. }
  103.  
  104. mod_num solve() {
  105.     next(int, n);
  106.     next(int, m);
  107.  
  108.     auto xs = readVector<int>(n);
  109.  
  110.     mod_num res = 0;
  111.  
  112.     int from = 0;
  113.     int to = 0;
  114.  
  115.     for (auto x : xs) {
  116.         int from2, to2;
  117.  
  118.         if (from >= x) from2 = from - x;   
  119.         else if (x >= to) from2 = x - to;
  120.         else if ((from & 1) == (x & 1)) from2 = 0;
  121.         else from2 = 1;
  122.  
  123.         if (to + x <= m) to2 = to + x;
  124.         else if (from + x >= m) to2 = m - (from + x - m);
  125.         else if (((from + x)& 1) == (m & 1)) to2 = m;
  126.         else to2 = m - 1;
  127.  
  128.  
  129.         from = from2;
  130.         to = to2;
  131.     }
  132.  
  133.     FOR (flipped, from, to + 1) {
  134.         res += c(m, flipped);
  135.         flipped++;
  136.     }
  137.     return res;
  138. }
  139.  
  140. int main() {
  141.     srand (time(NULL));
  142.     ios_base::sync_with_stdio(false); cin.tie(NULL);
  143.    
  144.     fact.resize(100000 + 1, 1);
  145.     fact_rev.resize(100000 + 1, 1);
  146.  
  147.     FOR (i, 2, fact.size()) {
  148.         fact[i] = fact[i - 1] * i;
  149.         fact_rev[i] = fact_rev[i - 1] * (mod_num(i).rev());
  150.     }
  151.  
  152.     next(int, t);
  153.     while (t --> 0) cout << solve() << endl;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment