Cod sursa(job #1180844)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 1 mai 2014 11:04:56
Problema Invers modular Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <ctime>
#include <cassert>
#include <iomanip>

using namespace std;

const string file = "inversmodular";
const string inputF = file + ".in";
const string outputF = file + ".out";

const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long 
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define base 73

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;

#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end()) 
#define sz(a) int((a).size()) 
#define pb push_back 
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)


int A, N;

LL getphi(LL x) {
	LL cur = x;
	for (LL i = 2; i * i <= x; ++i) {
		if (x % i == 0) {
			while (x % i == 0) {
				x /= i;
			}
			cur = (cur / i) * (i - 1);
		}
	}
	if(x > 1) {
		cur = (cur / x) * (x - 1);
	}
	return cur;
}

int rise(int x, int p) {
	if (p == 0) {
		return 1;
	}
	LL ans = rise(x, p / 2);
	ans *= ans;
	if ((p & 1) == 1) {
		ans = ans * x;
	}  
	return ans % N;
}

int main() {
#ifndef INFOARENA
	freopen("input.txt", "r", stdin);
#else 
	freopen(inputF.c_str(), "r", stdin);
	freopen(outputF.c_str(), "w", stdout);
#endif
	
	scanf("%d %d", &A, &N);
	printf("%d", rise(A, getphi(N) - 1));
	
	return 0;
}