The Football Season【Codeforces 1244 C】【数学】

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41730082/article/details/102538531

Codeforces Round #592 (Div. 2) C


  就是解决两个方程,

然后,结束HACK之后,只有不到一半的(原来A的)人过了这道题。

  具体错在哪了呢?就是因为没有判断此时的x和y是否是非负数,毕竟胜负或者是平局都不可能是负数的。

  具体思路呢:

  我们,首先可以假定最大的x,也就是p / w,但是我们不能保证此时的y是个整数,那么怎么办呢?我们可以不断的给x减去1,看看是不是合法了,如果合法了呢,那么此时的一定就是最优解!直接输出,否则,如果一直到搜完都没找到就是“-1”了。

  但是,我们需要搜索几次呢?搜索d次就可以了,因为d个中一定有一个是整除的,我们就可以保证这个了。

为什么搜索d次:因为在这里可以推导一下,y * d = p - w * x,那么,我们现在想得到一个合法的y是不是要让p - w * x是一个d的倍数?那么假如它现在不是d的倍数,我们是不是可以对x不断的减去,那么(p - w * x) % d的值是不是想要让它为0。而每次的变化量实际上就是w,gcd(w, d)一定是小于d的,并且gcd(w, d)乘以0~d中,一定有一个能被d给整除的。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MIN3(a, b, c) min(min(a, b), c)
#define MAX3(a, b, c) max(a, max(b, c))
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
ll n, p, w, d;
int main()
{
    scanf("%lld%lld%lld%lld", &n, &p, &w, &d);
    if(p / w > n)
    {
        cout<<-1<<endl;
        return 0;
    }
    ll t1=p / w;
    ll t2=(p - t1 * w) / d;;
    if((p - t1 * w) % d == 0)
    {
        cout<<t1<<" "<<t2<<" "<<n - t1 - t2<<endl;
        return 0;
    }
    for(int i=1; i<=d; i++)
    {
        ll tt1 = t1 - i;
        ll tt2 = (p - tt1 * w) / d;
        if((p - tt1 * w) % d == 0 && tt1 + tt2 <= n && tt1 >= 0 && tt2 >= 0)
        {
            cout<<tt1<<" "<<tt2<<" "<<n-tt1-tt2<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/102538531