Cod sursa(job #1404438)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 28 martie 2015 10:43:30
Problema Sum Scor 85
Compilator c Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>
#include <ctype.h>
#define MAXK 6
#define MAXM 266400
#define MAX 100000
#define MAXC 18
int c[MAX+1], val[MAXM+1], next[MAXM+1], lista[MAX+1], d[MAXK], cif[MAXC];
FILE *fin;
inline void shmenPetrescu(){
    int i, j, m;
    m=0;
    for(i=2; i<=MAX; i++){
        if(c[i]==0){
            for(j=i; j<=MAX; j+=i){
                c[j]=1;
                val[++m]=i;
                next[m]=lista[j];
                lista[j]=m;
            }
        }
    }
    //printf("%d\n", m);
}
inline long long gauss(long long x){
    return x*(x+1)/2LL;
}
inline int citeste(){
    int x=0;
    char ch=fgetc(fin);
    while(!isdigit(ch)){
        ch=fgetc(fin);
    }
    while(isdigit(ch)){
        x=x*10+ch-'0';
        ch=fgetc(fin);
    }
    return x;
}
int main(){
    int n, q, k, p, e, prod, i, j;
    long long sum;
    FILE *fout;
    fin=fopen("sum.in", "r");
    fout=fopen("sum.out", "w");
    shmenPetrescu();
    fscanf(fin, "%d", &q);
    while(q>0){
        q--;
        n=citeste();
        k=0;
        p=lista[n];
        while(p!=0){
            d[k++]=val[p];
            p=next[p];
        }
        sum=gauss(2*n);
        for(i=1; i<(1<<k); i++){
            prod=1;
            e=0;
            for(j=0; j<k; j++){
                if(i&(1<<j)){
                    e++;
                    prod*=d[j];
                }
            }
            if(e%2==0){
                sum+=1LL*prod*gauss(2*n/prod);
            }else{
                sum-=1LL*prod*gauss(2*n/prod);
            }
        }
        k=0;
        while(sum>0){
            cif[k++]=sum%10LL;
            sum/=10LL;
        }
        while(k>0){
            k--;
            fputc(cif[k]+'0', fout);
        }
        fputc('\n', fout);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}