Cod sursa(job #31764)

Utilizator wefgefAndrei Grigorean wefgef Data 16 martie 2007 15:11:13
Problema Zero 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#define ll long long

#define Dmax 1000

int N, B;
int fact[Dmax], put[Dmax], nrf;
ll put2[Dmax];
ll rez;

int main()
{
	freopen("zero2.in", "r", stdin);
	freopen("zero2.out", "w", stdout);
	
	int stp, i, j;
	int aux, cnt;
	ll nr, a1, r;
	ll best;
	
	for (stp = 0; stp < 10; ++stp)
	{	
		scanf("%d %d", &N, &B);
		
		aux = B;
		nrf = 0;
		for (i = 2; i*i <= aux; ++i) if (aux % i == 0)
		{
			for (cnt = 0; aux % i == 0; aux /= i, ++cnt)
			fact[++nrf] = i;
			put[nrf] = cnt;
		}
		if (aux > 1) fact[++nrf] = aux, put[nrf] = 1;
		
		memset(put2, 0, sizeof(put2));
		best = 0
		for (i = 1; i <= nrf; ++i)
		{
			for (j = fact[i]; j <= N; j *= fact[i])
			{
				r = j;
				a1 = (N-j+1)%j;
				nr = (N-j+1)/r + 1;
				
				put2[i] += a1*nr + r*nr*(nr-1)/2;
			}
			if (best == 0 || put2[i]/(ll)put[i] < best)
			best = put2[i]/(ll)put[i];
		}
		
		printf("%lld\n", best);
	}
	
	return 0;
}