Program to test for divisibility of numbers (EDIT: here is the follow up qusetion)

Using this program to test for the smallest number where its permutations or itself is divisible by 10 or less numbers is twice as slow as the fastest program I found doing this (I don't have the source code). How can this program be twice as slow, what optimizations can I do without messing with the compiler flags?

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#define MAX 1000000

int main() {
  int N,p[10],n[100];
  int mask[MAX];
  register int i, j, k;

  scanf("%d", &amp;N);
  for(i=0;i&lt;N;i++) scanf("%d", &amp;n[i]);
  memset(mask,0,sizeof(mask));
  i = MAX+1;
  while(--i) {
    k=i;
    p[0]=0;
    p[1]=0;
    p[2]=0;
    p[3]=0;
    p[4]=0;
    p[5]=0;
    p[6]=0;
    p[7]=0;
    p[8]=0;
    p[9]=0;
    while (k&gt;0) {p[k%10]++; k/=10;}

    for(j=1;j&lt;10;j++) if(p[j]&gt;0) {k=j; p[j]--; break;}
    for(j=0;j&lt;10;j++) while (p[j]&gt;0) {k=10*k+j; p[j]--;}
    for(j=0;j&lt;N;j++) if(i%n[j]==0) mask[k]|=(1&lt;&lt;j);
  }
  for(i=1;i&lt;MAX;i++) if(mask[i]==(1&lt;&lt;N)-1) break;
  fprintf(stdout, "%d\n", i);
}


This program uses bitmasks to test all numbers between 1 and MAX for divisibility with the given numbers and then prints out the smallest number whose permutations or itself is divisible by all given numbers. It is not fast enough!

Example input

7
164 278 293 382 483 598 23


This will test all numbers and check which ones are divisible by the given numbers and the output should be 102246.

It is compiled using the GCC compiler with the following flags:

-g -O2 -std=gnu99 -static -lm


Test the code here.
for loop

i = MAX+1;
while(--i) {


This should be converted to a for loop, it will become more readable.

Smallest scope possible

Maybe in this challenge C99 is not an option, but I would like to reccomend declaring variables in loop declarations, like:

for(int i=1;i&lt;MAX;i++) 


Avoiding repetition

Please, avoid repetion:

p[0]=0;
p[1]=0;
p[2]=0;
p[3]=0;
p[4]=0;
p[5]=0;
p[6]=0;
p[7]=0;
p[8]=0;
p[9]=0;


Should be:

for (int i = 0; i &lt;= 9; i++) {
    p[i] = 0;
}


and it becomes so much clearer (think what would have happened if you had to set 100 of such items).