Cod sursa(job #2931039)

Utilizator biancaivascuBianca Ivascu biancaivascu Data 30 octombrie 2022 13:28:52
Problema Suma divizorilor Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <iostream>
#include <fstream>

using namespace std;

#define Nr 9901

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

int putere(int a, int n)
{
	a=a%Nr;
	int rez=1;
	while(n>0)
	{
		if(n%2==1)
		{
			rez=(rez*a)%Nr;
		}
		a=(a*a)%Nr;
		n=n/2;
	}
	return rez;
}
int main()
{
    ifstream in("sumdiv.in");
    ofstream out("sumdiv.out");
    int a, b, exp=0, d, s=1, x, y, rez, cd;
    in>>a>>b;
    if(a==1) out<<1;
    else
    {
        if(b==0) out<<1;
        else
        {
            d=2;
            while(d*d<=a)
            {
                while(a%d==0)
                {
                    a=a/d;
                    exp++;
                }
                if(exp>0)
                {
                    exp*=b;
                    exp+=1;
                    rez=putere(d, exp)-1;
                    if(rez<1) rez+=Nr;

                    euclid(d-1, Nr, x, y);
                    if(x<0) x+=Nr;

                    s=(s*(rez*x)%Nr)%Nr;
                }
                exp=0;
                d++;
            }

            if(a>1)
            {
                exp=b+1;

                rez=putere(a, exp)-1;
                if(rez<1) rez+=Nr;

                euclid(a-1, Nr, x, y);
                if(x<0) x+=Nr;

                s=(s*(rez*x)%Nr)%Nr;
            }

            out<<s;
        }

    }
    return 0;
}