Lucky Coins Sequence

Lucky Coins Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35 Accepted Submission(s): 29
 

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

 

Source

2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU


Recommend

zhengfeng

/*
题意:定义一个硬币序列,如果超过三个相邻的硬币的正反相同,那么这个序列就是Lucky序列,给出序列
    长度n,问有几种Lucky序列

初步思路:这种题型一般都是递推过来的,不管了先打表试一下
    打表结果:(从3开始的)
        2
        6
        16
        38
        86
        188
        402
        846
        1760
        3630
        7438
        15164
        30794
        62342
        125904
        253782
        510758
        1026684
    每项和前项的二倍差的数刚好是一个变形的斐波那契数列。
    得到递推公式:G(n)=2*(G(n-1)+F(n-2));
#感悟:一边AC爽
*/
#include<bits/stdc++.h>
#define mod 10007
using namespace std;
/********************************矩阵模板**********************************/
class Matrix {
    public:
        int a[3][3]; 

        void init(int x) {
            memset(a,0,sizeof(a));
            if(x==1){
                a[0][0]=2;
                a[0][1]=1;
                a[0][2]=1;
            }else{
                a[0][0]=2;
                a[1][0]=2;
                a[1][1]=1;
                a[1][2]=1;
                a[2][1]=1;
            }
        }

        Matrix operator +(Matrix b) {
            Matrix c;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
            return c;
        }

        Matrix operator +(int x) {
            Matrix c = *this;
            for (int i = 0; i < 3; i++)
                c.a[i][i] += x;
            return c;
        }

        Matrix operator *(Matrix b)
        {
            Matrix p;
            memset(p.a,0,sizeof p.a);
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                for (int k = 0; k < 3; k++)
                    p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
            return p;
        }

        Matrix power_1(int t) {
            Matrix ans,p = *this;
            memset(ans.a,0,sizeof ans.a);
            for(int i=0;i<3;i++) ans.a[i][i]=1;
            while (t) {
                if (t & 1)
                    ans=ans*p;
                p = p*p;
                t >>= 1;
            }
            return ans;
        }

        Matrix power_2(Matrix a,Matrix b,int x){
            while(x){
                if(x&1){
                    b=a*b;
                }
                a=a*a;
                x>>=1;
            }
            return b;
        }
}unit,init;
/********************************矩阵模板**********************************/
int n;
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        if(n<3){
            printf("0\n");
            continue;
        }
        if(n==3){
            printf("2\n");
            continue;
        }
        unit.init(1);
        init.init(0);
        // for(int i=0;i<3;i++){
            // for(int j=0;j<3;j++){
                // cout<<unit.a[i][j]<<" ";
            // }
            // cout<<endl;
        // }
        init=init.power_1(n-3);
        printf("%d\n",(unit*init).a[0][0]);
    }
    return 0;
}
时间: 2024-10-16 10:55:00

Lucky Coins Sequence的相关文章

HDU5985 Lucky Coins 概率dp

题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[10][1e6]这个状态让我觉得丝毫没有用,当时一直以为每种硬币是互相独立的.然后中间吃了个饭,回来又想了很久很久,就是不想看题解,然后发现,这个dp和极限有关,同时状态跟走了几步有极大的关系.否则你初始值根本没法赋,那么显然我猜既然保留6位,肯定当数字小到一定程度时是可以忽略的,也就是进行的次数多

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955

2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5984 Pocky 题意 给出一根棒子(可以吃的)的长度x和切割过程中不能小于的长度d,每次随机的选取一个位置切开,吃掉左边的一半,对右边的棒子同样操作,直至剩余的长度不大于d时停止.现在给出x和d,问切割次数的数学期望是多少. 解题思路 当看到第二个样例2 1时,结果是1.693147,联想到ln

ACM选修HUST1058(市赛题) Lucky Sequence 同余定理

Description Edward  得到了一个长度为  N  的整数序列,他想找出这里面有多少个“幸运的”连续子序列.一个连续子序列被称为“幸运的”,当且仅当该子序列内的整数之和恰好是  K  的整数倍数.他请求你写一个程序来计算他喜欢的连续子序列个数. Input 输入第一行是一个整数  T,表示有  T  组数据.每组数据第一行是两个整数  N (1 <= N <= 10^6), K (1 <= K <= 10^9).接下来的一行包含  N  个整数  Ai (|Ai| &

ACM: Gym 101047M Removing coins in Kem Kadr&#227;n - 暴力

Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Andréh and his friend Andréas are board-game aficionados. They know many of their friends

PAT1068. Find More Coins

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: f

1068. Find More Coins (30)

动态背包 题意: 给定一系列的硬币值, 然后给定一个目标value, 从所有硬币中找出几个, 使得这几个硬币的和正好等于这个value, 而且这个硬币序列应该是满足硬币值字典序的最小序列. 分析: 属于典型的背包问题. 用动态规划(dp)做, 假设F(N, M)表示不超过面值M, 而且从前面N个硬币中挑选硬币值能得到的最大硬币面值总和, 那我们可以得到如下递归公式: F(N, M) = max{ F(N–1, M), F(N–1, M–c(N)) + c(N) },c(N)表示第N个硬币的面值

CodeForces Gym 101047M Removing coins in Kem Kadr&#227;n 暴力

Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 101047M Description standard input/output Andréh and his friend Andréas are board-game aficionados. They know many of their friends would lo

寒假集训.Lucky Tickets. Easy!

Lucky Tickets. Easy! Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Background The public transport administration of Ekaterinburg is anxious about the fact that passengers don't like to pay for