Codeforces Round #592 (Div. 2) C题

按照公式求值,可以枚举y = i从0到w-1的值,因为如果y的值大于等于w的值的话,y可以等i-w,x可以等于求的值加上d,所以暴力枚举就行了

int main()
{
    ll n,p,w,d,x,y,z;
    cin >> n >> p >> w >> d;
    int flag = 0;
    for(int i = 0; i < w; i++) {
        if((p-(d*i))%w == 0 && (p-(d*i)) / w + i <= n && p >= d*i) {
            x = (p-(d*i))/w;
            y = i;
            z = n - x - y;
            flag = 1;
            break;
        }
    }
    if(flag) cout << x << ' ' << y << ' ' << z << endl;
    else cout << -1 << endl;
    return 0;
}

好假一男的

猜你喜欢

转载自www.cnblogs.com/ASLHZXY/p/12117187.html