sgu141:Jumping Joe

141. Jumping Joe

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

Joe is a frog who likes to jump a lot. In fact, that's all he does: he jumps forwards and backwards on the integer axis (a straight line on which all the integer numbers, both positive and negative are marked). At first, Joe sits next to the point marked with 0. From here, he can jump in the positive or the negative direction a distance equal to either x1 or x2. From the point where he arrived, he can jump again a distance equal to x1 or x2, in the positive or the negative direction and so on.. Joe wants to arrive next to the point marked with the number P, after exactly K jumps. You have to decide whether such a thing is possible.

Input

The input will contain four integers: x1x2 (0 < x1 , x2 < 40 000), P (-40 000 < P  < 40 000) and (0 <= K < 2 000 000 000), separated by blanks.

Output

The first line of output will contain the word "YES", in case Joe can reach the point marked with P after exactly K jumps, or "NO", otherwise. In case the answer is "YES", the next line should contain four integers, separated by blanks: P1 N1 P2 and N2P1 is the number of times Joe jumped in the positive direction a distance equal to x1N1 is the number of times Joe jumped in the negative direction a distance equal to x1P2 is the number of times Joe jumped in the positive direction a distance equal to x2N2 is the number of times Joe jumped in the negative direction a distance equal to x2. In other words, you should find four non-negative integers, so that:

P1*x1 - N1*x1 + P2*x2 - N2*x2 = P 
P1 + N1 + P2 + N2 = K

In case there are more quadruples (P1,N1,P2,N2) which are solutions for the problem, you may print any of them.

Sample Input

2 3 -1 12

Sample Output

YES
1 0 5 6

Author: Mugurel Ionut Andreica
Resource: SSU::Online Contester Fall Contest #2
Date: Fall 2002
设x=p1-n1,y=p2-n2,则有x*x1+y*x2=p(①),如果p%GCD(x1,x2)!=0很明显无解;
通过扩展GCD求出x,y的一个特解,然而这并不一定是最终的答案,很明显由于p1,n1,p2,n2为非负整数,有|x|+|y|<=k;(可以自己尝试着严格证明)
方程①的通解为x'=x+t*x2/gcd(x1,x2),y'=y-t*x1/gcd(x1,x2),我们可以通过这种方法来对x,y缩放得到最小的|x|+|y|。
(1)如果|x|+|y|>k显然无解。否则到(2)。
(2)我们可以把求出的x,y先赋给p1,n1,p2,n2,还剩下rest=k-|x|-|y|。
(3)如果rest为偶数,p1+=rest/2,n1+=rest/2即可;若rest为奇数,通过之前的方法来调整x和y的值,但如果x2/gcd(x1,x2)+x1/gcd(x1,x2)为偶数的话,无论怎么改变t的值,rest均为奇数,此时无解;
如果x2/gcd(x1,x2)+x1/gcd(x1,x2)为奇数,把t++或t--即可,转到(1)。

详见代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; 

int ext(int a, int b, int &x, int &y)
{
	while(!b)
	{
		x = 1;
		y = 0;
		return a;	
	}
	int re = ext(b, a%b, x, y);
	int tmp = x;
	x = y;
	y = tmp-a/b*y;
	return re;
}

int main()
{
	int x1 = 0, x2 = 0, p = 0, k = 0;
	int x, y, gcd, _x, _y;
	int p1, n1, p2, n2;
	scanf("%d%d%d%d", &x1, &x2, &p, &k);
	gcd = ext(x1, x2, x, y);
	if(p%gcd != 0)
	{
		puts("NO");
		return 0; 
	}
	
	x *= p/gcd;
	y *= p/gcd;
	_x = x2/gcd;
	_y = x1/gcd;
	
	while(abs(x+_x)+abs(y-_y) < abs(x)+abs(y)) 
	{
		x += _x;
		y -= _y;
	}
	while(abs(x-_x)+abs(y+_y) < abs(x)+abs(y)) 
	{
		x -= _x;
		y += _y;	
	}
	
	if(abs(x)+abs(y) > k)
	{
		puts("NO");
		return 0;	
    }
	if(!((k-abs(x)-abs(y))&1))
	{
	  	int tmp = (k-abs(x)-abs(y))>>1;
	 	if(x < 0) p1 = 0, n1 = -x;
	  	else p1 = x, n1 = 0;
	  	if(y < 0) p2 = 0, n2 = -y;
	  	else p2 = y, n2 = 0;
	  	p1 += tmp;
	  	n1 += tmp; 	
	}
	else 
	{
		if((_x+_y)&1)
		{
			if(abs(x+_x)+abs(y-_y) <= k) 
			{
				x += _x;
				y -= _y;	
			}
			else if(abs(x-_x)+abs(y+_y) <= k)
			{
				x -= _x;
				y += _y;	
			}
			else 
			{
			  	puts("NO");
				return 0;
			}
			int tmp = (k-abs(x)-abs(y))>>1;
	 		if(x < 0) p1 = 0, n1 = -x;
	  		else p1 = x, n1 = 0;
	  		if(y < 0) p2 = 0, n2 = -y;
	  		else p2 = y, n2 = 0;
	  		p1 += tmp;
	  		n1 += tmp; 	
	    }
	    else 
	    {
	    	puts("NO");
			return 0;
	    }
	}
	puts("YES");
	printf("%d %d %d %d\n", p1, n1, p2, n2);
	return 0;  	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值