Recursive DP hdoj 5459 Jesus is here

Source: Internet
Author: User

Topic Portal

Test instructions: In simple terms, sn = sn-1 + sn-2 is recursive and asks for all C characters:∑I<J:SN[I..I+2]=SN[J..J+2]= ' cff" (j−< Span id= "mathjax-span-201" class= "Mi" >i)  mod  530600414

Analysis: Initially felt very difficult to start, similar to the Fibonacci sequence, the final number will be very large, not normal solution. Think of trying to find the rules of the table, the results did not find any rules ... I didn't come up with anything at the end. After the game came to an epiphany, this is a recursive question, take someone else's idea:

String length len, number of characters C in the string num, sum of the positions of all characters C in the string, and ans of the distance between all characters C in the string.

We can get the formula

First, Node[i].len = Node[i-1].len + Node[i-2].len;

Second, Node[i].num = Node[i-1].num + node[i-2].num;

Three, node[i].sum = node[i-1].sum + node[i-2].sum + node[i-2].len*node[i-1].num.

Iv. Node[i].ans = Node[i-1].ans + node[i-2].ans+ (node[i-2].len*node[i-2].num-node[i-2].sum) *node[i-1].num+node[i-1]. Sum*node[i-2].num;

Explain the 3rd and 4th with examples: S4 = "FFCFF", S5 = "CFFFFCFF", S6 = s4 + S5 = "FFCFF" + "CFFFFCFF", 3rd: in S5, the subscript for C is 1, 6, in S6, subscript is 1+5, 6+5, so able to explain: Node[i-2].len*node[i-1].num. 4th: Because [I-2].ans and [I-1].ans already know the position of the respective string C and the distance difference, and so can explain Node[i-1].sum + Node[i-2].sum,node[i-1].ans + Node[i-2].ans, So the rest is how to get S5 and S4 distance difference and. Do not consider the inner part of the string: [6].ans = (1 + 5 -3) + (6 + 5 -3), red for-node[i-2].sum) *node[i-1].num, Green Representative Node[i-1].sum*node[i-2].num, the remaining representative (Node[i-2].len*node[i-2].num *node[i-1].num. No matter what, I know it ~

Summary: This topic is very good and seldom touches such a recursive type. In addition, the person's show Love (confession?) The way also let me eye-opener, if have not seen contest clarifications, I do not know how many highlights:)

 

#include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include < Cstring> #include <string>using namespace std;typedef long long ll;const int N = 201314 + 10;const int INF = 0x3f3 f3f3f;const int MOD = 530600414;struct DP {ll len, num, sum, ans;}  Dp[n];void solve (void) {Dp[3].len = 3;  Dp[3].num = 1;  Dp[3].sum = 1;    Dp[3].ans = 0;  Dp[4].len = 5;  Dp[4].num = 1;  Dp[4].sum = 3;    Dp[4].ans = 0;        for (int i=5; i<=201314; ++i) {Dp[i].len = (Dp[i-2].len + dp[i-1].len)% MOD;        Dp[i].num = (dp[i-2].num + dp[i-1].num)% MOD;        Dp[i].sum = (dp[i-2].sum + dp[i-1].sum + (Dp[i-2].len * dp[i-1].num)% mod)% MoD; Dp[i].ans = (Dp[i-2].ans + Dp[i-1].ans + ((dp[i-2].len*dp[i-2].num-dp[i-2].sum)%mod*dp[i-1].num)%MOD + dp[i-2].num * dp[    i-1].sum)% MOD;    }}int Main (void) {solve ();  int T, cas = 0;scanf ("%d", &t), while (t--) {int n;        scanf ("%d", &n); printf ("Case #%d:%i64d\n", ++cas, Dp[n].ans);} return 0;}

Other messy, leave a read ~

id: 0i:1 len:1 sz:1 ans:0id:i:2 len:2 sz:0 ans:0id: 0i:3 len:3 sz:1 ans:0id: 2i:4 len:5 sz:1 ans:0id: 0 55i:5 len:8 sz:2 a  Ns:5id:2 5 1035i:6 len:13 sz:3 ans:16id:0 5 185535i:7 len:21 sz:5 ans:88id:2 5 ten 313535535i:8 len:34 Sz:8 ans:352id:0 5 52553553535535i:9 len:55 sz:13 ans:1552id:2 5 10 13 18 23 26 31 34 39 8635355353553553535535i:10 len:89 sz:21 ans:6512id:0 5 10 13 18 23 26 31 34 39 44 47 52 57 94, 102 107, 123, 133 136 141553553535535535355353553553535535i:11 len:144 sz:34 A. Ns:27753i:12 len:233 sz:55 ans:117392i:13 len:377 sz:89 ans:497728i:14 len:610 sz:144 ans:2107952i:15 len:987 sz:233 ans:8 930608i:16 len:1597 sz:377 ans:37829456i:17 len:2584 sz:610 ans:160251245i:18 len:4181 sz:987 ans:148231586i:19 len:6765 sz:1597 ans:222584546i:20 len:10946 sz:2584 ans:507962972i:21 len:17711 sz:4181 ans:132054082i:22 len:28657 sz:6765 ans : 505562166i:23 len:46368 SZ:10946 ans:31951635i:24 len:75025 sz:17711 ans:102724512i:25 len:121393 sz:28657 ans:442982018 
"CFF" "I ' ve sent Fang Fang around 201314 text messages in almost 5 years. Why can ' t she do sense of the what I mean? "" Love does isn't delight in evil but rejoices with the truth. It always protects, always trusts, always hopes, always perseveres. "" (j−i) mod 530600414, "113 1205 199312 199401 201314"

  

  

Recursive DP hdoj 5459 Jesus is here

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.