Cod sursa(job #2703654)

Utilizator gasparrobert95Gaspar Robert Andrei gasparrobert95 Data 8 februarie 2021 21:29:20
Problema Pascal Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("pascal.in");
ofstream fout("pascal.out");
ll n, d, rez;

int main() {
    fin >> n >> d;
    if (d > n) {
        fout << 0;
        return 0;
    }
    ll put = 0, putd2 = 0, d2 = d;
    if (d == 4)
        d = 2, d2 = 2;
    if (d == 6)
        d = 2, d2 = 3;
    ll aux = d;
    while (aux <= n) {
        put += n / aux;
        aux *= d;
    }
    aux = d2;
    while (aux <= n) {
        putd2 += n / aux;
        aux *= d2;
    }
    for (int j = 0; j <= n / 2; ++j) {
        aux = d;
        ll put2 = 0, put2d2 = 0;
        while (aux <= n - j) {
            put2 += (n - j) / aux;
            if (aux <= j)
                put2 += j / aux;
            aux *= d;
        }
        aux = d2;
        while (aux <= n - j) {
            put2d2 += (n - j) / aux;
            if (aux <= j)
                put2d2 += j / aux;
            aux *= d2;
        }
        if ((n != 4 && n != 6 && put - put2 > 0) || ((n == 4 || n == 6) && min(put - put2, putd2 - put2d2) > 0))
            ++rez;
    }
    rez *= 2;
    if (n % 2 == 0)
        --rez;
    fout << rez;
    return 0;
}