Cod sursa(job #1017230)

Utilizator impulseBagu Alexandru impulse Data 27 octombrie 2013 15:54:56
Problema Suma si numarul divizorilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <map>
#include <math.h>

#define MAXC 2000000
using namespace std;
typedef unsigned long long int ULONG;
typedef map<int,int> dict;
typedef map<int,int>::iterator it;

int ciur(int *n, int l)
{
    n[0] = n[1] = 1;
    for(int i = 2; i < l; i++)
        if(n[i] == 0)
            for(int j = i + i; j < l; j+=i)
                n[j] = 1;
    return 0;
}

int build(ULONG n, int* C, dict* b)
{
    if(n <= 1) return 0;
    for(int i = 2; i < MAXC; i++)
    {
        if(C[i] == 0)
        {
            if(n % i == 0)
            {
                (*b)[i]++;
                build(n/i, C, b);
                break;
            }
        }
    }
    return 0;
}

int compute(dict d, int *div, ULONG *sum)
{
    *div = 1;
    *sum = 1;
    it a = d.begin();
    it b = d.end();
    do
    {
        *div *= a->second + 1;
        ULONG x = (ULONG)pow(a->first, a->second + 1) - 1;
        ULONG y = a->first - 1;
        *sum *= (x / y);
        a++;
    }
    while(a != b);
    *sum %= 9973;
    return 0;
}

int main()
{
    int t;
    ifstream fin("ssnd.in");
    ofstream fout("ssnd.out");
    fin>>t;
    ULONG *A = new ULONG[t];
    for(int i = 0; i < t; i++)
        fin>>A[i];
    int* C = new int[MAXC];
    for(int i = 0; i < MAXC; i++) C[i] = 0;
    ciur(C, MAXC);
    dict B;
    int d = 0;
    ULONG s = 0;
    for(int i = 0; i < t; i++)
    {
        B.clear();
        build(A[i], C, &B);
        compute(B, &d, &s);
        fout<<d<<" "<<s<<"\n";
    }
    fout.close();
    return 0;
}