Cod sursa(job #1782524)

Utilizator giotoPopescu Ioan gioto Data 18 octombrie 2016 11:23:06
Problema Pascal Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
using namespace std;

short a[5000000];
int n, d;
inline int Legendre(int X, int IMP){
    int Sol = 0;
    long long L = IMP;
    while(L <= X){
        Sol = Sol + X / L;
        L *= (long long) IMP;
    }
    return Sol;
}
int main()
{
    freopen("pascal.in", "r", stdin);
    freopen("pascal.out", "w", stdout);
    scanf("%d%d", &n, &d);
    a[0] = 1;
    int Sol = 0;
    int A2 = Legendre(n, 2), A3 = Legendre(n, 3);
    for(int j = 1; j <= n / 2 ; ++j){
        int D2 = A2 - Legendre(n - j, 2) - Legendre(j, 2);
        int D3 = A3 - Legendre(n - j, 3) - Legendre(j, 3);
        if(d == 2){
            if(D2 > 0) ++Sol;
            continue ;
        }
        if(d == 3){
            if(D3 > 0) ++Sol;
            continue ;
        }
        if(d == 6){
            if(D2 && D3) ++ Sol;
            continue ;
        }
    }
    if(n % 2 == 1)
    printf("%d", Sol * 2 );
    else
    printf("%d", Sol * 2 - 1);
}