Cod sursa(job #465865)

Utilizator S7012MYPetru Trimbitas S7012MY Data 25 iunie 2010 13:40:08
Problema Ratphu Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2010, gimnaziu si clasa a IX-a, Ziua 1 Marime 1.04 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int cifre[20],nc,low,high,p;
long long cont,n;

int compareints (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}


int numararecifre(long long n) {
	int cont=0;
	while (n) {
		cifre[cont++]=n%10;
		n/=10;
	}
	return cont;
}

void getnb() {
	int i;
	for(i=0; i<nc; i++) low=low*10+cifre[i];
	for(i=nc-1; i>=0; i--) high=high*10+cifre[i];
}

int rezolva(int i) {
	int ccifre[20];
	memcpy(ccifre,cifre,sizeof(cifre));
	int x,*z;
	for( ;i;i/=10) {
		//if(i==n) break;
		x=i%10;
		z=(int*) bsearch(&x,ccifre,nc,sizeof(int),compareints);
		if(!z) return 0;
		ccifre[z-ccifre]=0;
	}
	return 1;
}

int main()
{
	int cont=0,i,lowp,highp;
	freopen("ratphu.in","r",stdin);
	freopen("ratphu.out","w",stdout);
	scanf("%lld %d",&n,&p);
	nc=numararecifre(n);
	sort(cifre,cifre+nc);
	getnb();
	lowp=low/p;
	highp=high/p;
	for(i=lowp; i<=highp; i++)
		cont+=rezolva(p*i);
	printf("%d",cont);
	return 0;
}