hdu 1502 Regular Words

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1502

思路:给定一个n,分别由n个a,b,c组成的字符串的所有前缀中a的个数大于等于b的个数大于等于c的个数,求满足这一条件的字符串的个数有多少,本题要用到dp及大数运算,转移方程:dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1],其中i,j,k分别代表a,b,c的个数

老实说并不会写大数,不过发现了一段很神奇的代码

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<sstream>
 6 #include<stack>
 7 #include<queue>
 8 #include<fstream>
 9 #include<algorithm>
10 #include<map>
11 #include<set>
12 #include<vector>
13 #include<cmath>
14 #define CLR(x) memset(x,0,sizeof(x))
15 #define ll long long
16 #define eps 1e-6
17 #define pow2(x) ((x)*(x))
18 #define forto(i,n) for(int i=0;i<n;i++)
19 #define for1to(i,n) for(int i=1;i<=n;i++)
20 #define VI vector<int>
21 using namespace std;
22 const double PI=acos(-1.0);
23 #define INF 0x3fffffff          ///INT_MAX一半防止上溢
24 #define NINF 0xbfffffff
25 
26 int dp[61][61][61][100];
27 
28 int add(int A[],int B[])        ///Add B to A
29 {
30     for (int i=0;i<100;i++)
31     {
32         A[i]+=B[i];
33         A[i+1]+=A[i]/10000;
34         A[i]%=10000;
35     }
36 }
37 
38 
39 
40 int main()
41 {
42     dp[0][0][0][0]=1;
43     for (int i=1;i<=60;i++)
44         for (int j=0;j<=i;j++)
45             for (int k=0;k<=j;k++)
46             {
47                 add(dp[i][j][k],dp[i-1][j][k]);
48                 add(dp[i][j][k],dp[i][j-1][k]);
49                 add(dp[i][j][k],dp[i][j][k-1]);
50             }
51 
52     int n;
53     while (cin>>n)
54     {
55         int k(99);
56         while (!dp[n][n][n][k])
57             k--;
58         for (int i=k;i>=0;i--)
59         {
60             if (i!=k)
61                 cout<<setw(4)<<setfill('0');
62             cout<<dp[n][n][n][i];
63         }
64         cout<<"\n"<<endl;
65     }
66     return 0;
67 } 

 还有打表的代码

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 char s[150][1000]={
 6 "1",
 7 "1",
 8 "5",
 9 "42",
10 "462",
11 "6006",
12 "87516",
13 "1385670",
14 "23371634",
15 "414315330",
16 "7646001090",
17 "145862174640",
18 "2861142656400",
19 "57468093927120",
20 "1178095925505960",
21 "24584089974896430",
22 "521086299271824330",
23 "11198784501894470250",
24 "243661974372798631650",
25 "5360563436201569896300",
26 "119115896614816702500900",
27 "2670926804331443293626900",
28 "60386171228363065768956000",
29 "1375596980582110638216817680",
30 "31554078431506568639711925552",
31 "728440733705121725605657358256",
32 "16916012593818937850175820875056",
33 "394984727560107218767652172156480",
34 "9269882950945137003216002357575872",
35 "218589820552932101591964442689934272",
36 "5177405669064206309480641678873685136",
37 "123139887106265725065261170839575261246",
38 "2940211742938376804365727956142799686970",
39 "70461309651358512358741033490151564263034",
40 "1694426732092192797198296281548882854896770",
41 "40879953049935966764838175153044218787509460",
42 "989318124094680800242093703952690318964293660",
43 "24011992526103689868224096174884123328708261100",
44 "584414956558400574946623386902564355477176447080",
45 "14261150342358043298392602404780869211095488665940",
46 "348876433985002864104580005170614922408018905657020",
47 "8555006509113973886896694412506009110609925390878620",
48 "210257823823361408953856390159370731312558948560177500",
49 "5178713915261459187808923452167773648813573133021584000",
50 "127816663734641521693312994768720558317819058630953008000",
51 "3160890723051037742300958639363743464856851891194511344000",
52 "78316111638147520232116305011469771592038383559489541704000",
53 "1943917771018304520047172570820410402016667020494472553010000",
54 "48334523581589010102952513742546024844918906756931542442556400",
55 "1203813957908516875152358489329058054078745007110871474716375280",
56 "30029983483935083858438698423851117882968874317657169412268673840",
57 "750270153399794678576435057573545926324276055884108148422050727840",
58 "18772482769028405636917719941593858764528793976890630506115671775200",
59 "470373947038907707302405010980987131831213397364392909428995307126880",
60 "11802109943885320655951253002795677125946808879324767545672973160638080",
61 "296516920131524804299707608337156053506400465189952712435084509896783040",
62 "7459203321130790040650176332416188852363369960068846727881499803410725440",
63 "187875141510304732204453155491218970539216498205240765481036372897711988800",
64 "4737637890492057297860769571861620074038072983555206964113320603342642320960",
65 "119605940186192921945993199027326146131452990076639651225155962772912609414400",
66 "3022912056752362939484322031260179006906680462576858197252183463144268821651200",
67 };
68 int main()
69 {
70     int n;
71     while(scanf("%d",&n)>0)
72     {
73         printf("%s\n\n",s[n]);
74     }
75     return 0;
76 } 

 

posted @ 2016-08-08 14:04  pter  阅读(373)  评论(0编辑  收藏  举报