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;  	
}


内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值