题意:本来有n张牌,每个牌上有一个数字,范围是1~l,用倒数第二张减倒数第一张,然后去掉刚刚的两张,再把这个数放在最后,不断重复这个操作最终剩下数字d(其实就是把所有奇数位上的数相加减去偶数位上的数)。
给出上述的数字,求最开始n张牌可能的序列,如果不存在可能的序列,输出-1。
思路:分d>0和d<=0两种情况。
当d>0时,因为d=奇数位和-偶数位和,奇数位全取l,偶数位全取1为d最大的情况,若给定的d比这个值大则输出-1。
偶数位和是固定的,用关系求出奇数位和,先平均分到每个奇数位,然后把多出来的再分开即可。
d<=0时相似。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max_ 100010
using namespace std;
int n,d,l;
int num[110];
int main()
{
while(cin>>n>>d>>l)
{
int od=(n+1)/2;
int even=n/2;
if(d<=0&&od*1-even*l>d)
{
printf("-1\n");
continue;
}
if(d>0&&od*l-even*1<d)
{
printf("-1\n");
continue;
}
int a,b;
if(d>0)
{
b=even*1;
a=d+b;
int c=a%od;
printf("%d",a/od);
for(int i=2;i<=n;i++)
{
if(i&1)
{
if(c)
printf(" %d",a/od+1),c--;
else
printf(" %d",a/od);
}
else
{
printf(" 1");
}
}
}
else
{
a=od*1;
b=a-d;
int c=b%even;
printf("1 %d",b/even);
for(int i=3;i<=n;i++)
{
if(i&1)
{
printf(" 1");
}
else
{
if(c)
printf(" %d",b/even+1),c--;
else
printf(" %d",b/even);
}
}
}
printf("\n");
}
}