Cod sursa(job #1820667)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 2 decembrie 2016 00:34:20
Problema Suma si numarul divizorilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <iostream>
#include <fstream>
#include <vector>

#define mod 9973

using namespace std;

ifstream f ("ssnd.in");
ofstream t ("ssnd.out");

int n,q,mx=0;
vector <int64_t> v;
vector <int> prim;

int64_t pow(int64_t x,int64_t y,int64_t n)
{
    int64_t m=1;
    while(y)
    {
        if(y&1)
        {
            m=(m*x)%n;
        }
        x=(x*x)%n;
        y>>=1;
    }

    return m;
}

void read(){
    int64_t aux;
    f>>q;
    for (int i=0;i<q;++i){
        f>>aux;
        v.push_back(aux);
        if(aux>mx)
            mx=aux;
    }
}

void ciuruieste(){
    bool prime[mx/2+1];
    for (int i=2;i<=(mx/2);++i)
        if (!prime[i]){
                for (int j=2;j<=(mx/2)/i;++j)
                    prime[i*j]=true;
        prim.push_back(i);
        }
}

vector < pair<int,int> > decompose(int64_t target){
    vector < pair<int,int> > aux;
    int how_many,target1=target;
    for (auto i:prim)
        if (target%i==0){how_many=0;
            while (target%i==0){target/=i;++how_many;}
        aux.push_back({i,how_many});
        target=target1;
    }
    return aux;
}

void solve(int64_t target){
    vector < pair<int,int> > w=decompose(target);
    int64_t how_many=1,sum=1;
    for (auto i:w){
        how_many*=(1+i.second);
        sum*=(pow(i.first,i.second+1,mod)-1)/(i.first-1);
    }
    if (target!=1){
        if (how_many==1)
            ++how_many;
        if (sum==1)
            sum+=target;}
    t<<how_many<<" "<<sum<<'\n';
    }

int main()
{
    read();
    ciuruieste();
    for (auto i:v)
        solve(i);
    return 0;
}