解题策略·状态精简

状态精简是一类极其重要的方法,在动态规划、组合计数中的应用尤为普遍。先来看一些习题:

1.LA 4380(CERC 2008) Counting Heaps

题意:给出一颗$n(1 \leq n \leq 500000)$个结点的有根树,要求给结点编号为$1 \sim n $,使得不同结点的标号不同,且每个非根结点标号比父节点小(满足堆性质)。求方案总数除以$m$的余数$(2 \leq m \leq 10^9)$。当$n=5$时有如下$8$种方案。

分析:我们可以这样给结点分配编号:按照从小到大的顺序分配,最小的必然是树根,次小的分配给根的子结点中的某一个...。也就是给出一个满足父结点小于所有子树结点的一个拓扑序,那么维护一个下一次可以被分配为分配最小编号的结点队列,直到队列为空,初始状态只有根节点在队列中。这样遍历所有节点一次就找到了一个方案,方法可行但无效,复杂度太高。为什么?因为我们考虑得太细致了,把所有细节都兼顾的代价就是无法承受的复杂度,虽然方法对于答案是充分的,但是却并不必要。计数并不等同于枚举。更好的方法?深入挖掘问题的实质。实际上该问题是有局部性质的:我们固定每棵子树的所有结点编号的集合,那么总的方案数就是确定的,因为满足局部拓扑序必然不会破坏整体,而局部的相对顺序已经由子树的形态给出,我们不需要知道赋给每个结点的编号是具体是几,而只需要知道它是第几大。我们设$f(i)$为根节点为$i$的子树在所有候选编号集合固定的情况下的方案数,那么考虑$i$的一个子节点$j$,我们用$g(u)$表示以$u$为根结点子树的大小,那么$j$子树的编号可取集合总数为$\textrm{C}_{g(i) - 1}^{g(j)}$,于是$j$子树的总方案数为$\textrm{C}_{g(i) - 1}^{g(j)}f(j)$,这样剩下部分(将$j$子树从$i$子树中删去)的总方案数与当前$j$子树的总方案数的乘积即为$i$子树的总方案数。即有$f(i)=f(j)\textrm{C}_{g(i) - 1}^{g(j)}f(i\setminus j)$其中$f(i\setminus g)$表示将$j$子树从$i$子树中删去得到的子树。我们设$i$子树有$p$个子结点,第$i$个为$n_i$,应用上式可以得到$f(i)=\prod_{j=1}^{p}{f(n_j)\textrm{C}_{g(i)-1-\sum_{k<p}{g(n_k)}}^{g(n_j)}}$,于是$O(n)$时间即可解决。至此问题离通过还有一段距离,一方面还需要解决组合数对非素数取模(TLE/WA)的问题,另一方面注意栈溢出(RE),程序具体实现有兴趣可以参考下面的代码。

代码:

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <ctime>
  9 #include <cmath>
 10 #include <iostream>
 11 #include <assert.h>
 12 #define PI acos(-1.)
 13 #pragma comment(linker, "/STACK:102400000,102400000")
 14 #define max(a, b) ((a) > (b) ? (a) : (b))
 15 #define min(a, b) ((a) < (b) ? (a) : (b))
 16 #define mp std :: make_pair
 17 #define st first
 18 #define nd second
 19 #define keyn (root->ch[1]->ch[0])
 20 #define lson (u << 1)
 21 #define rson (u << 1 | 1)
 22 #define pii std :: pair<int, int>
 23 #define pll pair<ll, ll>
 24 #define pb push_back
 25 #define type(x) __typeof(x.begin())
 26 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 27 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 28 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 29 #define dbg(x) std::cout << x << std::endl
 30 #define dbg2(x, y) std::cout << x << " " << y << std::endl
 31 #define clr(x, i) memset(x, (i), sizeof(x))
 32 #define maximize(x, y) x = max((x), (y))
 33 #define minimize(x, y) x = min((x), (y))
 34 //using namespace std;
 35 typedef long long ll;
 36 const int int_inf = 0x3f3f3f3f;
 37 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 38 const int INT_INF = (int)((1ll << 31) - 1);
 39 const double double_inf = 1e30;
 40 const double eps = 1e-14;
 41 typedef unsigned long long ul;
 42 inline int readint(){
 43     int x;
 44     scanf("%d", &x);
 45     return x;
 46 }
 47 inline int readstr(char *s){
 48     scanf("%s", s);
 49     return strlen(s);
 50 }
 51 //Here goes 2d geometry templates
 52 struct Point{
 53     double x, y;
 54     Point(double x = 0, double y = 0) : x(x), y(y) {}
 55 };
 56 typedef Point Vector;
 57 Vector operator + (Vector A, Vector B){
 58     return Vector(A.x + B.x, A.y + B.y);
 59 }
 60 Vector operator - (Point A, Point B){
 61     return Vector(A.x - B.x, A.y - B.y);
 62 }
 63 Vector operator * (Vector A, double p){
 64     return Vector(A.x * p, A.y * p);
 65 }
 66 Vector operator / (Vector A, double p){
 67     return Vector(A.x / p, A.y / p);
 68 }
 69 bool operator < (const Point& a, const Point& b){
 70     return a.x < b.x || (a.x == b.x && a.y < b.y);
 71 }
 72 int dcmp(double x){
 73     if(abs(x) < eps) return 0;
 74     return x < 0 ? -1 : 1;
 75 }
 76 bool operator == (const Point& a, const Point& b){
 77     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 78 }
 79 double Dot(Vector A, Vector B){
 80     return A.x * B.x + A.y * B.y;
 81 }
 82 double Len(Vector A){
 83     return sqrt(Dot(A, A));
 84 }
 85 double Angle(Vector A, Vector B){
 86     return acos(Dot(A, B) / Len(A) / Len(B));
 87 }
 88 double Cross(Vector A, Vector B){
 89     return A.x * B.y - A.y * B.x;
 90 }
 91 double Area2(Point A, Point B, Point C){
 92     return Cross(B - A, C - A);
 93 }
 94 Vector Rotate(Vector A, double rad){
 95     //rotate counterclockwise
 96     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 97 }
 98 Vector Normal(Vector A){
 99     double L = Len(A);
100     return Vector(-A.y / L, A.x / L);
101 }
102 void Normallize(Vector &A){
103     double L = Len(A);
104     A.x /= L, A.y /= L;
105 }
106 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
107     Vector u = P - Q;
108     double t = Cross(w, u) / Cross(v, w);
109     return P + v * t;
110 }
111 double DistanceToLine(Point P, Point A, Point B){
112     Vector v1 = B - A, v2 = P - A;
113     return abs(Cross(v1, v2)) / Len(v1);
114 }
115 double DistanceToSegment(Point P, Point A, Point B){
116     if(A == B) return Len(P - A);
117     Vector v1 = B - A, v2 = P - A, v3 = P - B;
118     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
119     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
120     else return abs(Cross(v1, v2)) / Len(v1);
121 }
122 Point GetLineProjection(Point P, Point A, Point B){
123     Vector v = B - A;
124     return A + v * (Dot(v, P - A) / Dot(v, v));
125 }
126 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
127     //Line1:(a1, a2) Line2:(b1,b2)
128     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
129            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
130     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
131 }
132 bool OnSegment(Point p, Point a1, Point a2){
133     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
134 }
135 Vector GetBisector(Vector v, Vector w){
136     Normallize(v), Normallize(w);
137     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
138 }
139 
140 bool OnLine(Point p, Point a1, Point a2){
141     Vector v1 = p - a1, v2 = a2 - a1;
142     double tem = Cross(v1, v2);
143     return dcmp(tem) == 0;
144 }
145 struct Line{
146     Point p;
147     Vector v;
148     Point point(double t){
149         return Point(p.x + t * v.x, p.y + t * v.y);
150     }
151     Line(Point p, Vector v) : p(p), v(v) {}
152 };
153 struct Circle{
154     Point c;
155     double r;
156     Circle(Point c, double r) : c(c), r(r) {}
157     Circle(int x, int y, int _r){
158         c = Point(x, y);
159         r = _r;
160     }
161     Point point(double a){
162         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
163     }
164 };
165 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, std :: vector<Point>& sol){
166     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
167     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
168     double delta = f * f - 4 * e * g;
169     if(dcmp(delta) < 0) return 0;
170     if(dcmp(delta) == 0){
171         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
172         return 1;
173     }
174     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
175     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
176     return 2;
177 }
178 double angle(Vector v){
179     return atan2(v.y, v.x);
180     //(-pi, pi]
181 }
182 int GetCircleCircleIntersection(Circle C1, Circle C2, std :: vector<Point>& sol){
183     double d = Len(C1.c - C2.c);
184     if(dcmp(d) == 0){
185         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
186         return 0; //two circles share identical center
187     }
188     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
189     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
190     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
191     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
192     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
193     sol.pb(p1);
194     if(p1 == p2) return 1;
195     sol.pb(p2);
196     return 2;
197 }
198 int GetPointCircleTangents(Point p, Circle C, Vector* v){
199     Vector u = C.c - p;
200     double dist = Len(u);
201     if(dist < C.r) return 0;//p is inside the circle, no tangents
202     else if(dcmp(dist - C.r) == 0){
203         // p is on the circles, one tangent only
204         v[0] = Rotate(u, PI / 2);
205         return 1;
206     }else{
207         double ang = asin(C.r / dist);
208         v[0] = Rotate(u, -ang);
209         v[1] = Rotate(u, +ang);
210         return 2;
211     }
212 }
213 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
214     //a[i] store point of tangency on Circle A of tangent i
215     //b[i] store point of tangency on Circle B of tangent i
216     //six conditions is in consideration
217     int cnt = 0;
218     if(A.r < B.r) { std :: swap(A, B); std :: swap(a, b); }
219     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
220     int rdiff = A.r - B.r;
221     int rsum = A.r + B.r;
222     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
223     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
224     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
225     if(d2 == rdiff * rdiff){ // internal tangency
226         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
227         return 1;
228     }
229     double ang = acos((A.r - B.r) / sqrt(d2));
230     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
231     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
232     if(d2 == rsum * rsum){
233         //one internal tangent
234         a[cnt] = A.point(base);
235         b[cnt++] = B.point(base + PI);
236     }else if(d2 > rsum * rsum){
237         //two internal tangents
238         double ang = acos((A.r + B.r) / sqrt(d2));
239         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
240         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
241     }
242     return cnt;
243 }
244 Point ReadPoint(){
245     double x, y;
246     scanf("%lf%lf", &x, &y);
247     return Point(x, y);
248 }
249 Circle ReadCircle(){
250     double x, y, r;
251     scanf("%lf%lf%lf", &x, &y, &r);
252     return Circle(x, y, r);
253 }
254 //Here goes 3d geometry templates
255 struct Point3{
256     double x, y, z;
257     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
258 };
259 typedef Point3 Vector3;
260 Vector3 operator + (Vector3 A, Vector3 B){
261     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
262 }
263 Vector3 operator - (Vector3 A, Vector3 B){
264     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
265 }
266 Vector3 operator * (Vector3 A, double p){
267     return Vector3(A.x * p, A.y * p, A.z * p);
268 }
269 Vector3 operator / (Vector3 A, double p){
270     return Vector3(A.x / p, A.y / p, A.z / p);
271 }
272 double Dot3(Vector3 A, Vector3 B){
273     return A.x * B.x + A.y * B.y + A.z * B.z;
274 }
275 double Len3(Vector3 A){
276     return sqrt(Dot3(A, A));
277 }
278 double Angle3(Vector3 A, Vector3 B){
279     return acos(Dot3(A, B) / Len3(A) / Len3(B));
280 }
281 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
282     return abs(Dot3(p - p0, n));
283 }
284 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
285     return p - n * Dot3(p - p0, n);
286 }
287 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
288     Vector3 v = p2 - p1;
289     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
290     return p1 + v * t;//if t in range [0, 1], intersection on segment
291 }
292 Vector3 Cross(Vector3 A, Vector3 B){
293     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
294 }
295 double Area3(Point3 A, Point3 B, Point3 C){
296     return Len3(Cross(B - A, C - A));
297 }
298 class cmpt{
299 public:
300     bool operator () (const int &x, const int &y) const{
301         return x > y;
302     }
303 };
304 
305 int Rand(int x, int o){
306     //if o set, return [1, x], else return [0, x - 1]
307     if(!x) return 0;
308     int tem = (int)((double)rand() / RAND_MAX * x) % x;
309     return o ? tem + 1 : tem;
310 }
311 ////////////////////////////////////////////////////////////////////////////////////
312 ////////////////////////////////////////////////////////////////////////////////////
313 void data_gen(){
314     srand(time(0));
315     freopen("in.txt", "w", stdout);
316     int kases = 250;
317     printf("%d\n", kases);
318     while(kases--){
319         int sz = Rand(500000, 1);
320         printf("%d %d\n", sz, Rand(1e9, 1) + 1);
321         FOR(i, 2, sz){
322             printf("%d\n", Rand(i - 1, 1));
323         }
324     }
325 }
326 
327 struct cmpx{
328     bool operator () (int x, int y) { return x > y; }
329 };
330 int debug = 1;
331 int dx[] = {0, 0, 1, 1};
332 int dy[] = {1, 0, 0, 1};
333 //-------------------------------------------------------------------------
334 const int maxn = 5e5 + 10;
335 ll dp[maxn];
336 ll mod;
337 int n;
338 std::vector<int> G[maxn];
339 int sz[maxn];
340 int prime[maxn], k;
341 bool vis[maxn];
342 void pre_init(){
343     k = 0;
344     prime[k++] = 2;
345     clr(vis, 0);
346     for(int i = 3; i < maxn; i += 2){
347         if(vis[i]) continue;
348         prime[k++] = i;
349         for(int j = i; j < maxn; j += i) vis[j] = 1;
350     }
351 }
352 int cnt[maxn][30];
353 int p[maxn], pointer;
354 ll fac[maxn];
355 void init(){
356     int tem = mod;
357     int mid = (int)sqrt(mod + .5);
358     pointer = 0;
359     for(int i = 0; prime[i] <= mid; i++){
360         if(tem % prime[i]) continue;
361         p[pointer++] = prime[i];
362         while(!(tem % prime[i])) tem /= prime[i];
363         mid = (int)sqrt(tem + .5);
364     }
365     if(tem != 1) p[pointer++] = tem;
366     clr(cnt, 0);
367     FOR(i, 1, n){
368         FOR(j, 0, pointer - 1) if(!(i % p[j])){
369             int ti = i;
370             while(!(ti % p[j])) ti /= p[j], ++cnt[i][j];
371         }
372     }
373     FOR(i, 1, n) FOR(j, 0, pointer - 1) cnt[i][j] += cnt[i - 1][j];
374     fac[0] = 1;
375     FOR(i, 1, n){
376         ll tem = i;
377         FOR(j, 0, pointer - 1) while(tem % p[j] == 0) tem /= p[j];
378         fac[i] = fac[i - 1] * tem % mod;
379     }
380 }
381 
382 ll power(ll a, ll p, ll mod){
383     ll res = 1;
384     a %= mod;
385     while(p){
386         if(p & 1) res = res * a % mod;
387         p >>= 1;
388         a = a * a % mod;
389     }
390     return res;
391 }
392 
393 ll egcd(ll a, ll b, ll &x, ll &y){
394     if(!b){
395         x = 1, y = 0;
396         return a;
397     }else{
398         ll r = egcd(b, a % b, y, x);
399         y -= (a / b) * x;
400         return r;
401     }
402 }
403 int buf[30];
404 ll cal(int n, int m){
405     FOR(i, 0, pointer - 1) buf[i] = cnt[n][i] - cnt[n - m][i] - cnt[m][i];
406     ll tem = 1;
407     FOR(i, 0, pointer - 1){
408         int num = buf[i];
409         while(num--) tem = tem * p[i] % mod;
410     }
411     tem = tem * fac[n] % mod;
412     ll x, y;
413     egcd(fac[n - m], mod, x, y);
414     tem = tem * ((x % mod + mod) % mod) % mod;
415     egcd(fac[m], mod, x, y);
416     tem = tem * ((x % mod + mod) % mod) % mod;
417     return tem;
418 }
419 //360041529
420 //-------------------------------------------------------------------------
421 int main(){
422     //data_gen(); return 0;
423     //C(); return 0;
424     debug = 0;
425     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
426     if(debug) freopen("in.txt", "r", stdin);
427     //freopen("out.txt", "w", stdout);
428     pre_init();
429     int T = readint();
430     while(T--){
431         scanf("%d%lld", &n, &mod);
432         init();
433         FOR(i, 1, n) G[i].clear();
434         FOR(i, 2, n){
435             int x = readint();
436             G[x].pb(i);
437         }
438         clr(sz, 0);
439         ROF(i, n, 1){
440             sz[i] = 1;
441             int num = G[i].size();
442             FOR(j, 0, num - 1) sz[i] += sz[G[i][j]];
443             dp[i] = 1;
444             int cur = sz[i] - 1;
445             FOR(j, 0, num - 1){
446                 int v = G[i][j];
447                 dp[i] = dp[i] * dp[v] % mod * cal(cur, sz[v]) % mod;
448                 cur -= sz[v];
449             }
450         }
451         //dfs(1);
452         printf("%lld\n", dp[1]);
453     }
454     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
455     return 0;
456 }
code:

 

posted @ 2016-08-07 23:12  astoninfer  阅读(224)  评论(0编辑  收藏  举报