Cod sursa(job #1844685)

Utilizator Ruxandra985Nanu Ruxandra Laura Ruxandra985 Data 10 ianuarie 2017 12:18:51
Problema Pascal Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <cstdio>
#include <iostream>
using namespace std;
int v[5000002],w[5000002];
int main()
{
    FILE *fin=fopen ("pascal.in","r");
    FILE *fout=fopen ("pascal.out","w");
    int r,d,sol,i,nr,ok,doi,trei;
    fscanf (fin,"%d%d",&r,&d);
    sol=0;
    if (d==2 || d==3 || d==5){
        for (i=1;i<=r+1;i++){
            nr=i;
            while (nr%d==0){
                v[i]++;
                nr/=d;
            }
            //printf ("%d ",v[i]);
            v[i]+=v[i-1];
        }
    }
    else if (d==4){
        ok=0;
        for (i=1;i<=r+1;i++){
            nr=i;
            while (nr%2==0){
                v[i]++;
                nr/=2;
            }
            if (v[i]%2==1){
                if (ok==1){
                    ok=0;
                    v[i]++;
                }
                else ok=1;
            }
            v[i]+=v[i-1];
        }
    }
    else {
        doi=0;
        trei=0;
        for (i=1;i<=r+1;i++){
            nr=i;
            while (nr%2==0){
                doi++;
                nr/=2;
            }
            while (nr%3==0){
                trei++;
                nr/=3;
            }
            v[i]=min(doi,trei);
            if (doi<trei){
                trei-=doi;
                doi=0;
            }
            else {
                doi-=trei;
                trei=0;
            }
            v[i]+=v[i-1];
        }
    }
    for (i=0;i<=r;i++){
        if (d==4 && v[r]-v[r-i]-v[i]>1)
            sol++;
        else if (d!=4 && v[r]-v[r-i]-v[i]>0)
            sol++;
    }
    fprintf (fout,"%d",sol);
    return 0;
}