Finite Encyclopedia of Integer Sequences
时间限制: 1 Sec 内存限制: 128 MB
提交: 343 解决: 76
[提交] [状态] [讨论版] [命题人: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).
题意:给你个k和一个n,现在要组成序列,k表示能用1-k的数字,n表示序列的长度可以是1-n。如:k=3,n=2,则可以组成的序列是(1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3)共12个,设总数有X个,问你按照字典序排序第X/2个是那个序列。
题解:自己写几个例子推推就出来了
1、k为偶数,序列为:k/2、k、k、k.......,共n个数。
2、k为奇数,序列为:(k+1)/2、(k+1)/2、(k+1)/2、(k+1)/2.......再往前推2/n个。(代码直接模拟往前推n/2的过程即可)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <vector>
#include<stack>
#include<numeric>
using namespace std;
const int m = 1e5;
typedef long long ll;
ll a[3*m+5];
int main()
{
int k,n;
scanf("%d%d",&k,&n);
if(k%2==0)
{
printf("%d",k/2);
for(int i = 2; i <= n; i++)
printf(" %d",k);
printf("\n");
}
else
{
for(int i = 1; i <= n; i++)
a[i] = (k+1)/2;
int len = n;
for(int i = 1; i <= n/2; i++) //模拟 ,模拟次数找规律
{
if(a[len] == 1) len--;
else
{
for(int j = len+1; j <= n; j++)
a[j] = k;
a[len]--;
len = n;
}
}
for(int i = 1; i <= len; i++)
if(i==1)printf("%d",a[1]);
else printf(" %d",a[i]);
printf("\n");
}
return 0;
}