There are n houses in a row. They are numbered from 1 to n in order from left to right. Initially you are in the house 1.You have to perform kmoves to other house. In one move you go from your current house to some other house. You can't stay where you are (i.e., in each move the new house differs from the current house). If you go from the house x to the house y, the total distance you walked increases by |x−y| units of distance, where |a| is the absolute value of a. It is possible to visit the same house multiple times (but you can't visit the same house in sequence).Your goal is to walk exactly s
units of distance in total.If it is impossible, print "NO". Otherwise print "YES" and any of the ways to do that. Remember that you should do exactly kmoves.
Input
The first line of the input contains three integers n, k, s (2≤n≤109, 1≤k≤2⋅105, 1≤s≤1018) — the number of houses, the number of moves and the total distance you want to walk.
Output
If you cannot perform kmoves with total walking distance equal to s, print "NO".Otherwise print "YES" on the first line and then print exactly kintegers hi (1≤hi≤n) on the second line, where hi is the house you visit on the i-th move.For each jfrom 1 to k−1 the following condition should be satisfied: hj≠hj+1. Also h1≠1 should be satisfied.
Examples
Input
10 2 15
Output
YES
10 4
Input
10 9 45
Output
YES
10 1 10 1 2 1 2 1 6
Input
10 9 81
Output
YES
10 1 10 1 10 1 10 1 10
Input
10 9 82
Output
NO
有n个房间,开始你处于第1个房间,问你移动k次,有没有一种方案使你移动的距离正好是s,有输出方案,没有输出NO。每次移动距离计算式:|当前房间号 - 移动后的房间号|,每次移动不能改变方向。
可以先打个表,记录每次移动应该走的步数,(即制定一个移动步数计划),若无法制定出计划(无法移动s),或计划不合理(不能正好移动k次),则输出NO
制定移动步数计划:
将s均分,剩下的步数分给前面的,从第一天开始,该天步数加一,直至分完。(构造)
按照移动步数计划,输出每次的到达点,先处理大的步数,这样能保证之后的移动不会越界
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define N 300000 + 10
using namespace std;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int GCD(int a,int b)
{
return b ? GCD(b,a%b) : a;
}
int main()
{
LL i,n,k,s,r,d;
LL pre[N]={0};
LL ans[N]={0};
scanf("%lld%lld%lld",&n,&k,&s);
d=s/k;
r=s%k;
for(i=0;i<k;i++){
pre[i]=d;
if(i<r)
pre[i]++;
}
for(i=0;i<k;i++)
if(pre[i]==0 || pre[i]>=n)
break;
if(i<k){
printf("NO\n");
return 0;
}
printf("YES\n");
LL neg=1,pos=1;
for(i=0;i<k;i++){
pos+=neg*pre[i];
ans[i]=pos;
neg*=-1;
}
for(i=0;i<k;i++)
printf("%lld%c",ans[i],i==k-1?'\n':' ');
return 0;
}
————————————————————————————————————————————————————
更新,也可以不用打表,只需记录当前位置,因为已经知道下一步该怎么走。
不过需要计算一下,能不能正好在k步走s的距离,若满足以下两种条件之一就不行:
1,每次只走一步都比要求的步数多
2,每次走最大步数都达不到所要求的步数
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define N 300000 + 10
#define INF 0x3f3f3f3f
using namespace std;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int GCD(int a,int b)
{
return b ? GCD(b,a%b) : a;
}
int main()
{
LL i,n,k,s,r,d;
scanf("%lld%lld%lld",&n,&k,&s);
d=s/k;
r=s%k;
if( s<k || s>(n-1)*k )
printf("NO\n");
else
{
printf("YES\n");
LL neg=1,pos=1;
for(i=0; i<k; i++)
{
pos+=neg*(d+(i<r));
printf("%lld%c",pos,i==k-1?'\n':' ');
neg*=-1;
}
}
return 0;
}