Codeforces431c-K-tree DP

Source: Internet
Author: User

Q: I will give you a K-Cross number. Each node must have k subnodes, and the cost for these K subnodes is 1-K, ask you the probability that the total cost is N and at least one D-K edge needs to be crossed

Solution: divide the problem into two-dimensional DP, one indicating that the process has not passed, and the other indicates that the process has passed, and the DP can be used to find the solution.

Solution code:

1 /************************************** * ********************** 2 * Author: darkdream 3 * Email: [email protected] 4 * Last modified: 5 * filename: 431c. CPP 6 * description: 7 *************************************** * ******************/8 // File Name: 431c. CPP 9 // Author: darkdream10 // created time: friday August 01, 2014 11 12 # include <vector> 13 # include <list> 14 # include <map> 15 # include <set> 16 # include <deque> 17 # include <stack> 18 # include <bitset> 19 # include <algorithm> 20 # include <functional> 21 # include <numeric> 22 # include <utility> 23 # include <sstream> 24 # include <iostream> 25 # include <iomanip> 26 # include <cstdio> 27 # include <cmath> 28 # include <cstdlib> 29 # include <cstring> 30 # include <ctime> 31 32 # define ll long long33 using namespace STD; 34 ll DP [104] [104]; 35 ll dpa [104] [104]; 36 # define MD 100000000737 int main () {38 int N, K, D; 39 scanf ("% d", & N, & K, & D); 40 memset (DP, 0, sizeof (DP); 41 memset (DPA, 0, sizeof (DPA); 42 DP [0] [0] = 1; 43 ll ans = 0; 44 for (INT I = 1; I <= N; I ++) 45 {46 for (Int J = I-1; j <= N; j ++) 47 {48 if (DP [I-1] [J]) 49 {50 for (int s = 1; S <= K; s ++) 51 {52 If (J + S> N) 53 break; 54 if (S <D) 55 DP [I] [J + S] = (DP [I] [J + S] + dp [I-1] [J]) % md; 56 else 57 DPA [I] [J + S] = (DPA [I] [J + S] + dp [I-1] [J]) % md; 58} 59} 60 if (DPA [I-1] [J]) 61 {62 for (int s = 1; S <= K; s ++) 63 {64 if (J + S> N) 65 break; 66 DPA [I] [J + S] = (DPA [I-1] [J] + DPA [I] [J + S]) % md; 67} 68} 69} 70} 71 for (INT I = 1; I <= N; I ++) 72 ans = (DPA [I] [N] + ans) % md; 73 printf ("% i64d \ n", ANS % MD); 74 return 0; 75}
View code

 

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.