Painting Fence - Stuck to find out generalized version
$begingroup$
Problem Statement:
There is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
My logic: 1st
Number of post :1
Number of color :1
Then I can paint the post using one way
2nd
Number of post:2
Number of Color: 1
Number of way I can paint it 1
If number of color: 2
Number of way I can paint the fence:
Say I have two color Red and Blue
I can colored the fence
Red Red
Blue Blue
Red Blue
Now if the number of color :3(Red, Blue, Green)
Red Red
Blue Blue
Green Green
Red Blue
Red Green
Blue Green
So, No. of way 6.
If I increase number of color and the posts then I get some other combination.
The generalized combination may be : 3*2*1 (redbluegreen), If I consider I have k color so the formula become k*(k-1)*(k-2).
So, k*(k-1)*(k-2) Number of way.
Am I thinking correctly? I cannot find out a generalized version for n post and k color of this problem.
Please help me to find out a general formula of this question.
puzzle recursion
$endgroup$
|
show 2 more comments
$begingroup$
Problem Statement:
There is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
My logic: 1st
Number of post :1
Number of color :1
Then I can paint the post using one way
2nd
Number of post:2
Number of Color: 1
Number of way I can paint it 1
If number of color: 2
Number of way I can paint the fence:
Say I have two color Red and Blue
I can colored the fence
Red Red
Blue Blue
Red Blue
Now if the number of color :3(Red, Blue, Green)
Red Red
Blue Blue
Green Green
Red Blue
Red Green
Blue Green
So, No. of way 6.
If I increase number of color and the posts then I get some other combination.
The generalized combination may be : 3*2*1 (redbluegreen), If I consider I have k color so the formula become k*(k-1)*(k-2).
So, k*(k-1)*(k-2) Number of way.
Am I thinking correctly? I cannot find out a generalized version for n post and k color of this problem.
Please help me to find out a general formula of this question.
puzzle recursion
$endgroup$
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13
|
show 2 more comments
$begingroup$
Problem Statement:
There is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
My logic: 1st
Number of post :1
Number of color :1
Then I can paint the post using one way
2nd
Number of post:2
Number of Color: 1
Number of way I can paint it 1
If number of color: 2
Number of way I can paint the fence:
Say I have two color Red and Blue
I can colored the fence
Red Red
Blue Blue
Red Blue
Now if the number of color :3(Red, Blue, Green)
Red Red
Blue Blue
Green Green
Red Blue
Red Green
Blue Green
So, No. of way 6.
If I increase number of color and the posts then I get some other combination.
The generalized combination may be : 3*2*1 (redbluegreen), If I consider I have k color so the formula become k*(k-1)*(k-2).
So, k*(k-1)*(k-2) Number of way.
Am I thinking correctly? I cannot find out a generalized version for n post and k color of this problem.
Please help me to find out a general formula of this question.
puzzle recursion
$endgroup$
Problem Statement:
There is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
My logic: 1st
Number of post :1
Number of color :1
Then I can paint the post using one way
2nd
Number of post:2
Number of Color: 1
Number of way I can paint it 1
If number of color: 2
Number of way I can paint the fence:
Say I have two color Red and Blue
I can colored the fence
Red Red
Blue Blue
Red Blue
Now if the number of color :3(Red, Blue, Green)
Red Red
Blue Blue
Green Green
Red Blue
Red Green
Blue Green
So, No. of way 6.
If I increase number of color and the posts then I get some other combination.
The generalized combination may be : 3*2*1 (redbluegreen), If I consider I have k color so the formula become k*(k-1)*(k-2).
So, k*(k-1)*(k-2) Number of way.
Am I thinking correctly? I cannot find out a generalized version for n post and k color of this problem.
Please help me to find out a general formula of this question.
puzzle recursion
puzzle recursion
asked Dec 27 '18 at 19:37
EncipherEncipher
1246
1246
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13
|
show 2 more comments
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13
|
show 2 more comments
1 Answer
1
active
oldest
votes
$begingroup$
Replace the colors with numbers $0,1,dots,k-1$. For every numbering of the $n$ fenceposts, consider its sequence of differences, which is a sequence of $n-1$ numbers equalling the difference modulo $k$ between each pair of adjacent fenceposts. A fencepost coloring is legal if any only if its sequence of differences does not contain two adjacent zeroes.
For example, when $k=4$, the difference sequence of $01223121$ is $1101213$.
How many sequences of $n-1$ numbers in ${0,1,dots,k-1}$ contain no two adjacent zeroes? Letting $a_{n-1}$ be this number, you can show $a_n=(k-1)a_{n-1}+(k-1)a_{n-2}$. The first summand counts sequences which do not end with zero, and the second counts ones which do. This lets you compute $a_n$ recursively.
Finally, every fencepost coloring is uniquely determined by its first element and its difference sequence, so there are $ka_{n-1}$ difference sequences.
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3054296%2fpainting-fence-stuck-to-find-out-generalized-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Replace the colors with numbers $0,1,dots,k-1$. For every numbering of the $n$ fenceposts, consider its sequence of differences, which is a sequence of $n-1$ numbers equalling the difference modulo $k$ between each pair of adjacent fenceposts. A fencepost coloring is legal if any only if its sequence of differences does not contain two adjacent zeroes.
For example, when $k=4$, the difference sequence of $01223121$ is $1101213$.
How many sequences of $n-1$ numbers in ${0,1,dots,k-1}$ contain no two adjacent zeroes? Letting $a_{n-1}$ be this number, you can show $a_n=(k-1)a_{n-1}+(k-1)a_{n-2}$. The first summand counts sequences which do not end with zero, and the second counts ones which do. This lets you compute $a_n$ recursively.
Finally, every fencepost coloring is uniquely determined by its first element and its difference sequence, so there are $ka_{n-1}$ difference sequences.
$endgroup$
add a comment |
$begingroup$
Replace the colors with numbers $0,1,dots,k-1$. For every numbering of the $n$ fenceposts, consider its sequence of differences, which is a sequence of $n-1$ numbers equalling the difference modulo $k$ between each pair of adjacent fenceposts. A fencepost coloring is legal if any only if its sequence of differences does not contain two adjacent zeroes.
For example, when $k=4$, the difference sequence of $01223121$ is $1101213$.
How many sequences of $n-1$ numbers in ${0,1,dots,k-1}$ contain no two adjacent zeroes? Letting $a_{n-1}$ be this number, you can show $a_n=(k-1)a_{n-1}+(k-1)a_{n-2}$. The first summand counts sequences which do not end with zero, and the second counts ones which do. This lets you compute $a_n$ recursively.
Finally, every fencepost coloring is uniquely determined by its first element and its difference sequence, so there are $ka_{n-1}$ difference sequences.
$endgroup$
add a comment |
$begingroup$
Replace the colors with numbers $0,1,dots,k-1$. For every numbering of the $n$ fenceposts, consider its sequence of differences, which is a sequence of $n-1$ numbers equalling the difference modulo $k$ between each pair of adjacent fenceposts. A fencepost coloring is legal if any only if its sequence of differences does not contain two adjacent zeroes.
For example, when $k=4$, the difference sequence of $01223121$ is $1101213$.
How many sequences of $n-1$ numbers in ${0,1,dots,k-1}$ contain no two adjacent zeroes? Letting $a_{n-1}$ be this number, you can show $a_n=(k-1)a_{n-1}+(k-1)a_{n-2}$. The first summand counts sequences which do not end with zero, and the second counts ones which do. This lets you compute $a_n$ recursively.
Finally, every fencepost coloring is uniquely determined by its first element and its difference sequence, so there are $ka_{n-1}$ difference sequences.
$endgroup$
Replace the colors with numbers $0,1,dots,k-1$. For every numbering of the $n$ fenceposts, consider its sequence of differences, which is a sequence of $n-1$ numbers equalling the difference modulo $k$ between each pair of adjacent fenceposts. A fencepost coloring is legal if any only if its sequence of differences does not contain two adjacent zeroes.
For example, when $k=4$, the difference sequence of $01223121$ is $1101213$.
How many sequences of $n-1$ numbers in ${0,1,dots,k-1}$ contain no two adjacent zeroes? Letting $a_{n-1}$ be this number, you can show $a_n=(k-1)a_{n-1}+(k-1)a_{n-2}$. The first summand counts sequences which do not end with zero, and the second counts ones which do. This lets you compute $a_n$ recursively.
Finally, every fencepost coloring is uniquely determined by its first element and its difference sequence, so there are $ka_{n-1}$ difference sequences.
answered Dec 28 '18 at 1:25
Mike EarnestMike Earnest
27.8k22152
27.8k22152
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3054296%2fpainting-fence-stuck-to-find-out-generalized-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Is RRBB a legal coloring?
$endgroup$
– Mike Earnest
Dec 27 '18 at 19:43
$begingroup$
You have omitted some possible colorings. For $2$ posts in $2$ colors there are $2^2=4$ colorings: RR,RB,BR,BB and for $3$ colors there are $3^2=9$ colorings: RR,RG,RB,GR,GG,GB,BR,BG,BB.
$endgroup$
– Daniel Mathias
Dec 27 '18 at 22:03
$begingroup$
There's an ambiguity in the question. Does it mean: (i) there must never be $3$ successive posts the same colour but any number of pairs is OK, or (ii) there must be at most one pair of adjacent identically-coloured posts?
$endgroup$
– timtfj
Dec 28 '18 at 0:03
$begingroup$
@MikeEarnest yes.
$endgroup$
– Encipher
Dec 28 '18 at 1:11
$begingroup$
@timtfj It mean : there must never be 3 successive posts the same colour but any number of pairs is OK
$endgroup$
– Encipher
Dec 28 '18 at 1:13