Give 2 numbers m and n (m <n), and M and n are interacting, find a number K to meet 0 <n n and k * m % n = 1, if there are multiple metal conditions that meet the conditions, , The smallest output.
Input input 2 numbers m, n intermediate space separation (1 <= m <n <= 10^9) output an output of a number K to meet 0 <k <n and k * m % n = 1, if there is any Those who meet the conditions are the smallest. Sample input
2 3
Sample Output
2
The method of expanding Europe by expanded Europe below
#include <iOSTREAM>
#include <cstdio>
#include <iOSTREAM>
using namespace std;
TypeDef Long Long ll;
void ex_gcd (int a, int b, int & x, int & y, int & d) {
if (! B)
{{
d = a;
x = 1,
y = 0;
}
else {
ex_gcd (b, a % b, y, x, d); // must be converted here; recursive ideas
y = y-x * (a / b);
}
}
int INV (int T, int P) {// If it does not exist, return -1
int D, x, y;
ex_gcd (t, p, x, y, d);
Return d == 1? (x % p + p) % P: -1;
}
int Main () {
int a, p;
cin >> a >> p;
COUT << Inv (a, P) << Endl;
}
#include <iOSTREAM>
using namespace std;
Int E (int a, int m, int & x, int & y) // must be taken here. This is also my idea of solving unknown in programming language. Get arrives.
{{
if (! m)
{{
x = 1;
y = 0;
Return a;
}
int d = e (m, a%m, x, y);
int T = x;
x = y;
y = t- (a/m)*y;
Return d;
}
int ANS (int M, int N)
{{
int x, y, d;
d = e (m, n, x, y);
if (d == 1)
{{
While (x <= 0) // Use Return (x%n+n)%n here is to solve the negative problem, and it may be the key to the minimum value;
x+= n;
return x;
}
else
return -1;
}
int main()
{
int n,m;
cin>>m>>n;
cout<<ans(m,n)<<endl;
}
This is the solution of Ferma’s theorem, but it should be noted that the number of Fema Xiao Ding’s theorem requires the number of MODs. Therefore, the Giola function is required.
#include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
typeDef long long ll; // This method is easy to super the boundary, use longlong
ll pow_mod(ll a,ll p2,ll p)
{
ll ans=1;
while(p2)
{
if(p2&1)
ans=(ans*a)%p;
p2>>=1;
a=(a*a)%p;
}
return ans;
}
ll oula(ll a)
{
ll ans=a;
for(ll i=2;i*i<=a;i++)
if(a%i==0)
{
ans=(ans/i)*(i-1);
while(a%i==0)
a/=i;
}
if(a>1)
ans=(ans/a)*(a-1);
return ans;
}
int main()
{
ll m,n;
cin>>m>>n;
ll k=oula(n);
Cout << POW_MOD (m, K-1, n)%n << endl; // here to mod, n Guaranteed results are the smallest
return 0;
}
#include <iOSTREAM>
#include <cstdio>
#include <iOSTREAM>
using namespace std;
TypeDef Long Long ll;
Ll d;
int EX_GCD (int a, int b, int & x, int & y) {
if (! B)
{{
// d = a;
x = 1,
y = 0;
Return a;
}
else {
int TX;
int Ty;
d = ex_gcd (b, a % b, tx, ty); // It must be converted here; recursive ideas
x = Ty;
y = tx- (a/b)*ty;
}
Return d;
}
int INV (int T, int P) {// If it does not exist, return -1
int x, y;
// int d;
ex_gcd (t, p, x, y);
Return d == 1? (x % p + p) % P: -1;
}
int Main () {
int a, p;
cin >> a >> p;
COUT << Inv (a, P) << Endl;
Return 0;
}