Cod sursa(job #268452)

Utilizator AndreyPAndrei Poenaru AndreyP Data 1 martie 2009 11:58:31
Problema Invers modular Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include<stdio.h>
int a,n,t,M;
inline void totient()
{
	t=n;
	if(!(n&1))
	{
		t>>=1;
		while(!(n&1))
			n>>=1;
	}
	for(int i=3; i*i<=n; i+=2)
	{
		if(!(n%i))
		{
			t=t/i*(i-1);
			while(!(n%i))
				n/=i;
		}
	}
	if(n!=1)
		t=t/n*(n-1);
}
int main()
{
	freopen("inversmodular.in","r",stdin);
	freopen("inversmodular.out","w",stdout);
	scanf("%d%d",&a,&n);
	M=n;
	totient();
	--t;
	int rez=1;
	for(; t; t>>=1)
	{
		if(t&1)
			rez=(a*rez)%M;
		a=(a*a)%M;
	}
	printf("%d\n",rez);
	return 0;
}