I think that you should use long double when dividing numbers, because some results might come too big when trying to multiply.
Some data for testing perhaps:
100 6
23 5
34 20
17 9
98 6
88 12
67 21
34 34
12 11
90 6
80 7
70 7
60 8
50 9
45 10
These should be the correct answers:
1192052400
33649
1391975640
24310
1052618392
3730762732
64891431
1
12
622614630
3176716400
1198774720
2558620845
2505433700
3190187286
Try with these numbers and find out what is wrong, but I suggest you to use long double when calculating
while(scanf("%d%d",&n,&m)!=-1) {
int i;
if( n==0 && m==0 ) break;
if(n==m) printf("%d things taken %d at a time is 1 exactly.\n",n,m);
else {
long a,b,i,k;
long *pilha,total=1;
int t=0;
k=n-m;
if(k > m) k=m; /*usa o menor k*/
pilha=(long*) malloc(sizeof(long)*(k+1));
Input :
100 20
100 90
100 85
Your output:
100 things taken 20 at a time is 3820054042 exactly.
100 things taken 90 at a time is 1591253560 exactly.
100 things taken 85 at a time is 1489087776 exactly.
The output should be:
100 things taken 20 at a time is 1027792266232686106 exactly.
100 things taken 90 at a time is 17310309456440 exactly.
100 things taken 85 at a time is 253338471349988640 exactly.
NB : Try to use long long , instead of long, long double, ...
I rewrite my code using another algorithm, but still WA
and I don't know where is the problem ...
[c]
#include <stdio.h>
#include <math.h>
int main() {
unsigned long n,m;
while(fscanf(stdin,"%d%d",&n,&m)==2) {
if( n==0 && m==0 ) break;
if(n==m) fprintf(stdout,"%d things taken %d at a time is 1 exactly.\n",n,m);
else {
unsigned long k=m,i;
long double r=1;
if (k>n-k) k=n-k; /*usa o menor k*/
for(i=1;i<=k;++i) r/=i;
for(i=n;i>=n-k+1;--i) r*=i;
fprintf(stdout,"%d things taken %d at a time is %.0f exactly.\n",n,m,fabs(r));
}
}
return 0;
}
[/c]
You not any problem with your code, but maybe your problem only at your via email. When I see your code, your algo is true and I don't get any mistake at yours. I just copy paste your code, and got AC.
[pascal]
program p369;
var m,n,i:longint;
r:real;
begin
while not eof(input) do begin
readln(m,n);
if (m=0) and (n=0) then exit;
r:=1;
if n>m-n then n:=m-n;
for i:=2 to n do r:=r/i;
for i:=m downto m-n+1 do r:=r*i;
writeln(m,' things taken ',n:0,' at a time is ',round(r):0,' exactly.');
end;
end.
[/pascal]
I think, it should be correct.
Please tell me where the error is.