hdu6130_Kolakoski_模拟递推

hdu6130_Kolakoski_模拟递推

描述

This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This sequence consists of 1 and 2, and its first term equals 1. Besides, if you see adjacent and equal terms as one group, you will get 1,22,11,2,1,22,1,22,11,2,11,22,1……. Count number of terms in every group, you will get the sequence itself. Now, the sequence can be uniquely determined. Please tell HazelFan its nth element.

输入

The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
A single line contains a positive integer n(1≤n≤107).

输出

For each test case:
A single line contains a nonnegative integer, denoting the answer.

样例输入

2
1
2

样例输出

1
2

题意

  • 开始读了半天,意思就是有这么一个序列1,2,2,1……,然后可以转化为1,22,11…..这么个序列,同时,第二个序列的每一项 bi 的数字若为2位数,那么对应的 ai 等于2,若 bi 是一位数,那么对应的 ai 就是1。
  • 现在需要求出给出 an 的值。

思路

  • 首先我们已经知道了一部分的a序列和b序列,现在我们开始打表,把a序列求出来即可。
  • 我们用两个下标indexa,indexb来表示分别表示要求得 a[i] 的下标以及 b[i] 的下标(b[i]实际不用求出)。那么我们在求 a[indexa] 时,先假定一定不与 a[indexa-1] 相同,然后我们看a[indexb],如果值是2,那么说明b[indexb]应该是两位数,那么a[indexa]和a[indexa+1]一定是相同的。如果b[indexb]是1,那么说明a[indexa]与a[indexa+1]一定不同。
  • 对于假设a[indexa]一定不能与a[indexa-1],因为我们做到了如果a[indexa]==a[indexa+1]就更新两个值。所以假设是成立的。

AC代码

    #include<iostream>
    #include<cstdio>
    using namespace std;

    const int maxn = 1e7 +5;

    int a[maxn] = {1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1};
    int n;

    int main()
    {
        int indexa = 20;
        int indexb = 13;

        while(indexa < 1e7)
        {
            int temp = a[indexa - 1];
            if(temp == 1)
            {
                a[indexa] = 2;
            }
            else if(temp == 2)
            {
                a[indexa] = 1;
            }

            if(a[indexb] == 1)
            {
                indexa ++ ;
                indexb ++;
            }

            else if(a[indexb] == 2)
            {
                if(temp == 1)
                {
                    a[indexa + 1] = 2;  
                }       

                else 
                    a[indexa + 1] = 1;

                indexa +=2;
                indexb +=1;
            }
        }

        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            cout<<a[n-1]<<endl;
        }

        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值