1. solving the inverse number of combinations
Target: obtaining C (n, m)% p where p is a prime number!
Methods: Fermat's little theorem inverse yuan
Because of the nature of the film is not suitable for the division, such as (a / b)% c;
However, when we know b% c inverse element d, the problem can be transformed into: (a * d)% c = ((a% c) * (b% c))% c;
Consider Fermat's little theorem:
a^p-1=1(mod p)
Obviously there are:
a*a^p-2=1(mod p)
Then a ^ p-2 is the inverse of the film at a p significance
You can find the use of fast power!
Then we need a pretreatment m!, (Nm) !, respectively determined their inverse, then with n! Modulo multiplication and can be!
2.Number common formula:
C(n,m)=C(n,n-m);
C(n,m)=C(n-1,m-1)+C(n-1,m);
Thus, all the number of combinations can be achieved linear recurrence!
Also note that pre-ah!
C(0,i)=C(1,1)=1;
C(0,0)=0;
P2822 combination of several issues
printf("%d\n",ans[n][m]);
}
return 0;
}