Here is my code, I have made an extended euclidian function, but Testcases #2, #4 and #5 still fails
#include<iostream>
using namespace std;
#define ll long long
#define MOD 1000000007
ll ex, ey,ed;
void ExtendedEuclid(ll a, ll b){
if(b==0){
ex=1; ey=0; ed=a;
}
else{
ExtendedEuclid(b, a%b);
ll temp = ex;
ex = ey;
ey = temp-((a/b)*ey);
}
}
int main(){
ll n;
cin>>n;
ExtendedEuclid(n,MOD);
cout<<ex<<endl;
}
Also I am not able to clearly comprehend the implementation, As I know that Extended Euclidean is used to find out the solution of equations of the form Ax+By = C where C is a multiple of divisor of A and B, so we calculate x and y from Ext. Euclid, now what I am able to understand is we are passing B as MOD and A as n and C as 1 so it becomes
nx + MODy = 1,
hence nx = 1 - MODy
so x = (1/n)*(1-MODy)
so why is x giving us the solution of inverse of n?
Also, will this code work every-time as it is known that for this to work as GCD(n,MOD) must always be 1, and it might not be true for some cases?