Cod sursa(job #1376912)

Utilizator corul_barbatescUNIBUC Kira96 lockmihai corul_barbatesc Data 5 martie 2015 19:23:34
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
/*
    Those without power, seek us!
    Those with power, fear us!
    We are the Order of the Black Knights!
*/

#include<fstream>
#include<cstdio>
#include<map>
#include<set>
#define FIT(a,b) for(vector<int >::iterator a=b.begin();a!=b.end();a++)
#define FITP(a,b) for(vector<pair<int,int> >::iterator a=b.begin();a!=b.end();a++)
#define RIT(a,b) for(vector<int>::reverse_iterator a=b.end();a!=b.begin();++a)
#include<stack>
#define ROF(a,b,c) for(int a=b;a>=c;--a)
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#define REP(a,b) for(register int a=0;a<b;++a)
#include<cstring>
#include<ctime>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<set>
#define f cin
#define g cout
#include<queue>
#define debug cerr<<"OK";
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned int
#define mod 1000000009LL
#define SQR 350
#define inf 1<<30
#define div fdasfasd
#define hash dsafdsfds
#define od 100003
#define mod  1999999973LL
#define DIM 60010000
#define base 256
#define bas 255
#define N 100100
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
/*
    int dx[]={0,0,0,1,-1};
    int dy[]={0,1,-1,0,0};
*/
ll a,n,mo,phi;
ll po(ll a,ll b)
{
    ll sol=1;
    while(b)
    {
        if(b&1)
            sol=sol*a%mo;
        a=a*a%mo;
        b>>=1;
    }
    return sol;
}
int main ()
{
    f>>a>>n;
    mo=n;
    phi=n;
    FOR(i,2,n)
    {
        if(1LL*i*i>n)
            break;
        if(n%i==0)
        {
          phi/=i;
          phi*=(i-1);
          while(n%i==0)
                n/=i;
        }
    }
    if(n!=1)
        phi/=n,phi*=(n-1);
    phi--;
    g<<po(a,phi);
    return 0;
}