Codeforces 1244C The Football Season

题目:C. The Football Season

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets w points, and the opposing team gets 0 points. If the game results in a draw, both teams get d points.

The manager of the Berland capital team wants to summarize the results of the season,but, unfortunately, all information about the results of each match is lost. The manager only knows that the team has played n games and got p points for them.

You have to determine three integers x, y and z — the number of wins, draws and loses of the team. If there are multiple answers, print any of them. If there is no suitable triple (x,y,z), report about it.

Input

The first line contains four integers n, p, w and d (1≤n≤1012,0≤p≤1017,1≤d<w≤105) — the number of games, the number of points the team got, the number of points awarded for winning a match, and the number of points awarded for a draw, respectively. Note that w>d, so the number of points awarded for winning is strictly greater than the number of points awarded for draw.

Output

If there is no answer, print −1.Otherwise print three non-negative integers x, y and z — the number of wins, draws and losses of the team. If there are multiple possible triples (x,y,z), print any of them. The numbers should meet the following conditions:

x⋅w+y⋅d=p,
x+y+z=n.

Examples

Input
30 60 3 1
Output
17 9 4
Input
10 51 5 4
Output
-1
Input
20 0 15 5
Output
0 0 20

Note

One of the possible answers in the first example — 17wins, 9 draws and 4 losses. Then the team got 17⋅3+9⋅1=60 points in 17+9+4=30 games.

In the second example the maximum possible score is 10⋅5=50. Since p=51, there is no answer.

In the third example the team got 0points, so all 20 games were lost.

思路:

1.找到一组非负的(x,y,z)满足x⋅w+y⋅d=px+y+z=n
2.z我们可以先不考虑,只需找到一组(x,y)满足x⋅w+y⋅d=p0≤x+y≤n
3.对等式x⋅w+y⋅d=p,若存在一组非负整数(x0,y0)满足这个等式,那必定存在一组(x1,y1)满足这个等式且其中的y<w
(对这个结论的解释:如果存在一对(a,b)满足等式且y≥w,那必定存在一对(a+[b/w]*d,b%w)也满足这个等式,其中[a]代表不超过a的最大整数,大家可自行验证^ _ ^)
4.发现上述规律最大的好处就是可以缩短暴力次数啦,我们将y0遍历到w-1,遍历的时候如果x<0或者x+y>nbreak,如果遍历到满足条件的就输出,否则输出-1就好了~;

代码:

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include<iostream>
using namespace std;
typedef long long ll;
int main(){
	IOS;
	ll n,p,w,d;
	cin>>n>>p>>w>>d;
	for(int y=0;y<w;y++){
		if((p-d*y)%w) continue;
		ll x=(p-d*y)/w;
		if(y+x<=n&&x>=0){
			cout<<x<<" "<<y<<" "<<n-y-x;
			return 0;
		}		
		else break;
	}
	cout<<"-1";
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308946.html