 2017-06-10 09:13

# Lucky Coins Sequence

Problem Description
As we all know,every coin has two sides,with one side facing up and another side facing down.Now,We consider two coins's state is same if they both facing up or down.If we have N coins and put them in a line,all of us know that it will be 2^N different ways.We call a "N coins sequence" as a Lucky Coins Sequence only if there exists more than two continuous coins's state are same.How many different Lucky Coins Sequences exist?

Input
There will be sevaral test cases.For each test case,the first line is only a positive integer n,which means n coins put in a line.Also,n not exceed 10^9.

Output
You should output the ways of lucky coins sequences exist with n coins ,but the answer will be very large,so you just output the answer module 10007.

Sample Input
3
4

Sample Output
2
6

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• You can do like this:

`````` public class Test {

public static void main(String[] args) {
String regex = "^\\d*(\\d)\\1{2,}\\d*\$";

int c = 10;
int len = (int) Math.pow(2, c);
int luckSeq = 0;

for (int i = 0; i < len; i++) {
String s = String.format("%"+c+"s", Integer.toBinaryString(i)).replace(" ", "0");
boolean isMatch = s.matches(regex);
System.out.println("[" + s + "], match:" + isMatch);
if (isMatch) {
luckSeq++;
}
}
System.out.println("LuckSeq size:" + luckSeq);
}

}
``````
点赞 评论 复制链接分享