CF1244C The Football Season

C. The Football Season

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 — 17 wins, 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 0 points, so all 20 games were lost.

 

擴展歐幾里得:需要求的只有x, y. 因爲勝局數和平局數有權值。

X1 = p/w ,    X2 = p%w/d,    X3 = p%w%d

即解   (m*w+x3)%d == 0.  其中已知x3, w, d.  且m的範圍是 [0, x1].

即解   m*w%d  =  d-x3.  m的範圍是 [0, x1].   

就轉化爲用擴展歐幾里得解模線性方程ax=b (mod n)。 a: w,  b: d-x3,  n: d。

 

AC代碼

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//擴展歐幾里得算法
ll extended_gcd(ll a, ll b, ll &x, ll &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll r = extended_gcd(b, a % b, x, y);
	ll t = x;
	x = y;
	y = t - a / b * y;
	return r;
}
//用擴展歐幾里得解模線性方程ax=b (mod n)
ll modularLinearEquation(ll a, ll b, ll n)
{
	ll x, y, x0, i;
	ll d = extended_gcd(a, n, x, y);     //ax=b (mod n) 等價於ax+ny=b
	if (b%d)
		return -1;
	x0 = x*(b / d) % n;
//	for (i = d; i >= 1; i--){
//        printf("%d\n", (x0 + i*(n / d)) % n);
//	}

	return (x0 + d*(n / d)) % n;
}
int main()
{
	ll n,p,w,d;
	cin>>n>>p>>w>>d;
	ll x1, x2,x3;
	x1 = p/w;
	if(p%w == 0){
        if(x1<=n){
            cout<<x1<<' '<<0<<' '<<n-x1;
        }
        else cout<<-1;
        return 0;
	}
	x2 = p%w/d;
	x3 = p%w%d;
    if(x3 == 0){
        if(x1+x2<=n){
            cout<<x1<<' '<<x2<<' '<<n-x1-x2;
        }
        else cout<<-1;
        return 0;
    }
    ll ans = modularLinearEquation(w, d-x3, d);
    if(ans == -1||ans > x1){
        cout<<-1;
        return 0;
    }
    else{
        x1 -= ans;
        x2 += (x3+ans*w)/d;
        if(x1+x2<=n){
            cout<<x1<<' '<<x2<<' '<<n-x1-x2;
        }
        else cout<<-1;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章