Cod sursa(job #476694)

Utilizator proxenetuNea Caisa proxenetu Data 12 august 2010 01:57:44
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <stdio.h>
#include <bitset>

using namespace std;

#define maxN    1000100

bitset <maxN> Prim;
int N, exp[maxN], tot[maxN];

int pow (int number, int exp) {
    if (exp == 0)
        return 1;
    if (exp == 1)
        return number;

    int x = pow(number, exp / 2);
    x *= x;

    return exp %  2 == 1 ? x * number : x;
}

int main () {
    int i, x, j, k, product;
    long long sum = 0;

    freopen("fractii.in", "r", stdin);
    freopen("fractii.out", "w", stdout);

    scanf("%d", &N);
    Prim.set();

    tot[1] = 1;

    for (i = 2; i <= N; i += 2) {
        Prim[i] = false;
        if (i % 4 == 0)
            tot[i] = tot[i / 2] * 2;
        else
            tot[i] = 1;
    }

    for (i = 3; i <= N; ++ i)
        if (Prim[i]) {
            for (j = i; j <= N; j += i) {
                if (1LL * j % (1LL * i * i) == 0)
                    tot[j] = tot[j / i] * i;
                else
                    tot[j] = tot[j / i] * (i - 1);
                Prim[j] = false;
            }
        }

    for (i = 2; i <= N; ++ i)
        sum += tot[i];

    printf("%lld\n", 2LL * sum + 1);
}