207 字
1 分钟
灌水
输入
样例输入1:
3 1
样例输出1:
3 1 2
样例输入2:
4 1
样例输出2:
4 3 1 2
样例输入3:
8 17
样例输出3:
6 2 3 1 8 4 5 7
提示
题解
先找规律然后瞎搞就可以了
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
LL Sum[1000005];
LL x;
int ans[1000005];
int main()
{
int n;
scanf("%d%lld", &n, &x);
for (int i = n - 2; i >= 0; i--)
Sum[i] = Sum[i + 1] + i;
if (Sum[0] < x)
{
puts("-1");
return 0;
}
int k, i, j;
for (k = 1; k <= n - 2; k++)
if (x > Sum[k])
break;
x = x - Sum[k];
for (i = n - k, j = 1; i <= n - 1; i++, j++)
ans[j] = i;
for (i = 1; i <= n - k - 1; i++, j++)
ans[j] = i;
ans[n] = n;
i = 1;
while (x--)
{
swap(ans[i], ans[i + 1]);
i++;
}
for (i = 1; i < n; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}