Cod sursa(job #1939014)

Utilizator werz66Nagy Peter werz66 Data 25 martie 2017 13:21:12
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream be("fractii.in");
    ofstream ki("fractii.out");
    long n;
    be >> n;
    long long result = 0;
    long a[500001]; a[1] = n; a[2] = n - n/2; a[3] = n - n/3;
    for(long i =4;i<=n;i++) {
        long j = 2;
        bool c = false;
        while((j<=i/2) && (c==false)) {
            if((i % j)==0) {
                c = true;
                long x = i/j;
                if((x % j)==0) {
                    a[i] = a[x];
                }
                else {
                    a[i] = n - ((n-a[x]) + (n-a[j]) - n/i);
                }
            }
            j++;
        }
        if((j>(i/2)) && (c==false)) {
            a[i] = n - n/i;
        }
    }

    for(long i =1;i<=n;i++) {
        result = result + a[i];
    }

    ki << result;
    return 0;
}