1244C - The Football Season - CodeForces Solution


brute force math number theory *2000

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, p, w, d = map(int, input().split())
ans = [-1]
l = pow(10, 6) + 5
for y in range(min(n, l) + 1):
    u = p - y * d
    if u < 0:
        break
    if u % w:
        continue
    x = u // w
    z = n - x - y
    if z >= 0:
        ans = [x, y, z]
        break
sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10;
const int mod=1e9+7;
/*int exgcd(int a,int b,int &x,int &y){
	if(b==0){
		x=0,y=1;
		return a;
	}
	int d=exgcd(b,a%b,y,x);
	y-=x*a/b;
	return d;
}*/
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int n,p,w,d;
	cin>>n>>p>>w>>d;
	for(int i=0;i<w;i++){
		if((p-i*d)%w==0&&i+(p-i*d)/w<=n&&p>=i*d){
			int x=(p-i*d)/w,y=i,z=n-x-y;
			cout<<x<<' '<<y<<' '<<z<<'\n';
			return 0;
		}
	}
	cout<<"-1\n";
}


Comments

Submit
0 Comments
More Questions

1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas