Advertisement
Guest User

Untitled

a guest
Jan 11th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <list>
  2. #include <map>
  3. #include <set>
  4. #include <deque>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <memory.h>
  15. #include <ctime>
  16. #include <bitset>
  17.  
  18. using namespace std;
  19.  
  20. #define ABS(a) ((a>0)?a:-(a))
  21. #define MIN(a,b) ((a<b)?(a):(b))
  22. #define MAX(a,b) ((a<b)?(b):(a))
  23. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  24. #define FI(i,n) for (int i=0; i<(n); ++i)
  25. #define pnt pair <int, int>
  26. #define mp make_pair
  27. #define PI 3.1415926535897
  28. #define MEMS(a,b) memset(a,b,sizeof(a))
  29. #define LL long long
  30. #define U unsigned
  31.  
  32. const int N = 300010;
  33. const int M = 100010;
  34. int mod = 1000000007;
  35. LL fact[N];
  36. LL inv[N];
  37. char s1[6];
  38. string s;
  39. int vals[M];
  40. stringstream tmp;
  41. int dp[M][202];
  42. int was[M][202];
  43. int test;
  44.  
  45. int bp(int val, int p)
  46. {
  47.     if (p == 0)
  48.         return 1;
  49.     LL res = bp(val, p / 2);
  50.     res *= res;
  51.     res %= mod;
  52.     if (p & 1)
  53.     {
  54.         res *= val;
  55.         res %= mod;
  56.     }
  57.     return res;
  58. }
  59. int last;
  60. int r(int n, int k)
  61. {
  62.     if (n == 0)
  63.     {
  64.         if (k > vals[last] + 1)
  65.             return 0;
  66.         return 1;
  67.     }
  68.     if (was[n][k] == test)
  69.         return dp[n][k];
  70.     was[n][k] = test;
  71.     int c = vals[last];
  72.     if ((k == 1) || (k == 2))
  73.     {
  74.         LL res = fact[c + n + n];
  75.         res *= (c + 1);
  76.         res %= mod;
  77.         res *= inv[n];
  78.         res %= mod;
  79.         res *= inv[c + n + 1];
  80.         res %= mod;
  81.         return dp[n][k] = res;
  82.     }
  83.     LL res = r(n, k - 1) - r(n - 1, k - 2);
  84.     if (res < 0)
  85.         res += mod;
  86.     return dp[n][k] = res;
  87. }
  88.  
  89. int main()
  90. {
  91. #ifdef Fcdkbear
  92.     freopen("in.txt", "r", stdin);
  93.     //freopen("out.txt", "w", stdout);
  94.     double beg = clock();
  95. #endif
  96.  
  97.     fact[0] = 1;
  98.     FOR(i, 1, N)
  99.     {
  100.         fact[i] = fact[i - 1] * i;
  101.         fact[i] %= mod;
  102.     }
  103.     FOR(i, 0, N)
  104.     {
  105.         inv[i] = bp(fact[i], mod - 2);
  106.     }
  107.     int n;
  108.     scanf("%d", &n);
  109.     MEMS(vals, -1);
  110.     FOR(i, 0, n)
  111.     {
  112.         scanf("%s", s1);
  113.         s = (string)s1;
  114.         if (s[0] == '?')
  115.             continue;
  116.         tmp.clear();
  117.         tmp << s;
  118.         tmp >> vals[i];
  119.     }
  120.     if (vals[0] == -1)
  121.         vals[0] = 0;
  122.     if (vals[0])
  123.     {
  124.         cout << 0 << endl;
  125.         return 0;
  126.     }
  127.     n++;
  128.     vals[n - 1] = 1;
  129.     last = 0;
  130.     LL res = 1;
  131.     FOR(i, 1, n)
  132.     {
  133.         if (vals[i] == 0)
  134.         {
  135.             cout << 0 << endl;
  136.             return 0;
  137.         }
  138.         if (vals[i] != -1)
  139.         {
  140.             test++;
  141.             LL res1 = r(i - last - 1, vals[i]);
  142.             res *= res1;
  143.             res %= mod;
  144.             last = i;
  145.         }
  146.     }
  147.     cout << res << endl;
  148.  
  149. #ifdef Fcdkbear
  150.     double end = clock();
  151.     fprintf(stderr, "*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
  152. #endif
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement