Cod sursa(job #484054)

Utilizator bent_larsenSturzu Antonio-Gabriel bent_larsen Data 11 septembrie 2010 20:04:32
Problema Suma divizorilor Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;

const int P=9901;
const int lim=7200;
int primes[lim],cnt=0;

void gcd(int a,int b,int& x,int& y)
{
	if(b==0)
	{	
		x=1;
		y=0;
	}
	else
	{
		gcd(b,a%b,x,y);
		int aux=x;
		x=y;
		y=aux-(a/b)*y;
	}
}

int inv_modular(int a,int p)
{
	int x,y;

	gcd(a,p,x,y);
	if(x<=0)
		x=p+x%p;
	return x;
}

void ciur()
{
	int v[lim];
	memset(v,0,sizeof(v));

	for(int i=2;i<lim;i++)
		if(v[i]==0)
		{		
			for(int j=i+i;j<lim;j+=i)
				v[j]=1;
			primes[cnt++]=i;
		}
}

int pow(int a,int b,int mod)
{
	if(b==1)
		return (a%mod);
	if(b%2==0)
	{
		int x=pow(a,b/2,mod);
		return (x*x)%mod;
	}
	else
	{
		int x=pow(a,b/2,mod);
		return (((x*x)%mod)*(a%mod))%mod;
	}
}

int solve(int A,int B,int mod)
{
	int p=1;

	ciur();
	int putere=0;
	for(int i=0;i<cnt;i++)
	{
		putere=0;
		while(A%primes[i]==0)
		{
			++putere;
			A/=primes[i];
		}
		if(putere>0)
		{
			int pr=putere*B+1;
			p*=((pow(primes[i],pr,mod)-1+mod)*inv_modular(primes[i]-1,mod))%mod;
			p%=mod;
		}
		if(A==1)
			break;
	}
	if(A>1)
	{
		if(A%mod==1)
		{
			p=p*(B+1);
			p%=mod;
		}
		else
		{
			p*=((pow(A,B+1,mod)-1+mod)*inv_modular(A-1,mod))%mod;
			p%=mod;
		}
	}
	return p;
}

int main()
{
	int A,B;
	ifstream in("sumdiv.in");
	in>>A>>B;
	in.close();
	ofstream out("sumdiv.out");
	out<<solve(A,B,P)<<endl;
	out.close();
}