[whu1568]dp优化
http://acm.whu.edu.cn/land/problem/detail?problem_id=1568
思路:先将所有数分解,得到2,3,5,7的个数,转化为用这些2,3,5,7"构成"的不同序列的个数。一般思路,令dp[a][b][c][d]表示2有a个,3有b个,5有c个,7有d个时的答案,那么有如下转移方程:dp[a][b][c][d] = sigma(i:2->9)(a', b', c', d'),a'为a减去i包含2的因子个数的结果,b',c',d'同理。由于空间消耗太大,必须另外考虑方法。注意到5和7是不可能组成其它的数的,可以单独处理,于是可以先用2和3构造,但需要把长度加进去作为状态的一部分,最后再把5和7插到构造成的序列里面。
1 //#pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 38 #define pchr(a) putchar(a) 39 #define pstr(a) printf("%s", a) 40 #define sstr(a) scanf("%s", a) 41 #define sint(a) scanf("%d", &a) 42 #define sint2(a, b) scanf("%d%d", &a, &b) 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 44 #define pint(a) printf("%d\n", a) 45 #define test_print1(a) cout << "var1 = " << (a) << endl 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 48 49 typedef double db; 50 typedef long long LL; 51 typedef pair<int, int> pii; 52 typedef multiset<int> msi; 53 typedef set<int> si; 54 typedef vector<int> vi; 55 typedef map<int, int> mii; 56 57 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 58 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 59 const int maxn = 1e6 + 7; 60 const int md = 1e9 + 7; 61 const int inf = 1e9 + 7; 62 const LL inf_L = 1e18 + 7; 63 const double pi = acos(-1.0); 64 const double eps = 1e-6; 65 66 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 67 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 68 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 69 template<class T>T condition(bool f, T a, T b){return f?a:b;} 70 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 71 int make_id(int x, int y, int n) { return x * n + y; } 72 73 template<int mod> 74 struct ModInt { 75 const static int MD = mod; 76 int x; 77 ModInt(int x = 0): x(x) {} 78 int get() { return x; } 79 80 ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); } 81 ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); } 82 ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); } 83 ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } 84 85 ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; } 86 ModInt operator -= (const ModInt &that) { x -= that.x; if (x < 0) x += MD; } 87 ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; } 88 ModInt operator /= (const ModInt &that) { *this = *this / that; } 89 90 ModInt inverse() const { 91 int a = x, b = MD, u = 1, v = 0; 92 while(b) { 93 int t = a / b; 94 a -= t * b; std::swap(a, b); 95 u -= t * v; std::swap(u, v); 96 } 97 if(u < 0) u += MD; 98 return u; 99 } 100 101 }; 102 typedef ModInt<1000000007> mint; 103 104 105 const int c[10][2] = {{0, 0}, {0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 0}, {1, 1}, {0, 0}, {3, 0}, {0, 2}}; 106 const int d[2] = {2, 3}; 107 int cnt[10]; 108 mint dp[160][110][160]; 109 bool vis[160][110][160]; 110 mint fact[420], fact_inv[420]; 111 112 mint dfs(int a, int b, int p) { 113 if (vis[a][b][p]) return dp[a][b][p]; 114 if (p == 0) return 0; 115 vis[a][b][p] = true; 116 dp[a][b][p] = 0; 117 for (int i = 2; i < 10; i++) { 118 if (i == 5 || i == 7) continue; 119 int aa = c[i][0], bb = c[i][1]; 120 if (aa <= a && bb <= b) dp[a][b][p] += dfs(a - aa, b - bb, p - 1); 121 } 122 return dp[a][b][p]; 123 } 124 125 void Init() { 126 fact[0] = 1; 127 fact_inv[0] = 1; 128 for (int i = 1; i <= 410; i++) { 129 fact[i] = fact[i - 1] * i; 130 fact_inv[i] = fact[i].inverse(); 131 } 132 } 133 134 int n; 135 char s[maxn]; 136 137 int main() { 138 //freopen("in.txt", "r", stdin); 139 dp[0][0][0] = 1; 140 vis[0][0][0] = true; 141 Init(); 142 while (cin >> n) { 143 scanf("%s", s); 144 mem0(cnt); 145 rep_up0(i, n) { 146 if (s[i] == '5' || s[i] == '7') { 147 cnt[s[i] - '0']++; 148 continue; 149 } 150 rep_up0(j, 2) { 151 cnt[d[j]] += c[s[i] - '0'][j]; 152 } 153 } 154 mint ans = 0; 155 int c23 = cnt[2] + cnt[3]; 156 rep_up1(i, c23) ans += dfs(cnt[2], cnt[3], i) * fact[i + cnt[5] + cnt[7]] * fact_inv[cnt[5]] * fact_inv[cnt[7]] * fact_inv[i]; 157 printf("%d\n", ans.get()); 158 } 159 return 0; 160 }