: Finite Encyclopedia of Integer Sequences

问题 F: Finite Encyclopedia of Integer Sequences

时间限制: 1 Sec  内存限制: 128 MB
提交: 304  解决: 62
[提交] [状态] [讨论版] [命题人:admin]

题目描述

In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 and N (inclusive) consisting of integers between 1 and K (inclusive) are listed.
Let the total number of sequences listed in FEIS be X. Among those sequences, find the (X⁄2)-th (rounded up to the nearest integer) lexicographically smallest one.

Constraints
1≤N,K≤3×105
N and K are integers.

输入

Input is given from Standard Input in the following format:
K N

输出

Print the (X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence listed in FEIS, with spaces in between, where X is the total number of sequences listed in FEIS.

样例输入

3 2

样例输出

2 1 

提示

There are 12 sequences listed in FEIS: (1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3). The (12⁄2=6)-th lexicographically smallest one among them is (2,1).

1.k为偶时:因为序列总数x=,那(X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence一定是{k/2,k,k,k...}

2.k为奇时:可以证明(不贴了),(X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence是{k/2,k/2,k/2,...}的前[n/2](取下整)个序列,(即{k/2,k/2,k/2,...}再向前挪[n/2]就是答案);

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int n,i,m;
    int len;
    int a[300010];
    scanf("%d%d",&m,&n);
    if(m%2==0)
    {
        printf("%d",m/2);
        for(i=2;i<=n;i++)
        {
            printf(" %d",m);
        }
        printf("\n");
    }
    else
    {
        int t;
        if(n%2==1)
        {
            t=(n-1)/2;
        }
        else
        {
            t=(n-1)/2+1;
        }
        for(i=1;i<=n;i++)
        {
            a[i]=m/2+1;
        }
        int len=n;
        while(t--)
        {
            if(a[len]==1)
            {
                len--;
            }
            else
            {
                a[len]--;
                for(i=len+1;i<=n;i++)
                {
                    a[i]=m;
                }
                len=n;
            }
        }
        for(i=1;i<=len;i++)
        {
            if(i!=1)
            {
                printf(" ");
            }
            printf("%d",a[i]);
        }
        printf("\n");
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/yangkunming12138/article/details/81407183