tc SRM 554 1000pt

来源:互联网 发布:常州网络推广公司 编辑:程序博客网 时间:2024/05/01 09:35

Problem Statement

 

John and Brus are building towers using toy bricks.They have two types of bricks: red and blue ones.The number of red bricks they have isredCount and each of them has a height of redHeight.The number of blue bricks they have isblueCount and each of them has a height of blueHeight.

A tower is built by placing bricks one atop another.A brick can be placed either on the ground, or on a brick of a different color.(I.e., you are not allowed to put two bricks of the same color immediately on one another.)A tower has to consist of at least one brick.The height of a tower is the sum of all heights of bricks that form the tower.Two towers are considered to be different if they have different heights.(Two towers of the same height are considered the same, even if they differ in the number and colors of bricks that form them.)

You are given the ints redCount, redHeight, blueCount and blueHeight.Return the number of different towers that John and Brus can build.

Definition

 Class:TheBrickTowerEasyDivTwoMethod:findParameters:int, int, int, intReturns:intMethod signature:int find(int redCount, int redHeight, int blueCount, int blueHeight)(be sure your method is public)  

Constraints

-redCount will be between 1 and 47, inclusive.-redHeight will be between 1 and 47, inclusive.-blueCount will be between 1 and 47, inclusive.-blueHeight will be between 1 and 47, inclusive.

Examples

0)  
1
2
3
4
Returns: 4
John and Brus have 1 red brick of height 2 and 3 blue bricks of height 4. Using these bricks, it's possible to build 4 towers:
  • red (height 2);
  • blue (height 4);
  • red, blue (height 6);
  • blue, red, blue (height 10).
1)  
4
4
4
7
Returns: 12
2)  
7
7
4
4
Returns: 13
3)  
47
47
47
47
Returns: 94


思路: 我是用dp做得,状态表示还是很好想的,dp[ i ][ j ][ k ]  表示高度为i,有j个相邻的块同颜色,第i层放置情况为k(我用的状态压缩),然后dp方程就很好搞了……

比赛的时候,我没有注意到我的时间超时,直接交了,然后再system test 的时候就挂了……我算的时间复查度 最多 47*7*16^24 == 21561344 ,很有危险会T,结果我想不出来怎么优化,只好暴力打表了,打表的时候有个技巧就是,算出C*K*47 那么 C*K*h  (h>=1 && h<=47) 就都算出来了,这样打表很快很多就把答案算出来……

 打表程序

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;     long long ans[5][8][48];class TheBrickTowerHardDivTwo{        public:        long long dp[48][8][16*16];        int cal(int d,int c)        {            int a1=d%c;            d/=c;            int a2=d%c;            d/=c;            int a3=d%c;            d/=c;            int a4=d%c;            int ret=0;            if(a1==a2) ret++;            if(a2==a3) ret++;            if(a3==a4) ret++;            if(a1==a4) ret++;            return ret;        }        int cal1(int j1,int j2,int c)        {            int a[4],b[4];            for(int i=0;i<4;i++)            {                a[i]=j1%c;                j1/=c;            }            for(int i=0;i<4;i++)            {                b[i]=j2%c;                j2/=c;            }            int ret=0;            for(int i=0;i<4;i++)              if(a[i]==b[i]) ret++;            return ret;        }        int find(int C, int K, int H)        {              return ans[C][K][H];        }        void init(int C, int K, int H)        {                //if(C==4&&K==7&&H==47) return 1008981254;                long long lim=C*C*C*C;                memset(dp,0,sizeof(dp));                for(int i=0;i<lim;i++)                {                    int num=cal(i,C);                    if(num<=K) dp[1][num][i]=1;                }               //cout<<C<<" "<<K<<" "<<H<<endl;                for(int i=1;i<H;i++)                for(int j=0;j<=K;j++)                for(int r=0;r<lim;r++)                    if(dp[i][j][r])                    {                        for(int k=0;k<lim;k++)                        {                            int tmp=cal(k,C);                            tmp+=cal1(r,k,C);                            if(tmp+j<=K) dp[i+1][tmp+j][k]=(dp[i+1][tmp+j][k]+dp[i][j][r])%1234567891;                        }                    }              //cout<<"bug"<<endl;               long long ret=0;               for(int i=1;i<=H;i++)               {                  for(int j=0;j<=K;j++)                  for(int r=0;r<lim;r++) ret=(ret+dp[i][j][r])%1234567891;                  ans[C][K][i]=ret;               }        }}pp;int main(){    //freopen("data.in","r",stdin);    freopen("data.out","w",stdout);    for(int i=1;i<=4;i++)    for(int j=0;j<=7;j++)       pp.init(i,j,47);    for(int i=0;i<=4;i++)    {        for(int j=0;j<=7;j++)        for(int k=0;k<=47;k++)          cout<<ans[i][j][k]<<",";        cout<<endl;    }    return 0;}

然后就可以直接搞了

// BEGIN CUT HERE/**/// END CUT HERE#line 7 "TheBrickTowerHardDivTwo.cpp"#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;     long long ans[5][8][48]=        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,0,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,0,14,32,50,68,86,104,122,140,158,176,194,212,230,248,266,284,302,320,338,356,374,392,410,428,446,464,482,500,518,536,554,572,590,608,626,644,662,680,698,716,734,752,770,788,806,824,842,0,16,64,110,166,232,308,394,490,596,712,838,974,1120,1276,1442,1618,1804,2000,2206,2422,2648,2884,3130,3386,3652,3928,4214,4510,4816,5132,5458,5794,6140,6496,6862,7238,7624,8020,8426,8842,9268,9704,10150,10606,11072,11548,12034,0,16,112,190,278,376,484,602,730,868,1016,1174,1342,1520,1708,1906,2114,2332,2560,2798,3046,3304,3572,3850,4138,4436,4744,5062,5390,5728,6076,6434,6802,7180,7568,7966,8374,8792,9220,9658,10106,10564,11032,11510,11998,12496,13004,13522,0,16,176,386,634,932,1280,1678,2126,2624,3172,3770,4418,5116,5864,6662,7510,8408,9356,10354,11402,12500,13648,14846,16094,17392,18740,20138,21586,23084,24632,26230,27878,29576,31324,33122,34970,36868,38816,40814,42862,44960,47108,49306,51554,53852,56200,58598,0,16,224,754,1354,2084,2944,3934,5054,6304,7684,9194,10834,12604,14504,16534,18694,20984,23404,25954,28634,31444,34384,37454,40654,43984,47444,51034,54754,58604,62584,66694,70934,75304,79804,84434,89194,94084,99104,104254,109534,114944,120484,126154,131954,137884,143944,150134,0,18,132,858,5484,34962,222804,1419786,9047292,57651906,367374180,1106443751,102770088,1231887182,804722582,705509352,485107251,573713355,840996596,1122986976,793218781,1060583569,547506192,824776965,1114278211,797135950,1122838812,967624217,1047446386,992492058,288523086,518829158,8575986,453851058,673517686,430083794,316515820,495275570,966297825,1079278828,1220624720,523554061,16947444,493551652,917936012,747641809,327180730,534265771,0,42,372,3138,24996,191250,1422516,10363554,74316420,526308210,1220741998,963265798,582685831,846392778,701740347,1020352330,15076215,1139492253,511605568,804123763,1222117970,211151226,1004831801,814528470,719554275,488415729,297783611,1122722111,1002609362,877909599,904222294,800835481,199621086,1144375025,454177744,1222919673,271956628,484183475,641847228,507049458,683776326,225583401,826649409,498459316,1057835875,685492245,1024614449,609078566,0,78,1200,11886,107652,919098,7529304,59825862,464292780,1067038636,595490097,77090769,1213489008,934764062,107833544,938821220,1184539683,722646991,332644538,158105450,803614619,529615699,1057493822,29390972,387804454,243587562,81001592,905588919,237559936,582875654,757914517,57563450,629615529,541878900,1022653542,758362555,623129270,536029168,947129217,680543035,1028908651,21500241,241640780,754643688,838972091,950932281,627072176,423464617,0,78,2592,34926,372420,3594426,32556696,281968422,1126785881,742588149,711353420,406556923,26083950,259621376,995644620,1088150291,685696381,720028775,680170247,836817072,250050941,861467507,882721920,952286741,1017049397,419066232,1035999654,446306965,172264920,221320586,417841580,707184858,852840353,813808953,259701633,557201296,1156334684,236941475,1110025485,1004320681,833714332,557468711,470724356,271970178,1215304570,970994786,417718139,1177547579,0,81,4269,81987,1068303,11829069,119359149,1131059703,361173859,569259417,788050068,700297894,221066010,654753707,201322500,774514793,1134435848,88424618,972049068,283471446,897449973,1167837664,150677947,270437949,822599609,1027216386,495069939,746725948,420590993,891732494,45108789,422841381,544124605,528877361,544574123,477022225,883778270,25157092,1187099708,21378051,871267753,26185122,528934172,898966134,1175696211,108330053,1215580230,533393370,0,81,5493,158187,2619159,33842253,383325909,286379886,842705270,1037910169,1200527744,150445661,840614656,333416754,34951959,387677537,598171624,240105090,203647346,377428457,703791259,558405148,63529686,959902699,646385237,1192313012,283207753,159709934,956436465,1103076583,758301965,995377966,939212712,776709673,536342110,816757126,666266653,1177327361,976214319,783986702,58785165,703246216,35869633,654390294,406477438,220164137,650982261,1104142200,0,81,6285,259959,5551407,85243917,1094818221,243432641,768899511,463892834,1032782509,834677472,1074673975,341432764,1148701285,198242641,16030925,290171558,527384404,385185765,777095932,473076165,622270688,1224880284,662955558,288774714,63744861,505235924,308129634,284574077,187790515,877338646,223704894,973357808,1103564791,176305279,115528516,1074634036,418508517,1052878852,797745483,729285513,231351794,1151166651,32994240,857222056,391917087,747804203,0,81,6501,359559,10207959,190045629,334050919,73336048,1149356528,391248094,710554984,1016260945,1223804669,178597886,936590532,1106293441,960659702,770837880,1119366650,498437327,1212674001,43822877,1040723393,935073081,910758628,133979599,278915938,310916404,18957021,1027993990,73957576,97115662,500981204,664265118,953899054,1133168527,97132083,949024548,131424687,871834470,186219663,830094723,32624424,220297510,370329222,925012268,284614621,26123566,0,84,2736,86772,2750400,87179124,294175114,1168741922,978491432,1036967575,864059624,592973979,294638799,63951636,418809433,929075937,1015493129,37555922,1219976271,466006167,860015235,160339243,695112864,817961498,524241669,88335566,536236126,672420827,1001021274,997385408,342723262,571850554,596960045,773773006,14149273,785768710,991881271,1054205084,472676683,842430520,310079156,897114646,458712110,724125993,1010842740,25973916,1165666464,699527058,0,180,10032,455508,18800736,734218740,495504110,739690386,242607125,451119092,404289736,157183111,1175393251,377157155,93955890,8043030,334050077,868692352,409573370,1065715820,869770288,134372785,342675797,268927824,440104657,555649993,766818025,3565631,1193991162,987164651,667318274,158952208,454418222,603492128,803109514,997230084,6977675,1102047744,531388056,678576974,2675643,191090640,520617504,1031093850,659904261,511334477,1036083748,576622724,0,252,25368,1555356,80001192,24543363,1196401855,621733930,406816482,752462956,692607946,714585508,352415764,902342082,1118030212,759147244,282376494,418231618,1163235459,948231878,1221302150,139616825,806399733,425434987,607317128,346664797,942383538,550106835,438735095,329010711,67165262,1161362076,487970471,553170238,370508038,976999822,1107412383,827634743,550457452,985865206,89117976,411343999,546398371,940720111,79508715,599019914,1057771156,640362825,0,252,42456,3732444,243972168,71341195,99467637,1233957508,734155265,769065493,294982692,49377415,648863296,533527204,937473718,863168939,680103228,577534162,359750845,25826397,953948611,424632599,412848034,321932308,1234425118,180165525,573755295,607321662,351631542,1164176246,217941146,1149389264,1094085338,260965703,355529620,276110155,111799598,830038695,832643647,91284501,1095920894,238473462,537208985,529789807,1183395812,481691152,740524323,528499535,0,256,56392,6912100,580020220,958696611,810441406,616522233,984576984,319929218,456430089,789049616,378742217,982834781,896883763,427350486,334131531,950958745,604382828,322654184,828076371,550648345,465537787,1114589673,1077729369,878350784,906525898,68129532,494405847,66530847,359245306,91149131,355204495,501862652,1152748848,1204581040,621600695,269619280,416665239,979307187,549710740,945670807,170931175,363481560,279010116,32138061,1066459968,606775667,0,256,62152,10422148,1124603644,1180711154,1180102375,202094092,646896580,838574167,289557095,18311516,867619386,854620121,915949674,900593534,125364051,1037869727,632455784,453590686,723094587,1172988043,1017521746,51754536,177710545,1201247038,561220996,652280089,131132247,738466964,823301719,1188311479,179563263,1200422768,539995327,1149760532,392917620,744449365,198834623,842373456,1160821617,631984259,354295233,1154651863,485822408,1099959731,922303108,734623669,0,256,64936,13326652,600918369,473348489,941725416,344894395,9217534,391177349,245893553,1069176525,305148342,1182055057,1205372839,249691688,388493806,887737226,607659073,439237173,862595405,1169777728,532358174,798696933,61544716,929894158,409636742,418057467,430141230,691929573,655524411,715002240,1058053873,1081913478,1002599755,903452170,977634168,935163120,158456957,825172471,723339198,593268980,689639557,937560879,487413382,675895602,133331749,240414241,0,256,65512,15198844,121628750,41769413,2944210,96278269,81019402,625969026,620664420,3501965,1134635018,479486237,1099348343,519603522,760223312,858380642,1150600294,669435550,11382558,771541377,1107874352,355408706,1008133827,296622608,252311784,43844934,356114049,482712639,448320719,1176477670,37486878,1139022929,187247000,355241126,526684429,1094738200,439249673,1232512744,322325313,313576148,738509573,815436091,47883684,190848937,1064072547,1008981254        };class TheBrickTowerHardDivTwo{        public:                int find(int C, int K, int H)        {              return ans[C][K][H];        }// BEGIN CUT HEREpublic:void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }private:template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }void test_case_0() { int Arg0 = 2; int Arg1 = 0; int Arg2 = 2; int Arg3 = 4; verify_case(0, Arg3, find(Arg0, Arg1, Arg2)); }void test_case_1() { int Arg0 = 1; int Arg1 = 7; int Arg2 = 19; int Arg3 = 1; verify_case(1, Arg3, find(Arg0, Arg1, Arg2)); }void test_case_2() { int Arg0 = 2; int Arg1 = 3; int Arg2 = 1; int Arg3 = 14; verify_case(2, Arg3, find(Arg0, Arg1, Arg2)); }void test_case_3() { int Arg0 = 4; int Arg1 = 7; int Arg2 = 47; int Arg3 = 1008981254; verify_case(3, Arg3, find(Arg0, Arg1, Arg2)); }// END CUT HERE};// BEGIN CUT HEREint main(){        TheBrickTowerHardDivTwo ___test;        ___test.run_test(-1);        return 0;}// END CUT HERE


原创粉丝点击