6617: Finite Encyclopedia of Integer Sequences

6617: Finite Encyclopedia of Integer Sequences

时间限制: 1 Sec  内存限制: 128 MB
提交: 341  解决: 75
[提交] [状态] [讨论版] [命题人: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).

来源/分类

ABC077&ARC084 

这题找规律,打表程序我放这里了

#include<bits/stdc++.h>
#define ll long long
#define mod (ll)(1e9+7)
using namespace std;
int n = 4, k = 3;
int cnt = 0;
struct P
{
    char a[10];
}p[10005];
int cmp(P a,P b)
{
    return strcmp(a.a,b.a)<0;
}
int an[100];
int vis[100];
void dfs(int t,int n)
{
    if(t>=n)
    {
        for(int i=0;i<n;i++)
            p[cnt].a[i]='0'+an[i];
        cnt++;
        return;
    }
    for(int i=1;i<=k;i++)
    {
        an[t]=i;
        dfs(t+1,n);
    }
}
int main()
{
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        dfs(0,i);
    }
    sort(p,p+cnt,cmp);
    for(int i=0;i<cnt;i++)
    {
        if(i&&i%5==0)printf("\n");

        printf("%-10s",p[i].a);
    }
    printf("\ncnt = %d\n",cnt);
    printf("ans = %s",p[cnt/2-1].a);

    return 0;
}

那么代码,源自这儿、

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,s,t) for(int i=s;i<=t;i++)
const int MAX=3e5+5;
int a[MAX];
int main()
{
    int n,k,rem,p;
    while(cin>>k>>n)
    {
        p=n;
        if(k%2==0)
        {
            rep(i,2,n)a[i]=k;
            a[1]=k/2;
        }
        else
        {
            rep(i,1,n)a[i]=(k+1)/2;
            rem=n/2,p=n;
            while(rem--) ///按字典序往后退
            {
                if(a[p]==1)a[p--]--; ///向上退
                else
                {
                    a[p]--;
                    while(p<n)a[++p]=k; ///回退到了上一课子树,并直接到底
                }
            }
        }
        rep(i,1,p)printf("%d%c",a[i],i<p?' ':'\n');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/81415264
今日推荐