Pagini recente » Cod sursa (job #1431389) | Cod sursa (job #806836) | Cod sursa (job #776465) | Cod sursa (job #2223752) | Cod sursa (job #465865)
Cod sursa(job #465865)
#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;
}