Pagini recente » Cod sursa (job #2710048) | Cod sursa (job #1150317) | Cod sursa (job #1621872) | Cod sursa (job #2597845) | Cod sursa (job #3223968)
#include <fstream>
using namespace std;
ifstream in("pascal.in");
ofstream out("pascal.out");
int n, d, ans;
int fact[2][5000005];
//fact[i] = cati d avem in descompunere lui factorial[i]
//daca d e 6 trb sa numar si cati sunt de 2 si de 3
int main()
{
in>>n>>d;
int x;
for(int i = 2; i<=n; i++)
{
fact[0][i] = fact[0][i - 1];
fact[1][i] = fact[1][i - 1];
if(d == 4)
{
x = i;
while(x % 2 == 0)
{
fact[0][i]++;
x /= 2;
}
}
else if(d == 6)
{
x = i;
while(x % 2 == 0)
{
fact[0][i]++;
x /= 2;
}
while(x % 3 == 0)
{
fact[1][i]++;
x /= 3;
}
}
else
{
x = i;
while(x % d == 0)
{
fact[0][i]++;
x /= d;
}
}
}
if(d == 4)
{
int nr;
for(int j = 0; j<=n; j++)
{
nr = fact[0][n] - fact[0][j] - fact[0][n - j];
nr /= 2;
if(nr > 0)
{
ans++;
//out<<j<<" -> "<<nr<<'\n';
}
}
}
else if(d == 6)
{
int nr2, nr3;
for(int j = 0; j<=n; j++)
{
nr2 = fact[0][n] - fact[0][j] - fact[0][n - j];
nr3 = fact[1][n] - fact[1][j] - fact[1][n - j];
if(nr2 > 0 && nr3 > 0)
{
ans++;
//out<<j<<" -> "<<nr<<'\n';
}
}
}
else
{
int nr;
for(int j = 0; j<=n; j++)
{
nr = fact[0][n] - fact[0][j] - fact[0][n - j];
if(nr > 0)
{
ans++;
//out<<j<<" -> "<<nr<<'\n';
}
}
}
out<<ans;
return 0;
}