[ACM] zoj 3725 painting storages (DP count + combination)

Source: Internet
Author: User

Painting storages Time Limit: 2 seconds memory limit: 65536 KB

There is a straight highwayNStorages alongside it labeled1, 2, 3,..., n. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with exactly one color.

Bob has a requirement: there are at leastMContinuous storages (e.g. "2, 3, 4" are 3 continuous storages) to be painted with red. How many ways can you paint all storages under Bob's requirement?

Input

There are multiple test cases.

Each test case consists a single line with two integers:NAndM(0 <n, m <= 100,000 ).

Process to the end of input.

Output

One line for each case. output the number of ways module 1000000007.

Sample Input
4 3 
Sample output
3
Author: Zhao, Kui
Contest: zoj monthly, Jun 2013

Solution:

This question is very similar to that in the provincial competition .. If you have done this before, you won't be confused during the provincial competition...

This question cannot be simply combined...

Color n warehouses (numbers 1-N) in red and blue colors. At least m consecutive warehouses must be painted in red. Result modulus 1000000007

DP [I] indicates the number of methods where the previous I warehouse must satisfy at least m continuous warehouse red.

So DP [m] must be 1, DP [, ...... M-1] are 0.

There are two situations when obtaining DP [I ]:

I. The front I-1 warehouse coloring has met the meaning of the question, then I warehouse painted what color can, there DP [I] = 2 * DP [I-1]; (may be out of range, don't forget mod)

II. When the I-th warehouse is painted red, the M-th consecutive warehouse is red, and the interval [I-m + 1, I] is red, the I-m warehouse must be blue and the warehouse from 1 to i-m-1 must be non-conforming to the color of the meaning, so all the coloring methods of the warehouse from 1 to i-m-1 2 ^ (i-m-1) subtract the number of methods that match the question DP [i-m-1]. So the number of methods is 2 ^ (i-m-1)-DP [i-m-1]

Code:

# Include <iostream> # include <string. h> using namespace STD; const int mod = 1000000007; const int maxn = 100005; int power [maxn + 1]; int DP [maxn]; // The first I warehouse meets the requirements of M warehouse methods that are red int n, m; void getpower () // The number of power pairs that are preprocessed to 2 {power [0] = 1; for (INT I = 1; I <= maxn; I ++) power [I] = power [I-1] * 2% MOD;} int main () {getpower (); while (CIN> N> m) {memset (DP, 0, sizeof (DP); DP [m] = 1; for (INT I = m + 1; I <= N; I ++) DP [I] = (DP [I-1] * 2% mod + power [i-m-1]-DP [i-m-1]) % MOD; cout <DP [N] <Endl;} return 0 ;}


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.