Coder-strike 2014-round2 D 2048 (Dp-Based Memory search)

Source: Internet
Author: User

Refer to the http://blog.csdn.net/keshuai19940722/article/details/24723417 he started a little small analysis, of course there are questions, it is rare to see the CF question a little long, read for a long time to add YY to get out of the question,

But I still haven't introduced the state transition equation, because the question says to move to the beginning, I pushed it backwards, so I was trying a memory-based search, and all the way to the search came across only 0, 2, 4. Check whether the last digit in the current state can be merged with the current digit. Only the same value is required for the question. Here is a tips, commented out in the code,

DP [ID] [now] [Mark] indicates that the sum of the first (ID-1) of the current ID is now and whether the current now> 2 ^ K occurs.


# Define mod limit 7int N, K; int nnum [2000 + 55]; int DP [2000 + 55] [8000 + 55] [2]; int win; void Init () {memset (nnum, 0, sizeof (nnum); memset (DP,-1, sizeof (DP);} bool input () {While (CIN> N> K) {for (INT I = 0; I <n; I ++) scanf ("% d ", & nnum [I]); Return false;} return true;} int DFS (INT ID, int now, int mark) {If (now> = Win) mark = 1; if (DP [ID] [now] [Mark]! =-1) return DP [ID] [now] [Mark]; DP [ID] [now] [Mark] = 0; If (ID = N) return DP [ID] [now] [Mark] = mark; If (nnum [ID] = 0) {DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, now + 2, mark) % MOD; if (now % 4) DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, 4, mark )) % MOD;/* Here % 4 means: because all the above are composed of 2 and 4, it can be merged into the current state, so the last two and four in the previous division must match each other, if the modulus of 4 is used, the remainder must be 0 or 2. If it is 2, the last number that constitutes the current sum of now is 2, if the last one is 2, it cannot be merged with the next four. If the sum of now is 0, the current sum is 2 and now + 2. In the next step, the remainder must be 2, therefore, if we limit that we can only take 2 and not take 4, for example, sequence 4 4 4 2, then we must take 2 to merge, and the current value of % 4 is 2, and because 2 ^ K, k> = 3, it does not constitute a sum of 2 that is equal to 1 <K, as a result, 2 is added to make the answer 1 */else DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, now + 4, mark) % MOD;} else {If (nnum [ID] = 2) DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, now + 2, mark) % MOD; else {If (now % 4) DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, 4, mark )) % MOD; else DP [ID] [now] [Mark] = (DP [ID] [now] [Mark] + DFS (ID + 1, now + 4, mark )) % mod ;}} return DP [ID] [now] [Mark];} void CAL () {win = 1 <K; int ans = DFS (0, 0 ); cout <ans <Endl;} void output () {} int main () {While (true) {Init (); If (input () return 0; cal (); output ();} return 0;}/* 6 4 4 4 4 4 2 26 4 4 4 4 4 4 2 0ans: 12 */


Coder-strike 2014-round2 D 2048 (Dp-Based Memory search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.