Cod sursa(job #2230660)

Utilizator giotoPopescu Ioan gioto Data 10 august 2018 23:14:36
Problema Zero 2 Scor 7
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;

inline long long legendre(int x, int y){
    long long p = y, Sol = 0;
    while(p <= x) {
        int r = x / p;
        Sol = Sol + 1LL * p * (1LL * r * (r + 1) / 2);
        int rest = (x + 1) % p;
        if(rest > 0) Sol = Sol - 1LL * r * (p - rest);
        p = p * y;
    }
    return Sol;
}
inline long long solve(int x, int y){
    long long Sol = 0;
    if(y == 4) Sol = legendre(x, 2) / 2;
    else if(y == 9) Sol = legendre(x, 3) / 2;
    else if(y == 8) Sol = legendre(x, 2) / 3;
    else if(y == 6) Sol = legendre(x, 3);
    else if(y == 10) Sol = legendre(x, 5);
    else Sol = legendre(x, y);
    return Sol;
}
int main()
{
    freopen("zero2.in", "r", stdin);
    freopen("zero2.out", "w", stdout);

    int t = 10, Sol = 0;
    while(t--){
        int x, y;
        scanf("%d%d", &x, &y);
        printf("%lld\n", solve(x, y));
    }

    return 0;
}