I am not able to solve problem "once upon a time" from this(http://mirror.codeforces.com/gym/100963/attachments) set . I tried extended euclidean algorithm. Can anyone provide the solution (in form of code or editorial) for this problem Or help me debug my code (http://mirror.codeforces.com/gym/100963/attachments) .
Auto comment: topic has been updated by devarshi09 (previous revision, new revision, compare).
https://cp-algorithms.com/algebra/linear-diophantine-equation.html
Just modify the find_all_solutions function.
Exactly where is that function present?
I'm also unable to solve this problem and cant find an editorial on this anywhere.Can someone who solved this please post the solution?
include<bits/stdc++.h>
using namespace std;
define FastIO ios_base::sync_with_stdio(false); cin.tie(0);
define ll long long
define ull unsigned long long
define pb push_back
define All(x) (x).begin(),(x).end()
define mp make_pair
define nl "\n"
typedef pair<int,int>ii; typedef vectorvi; typedef vectorvii; typedef vectorvvi; typedef vectorvl; typedef vector vvl; template void print(T& a) { for(auto x:a) cout<<x<<" "; cout<<"\n"; }
ll gcd(ll a, ll b, ll& x, ll& y) { if(b==0) { x=1; y=0; return a; }
}
bool anys(ll a, ll b, ll& x, ll &y, ll c,ll& g) { g=gcd(a,b,x,y);
}
void ss(ll a, ll b, ll& x, ll& y,ll cnt) { x+=(cnt*b); y+=(cnt*a); }
bool alls(ll a, ll b, ll& x, ll& y, ll c) { ll cnt,g;
}
int main() {
}
in ss when you increase the value of x should you not decrease the value of y?
no, because initially he had multiplied "y" with a -1.so the factor by which we change x and y are now of the same sign.
Thank you man ...got it
I just solved this problem, here is my solution.
I know that this is a very old post, but i would like to help other people like me that also want to better understand the solution to this problem.
To simple began the solution, its good to observe that we want to find x and y with some constraints that solve this equation :
$$$k+a*x=n+m*y$$$
$$$x \geq 1, x \in Z$$$
$$$y \geq 0, y \in Z$$$
x must be greater or equal than 1 because when x equals to 0, the man approaches and not stretches, and x < 0 does not make sense.
y must be greater or equal than 0 because when y equals to 0, the woman already stretches, and y < 0 does not make sense.
Also observe that we can write the above equation in another way:
$$$a*x-m*y=n-k$$$
And also:
$$$a*x+(-m)*y=n-k$$$
And this type of equation can be solved simply using the extend euclidean algorithm, see the link.
Observe that we don't have answer if n-k is not a multiple of $$$gcd(a,-m)$$$.
Unfortunately, sometimes x, and y could be negative, but observe that:
$$$0 = a*\frac{-m}{gcd(a,-m)}+(-m)*\frac{a}{gcd(a,-m)}$$$
And use that to change x and y without affect the answer.
See my code below
Sr but can I don't know why do we have : ll d = m/g; and then how we find x by: if(x <= 0){ ll mult = (abs(x)/d)+1; x += d*mult; }else{ ll mult = (x-1)/d; x -= d*mult; } Can you explain more, thank you so much
As mentioned here: Getting all solutions of linear Diophantine Equation, to get all solutions of x when you have found x0 by using extended Euclidean, then x = x0 + k * b / g, and the d = m / g which he wrote actually is the b / g part in this formula. Since we want x >= 1 (as he explained above), and x is minimum (since the problem required the first time that the man and the woman stretch each other), we need to find the propriate k for it. The variable mult his declared is that k;
That's all. Also, I have a simpler implementation for this part:
I am getting WA on test case 2 Can someone help me in which case my code is getting wrong My Approach: get initial solutions to equation mx+ay=k+a-n and then shift the solutions using x'=x+kg/a and y=y-kg/m to get both x and y greater than zero
code: 174696154
lost a day on it because using it instead of long long on some variables.