530 - Binomial Showdown
Moderator: Board moderators
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia
530 - Judge's bug found!!
This code is AC:
[c]
#include<stdio.h>
long double n,k,sum,c;
void main(){
while(1){
scanf("%Lf %Lf",&n,&k);
if(n==0) break;
if(k>n/2) k=n-k;
if(k<2){
if(k==0) printf("1\n");
else printf("%.0Lf\n",n);
}
else{
if(n<2) printf("1\n");
else{
if(n<37){
if(n==35) printf("%.0Lf\n",n);
else{
for(sum=1,c=k;c>0;c--,n--) sum*=n;
for(;k>1;k--) sum/=k;
printf("%.0Lf\n",sum);
}
}
else{
for(sum=1,c=k;c>0;c--,n--) sum*=n;
for(;k>1;k--) sum/=k;
printf("%.0Lf\n",sum);
}
}
}
}
}
[/c]
Check the bold part, this line mean if the n is 35 then the output will always be 35.

[c]
#include<stdio.h>
long double n,k,sum,c;
void main(){
while(1){
scanf("%Lf %Lf",&n,&k);
if(n==0) break;
if(k>n/2) k=n-k;
if(k<2){
if(k==0) printf("1\n");
else printf("%.0Lf\n",n);
}
else{
if(n<2) printf("1\n");
else{
if(n<37){
if(n==35) printf("%.0Lf\n",n);
else{
for(sum=1,c=k;c>0;c--,n--) sum*=n;
for(;k>1;k--) sum/=k;
printf("%.0Lf\n",sum);
}
}
else{
for(sum=1,c=k;c>0;c--,n--) sum*=n;
for(;k>1;k--) sum/=k;
printf("%.0Lf\n",sum);
}
}
}
}
}
[/c]
Check the bold part, this line mean if the n is 35 then the output will always be 35.


-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
that only means that judge has not such case ... and this is not a bug ...
Best regards
DM
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia
-
- System administrator & Problemsetter
- Posts: 399
- Joined: Sat Jan 12, 2002 2:00 am
Not a bug
Judge data does not contain all possible input. So it is not a bug.
530 pliiiiiiiiiiiiiiiiiiizzzzzzzzzzzzzzzzz help
i used a very simple algorithm for counting nCr. i used array for solving the problem . cant find any problem , but getting wa all the time . pliiiizzzzzzzzzzzzzzz help . here is my code:---------->
plizzzzzzzzzzzzzzzzzz help me
Bye
Riyad
plizzzzzzzzzzzzzzzzzz help me
Bye
Riyad
Last edited by Riyad on Thu Sep 11, 2003 9:17 pm, edited 1 time in total.
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
thanx a lot
used long double instead of long int and got ac . thanx buddy for u help.
Bye
Riyad
Bye
Riyad
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
Hi there!. This is my code:
While running at home I get a right output, but the judge always answers: Runtime Error (SIGSEGV). Does anyone know what happend?.
Code: Select all
#include <stdio.h>
long double Factorial(int f, int s) {
if (f < s) return 1;
else return f*Factorial(f-1, s);
}
main() {
long double n, k;
while ((scanf("%Lf %Lf\n", &n, &k)>0)&&(n>0))
if (k > 0)
printf("%.0Lf\n", Factorial(n, n-k+1)/Factorial(k, 1));
else printf("0\n");
}
530
why ? ... ^^"
Please help me... T____T
[cpp]
#include <iostream>
using namespace std;
int combi(int n,int m) {
long i,p;
for (i=1,p=1;i<=m;i++)
p = p*(n-i+1)/i;
return p;
}
int main() {
long n,m;
while (cin>>n>>m) {
if (m==0&&n==0) break;
if (m==0) cout << 1 << endl;
else cout << combi(n,m) << endl;
}
return 0;
}
[/cpp]
Please help me... T____T
[cpp]
#include <iostream>
using namespace std;
int combi(int n,int m) {
long i,p;
for (i=1,p=1;i<=m;i++)
p = p*(n-i+1)/i;
return p;
}
int main() {
long n,m;
while (cin>>n>>m) {
if (m==0&&n==0) break;
if (m==0) cout << 1 << endl;
else cout << combi(n,m) << endl;
}
return 0;
}
[/cpp]
Thanks for your help ! 

Hi Frostina,
Remember nCr = n! / ( (r!) * (n-r)!);
So you can either run a loop from r+1 to n or
from n-r+1 to n;
Starting from max( r+1, n-r+1) is certainly better.
I changed this part of your code and got it AC in 0.00 seconds.
And in your combi() function why are you using int n, and int m; when they are long. By the way I think you should use long long.
Hope it helps.

Remember nCr = n! / ( (r!) * (n-r)!);
So you can either run a loop from r+1 to n or
from n-r+1 to n;
Starting from max( r+1, n-r+1) is certainly better.
I changed this part of your code and got it AC in 0.00 seconds.
And in your combi() function why are you using int n, and int m; when they are long. By the way I think you should use long long.
Hope it helps.

WA in 0.000 s
Hi,
I'm still new, so the question might be silly- but it's
frustrating anyway.
Recently I switched from Pascal to C, no special reason,
just for fun.
And now I have a problem with a couple of programs.
They all give a wrong answer in 0.000 seconds.
Quote:
"Your program has not solved the problem. It ran during 0.000 seconds."
The main function's always similar, like the one following (Problem 530):
[c]int main(void)
{
long k, n;
unsigned long p;
while (scanf("%ld %ld",&k,&n) == 2)
if ((k > 0) && (n > 0)){
if (k >= n)
p = binom(k,n);
else
p = binom(n,k);
printf("\n%ld",p);
}
return 0;
}[/c]
The programs run ok on my computer!
Do I have a problem with my program, with the e-mail program
or with the OJ?.
Answers are very appreciated!
Thanks in advance.
I'm still new, so the question might be silly- but it's
frustrating anyway.
Recently I switched from Pascal to C, no special reason,
just for fun.
And now I have a problem with a couple of programs.
They all give a wrong answer in 0.000 seconds.
Quote:
"Your program has not solved the problem. It ran during 0.000 seconds."
The main function's always similar, like the one following (Problem 530):
[c]int main(void)
{
long k, n;
unsigned long p;
while (scanf("%ld %ld",&k,&n) == 2)
if ((k > 0) && (n > 0)){
if (k >= n)
p = binom(k,n);
else
p = binom(n,k);
printf("\n%ld",p);
}
return 0;
}[/c]
The programs run ok on my computer!
Do I have a problem with my program, with the e-mail program
or with the OJ?.
Answers are very appreciated!
Thanks in advance.
Re: WA in 0.000 s
It looks like incorrect input handling or incorrect checking for data termination...WR wrote:And now I have a problem with a couple of programs.
They all give a wrong answer in 0.000 seconds.
The main function's always similar, like the one following (Problem 530):
Two observations:WR wrote:[c]int main(void)
{
long k, n;
unsigned long p;
while (scanf("%ld %ld",&k,&n) == 2)
if ((k > 0) && (n > 0)){
[/c]
- -here you discard inputs in the form "n 0" while the problem specification allows for k=0;
-input is terminated by "0 0", you should check for this and exit, although this shouldn't affect your early termination problem.
This check is redundant since prob specification says explicitly that 0<=k<=n.WR wrote:[c] if (k >= n)[/c]
here you put a newline at the very beginning of the output and the last line of output isn't terminated.WR wrote:[c] p = binom(k,n);
else
p = binom(n,k);
printf("\n%ld",p);
[/c]
You don't have judge input data, do you?WR wrote:[c]
}
return 0;
}[/c]
The programs run ok on my computer!

In this case it looks like it's your program at fault... as most of the times the problem lies in my programsWR wrote: Do I have a problem with my program, with the e-mail program
or with the OJ?.

Ciao!!!
Claudio