CodeForces 1244C-exgcd?

C. The Football Season

Title effect: a game, win points amazing w, d points for a tie, if lose the game, the score is not now conducted a total of n games, to give the points p, w and d are given

Solution to meet the meaning of the questions asked whether obtained

Topic links: https://codeforces.com/contest/1244/problem/C

 

Problem-solving ideas: This question can be used to expand the Euclidean algorithm to solve, but because of too much food, only to find him another way, we note that, if I win d innings, a draw is w innings, this time the score it's the same. Solvability of the case so, we have to assume that we draw is the number of y

If y is less than w y this time we can iterate from 0 to w, does not time out, if y is greater than w, we can be w * d to score to win the bureau to become the number inside, that is, the number of innings into a win x + d, at this time, x + y become x + d + yw x + y is less than the original but the total score is unchanged

In the case of solvability, there must be a set of solutions y <w, where if y is from 0 to w is not found, then it belongs to the case where no solution

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,p,w,d;
    cin>>n>>p>>w>>d;
    for(ll y=0;y<w;y++)
    {
        ll ans=p-y*d;
        if(ans%w==0&&y+ans/w<=n&&ans>=0)
        {
            cout<<ans/w<<" "<<y<<" "<<n-y-ans/w<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/tombraider-shadow/p/11688317.html