530 - Binomial Showdown
Moderator: Board moderators
530 - Binomial Showdown
Can anybody offer any hints to solving problem #530 - Binomial Showdown? My first attempt at solving this problem resulted in an exceeded time limit. Any tips?
Here is the code I submitted. It gives me the right answers to the sample input on my computer, but I get Time Limit Exceeded when I submit it for judging. Any input would be appreciated:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main()
{
int num1, num2, i;
long double numerator, denominator, ans;
int n_minus_r;
#ifndef ONLINE_JUDGE
close (0); open ("input.in", O_RDONLY);
close (1); open ("output.out", O_WRONLY | O_CREAT, 0600);
#endif
while(scanf("%d %d", &num1, &num2) == 2)
{
numerator = 1;
denominator = 1;
n_minus_r = num1 - num2;
for(i=num1; i > n_minus_r; i--)
numerator = numerator * i;
for(i=num2; i > 1; i--)
denominator = denominator * i;
ans = numerator / denominator;
printf("%.fn", ans);
}
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main()
{
int num1, num2, i;
long double numerator, denominator, ans;
int n_minus_r;
#ifndef ONLINE_JUDGE
close (0); open ("input.in", O_RDONLY);
close (1); open ("output.out", O_WRONLY | O_CREAT, 0600);
#endif
while(scanf("%d %d", &num1, &num2) == 2)
{
numerator = 1;
denominator = 1;
n_minus_r = num1 - num2;
for(i=num1; i > n_minus_r; i--)
numerator = numerator * i;
for(i=num2; i > 1; i--)
denominator = denominator * i;
ans = numerator / denominator;
printf("%.fn", ans);
}
return 0;
}
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
When testing my program using sample input, I get correct answers. Yet, when I submit my program, it says I have a wrong answer. What are the tricky inputs?
I've tried using sample inputs of num1 = 2^31 and num2 = 2^31, and num1 = 2^31 and num2 = 1, which seem to be about the two limits since the answer will fit into an integer. And my answers are correct on my machine! I've also experimented with num1 = 0 & num2 = 0. What gives?
I've tried using sample inputs of num1 = 2^31 and num2 = 2^31, and num1 = 2^31 and num2 = 1, which seem to be about the two limits since the answer will fit into an integer. And my answers are correct on my machine! I've also experimented with num1 = 0 & num2 = 0. What gives?
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
why I get signal 11 (SIGSEGV) again!!?
I get
----------------------------
Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference
Before crash, it ran during 5.550 seconds.
-----------------------------
why?
my source is wrong?
(problem 530 in C)
#include "stdio.h"
int data[141],n,k;
long p;
void clear_data()
{
int i;
for(i=1;i<=140;i++)
data=0;
}
void run_data()
{
int i,r,q;
p=1;
i=n;
while(i>=2)
{
if(data==1&&data[i-1]==0)
{
data=0;
data[i-1]=1;
q=0;
for(r=i;r<=n;r++)
if(data[r]==1) q++;
for(r=i;r<=n;r++)
data[r]=0;
for(r=n;r>n-q;r--)
data[r]=1;
i=n;
p++;
}else{
i--;
}
}
}
void main()
{
int i;
while(1){
scanf("%d %d\n",&n,&k);
if(n==0&&k==0) break;
clear_data();
for(i=n;i>n-k;i--)
data=1;
run_data();
printf("%ld\n",p);
}
}
----------------------------
Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference
Before crash, it ran during 5.550 seconds.
-----------------------------
why?
my source is wrong?
(problem 530 in C)
#include "stdio.h"
int data[141],n,k;
long p;
void clear_data()
{
int i;
for(i=1;i<=140;i++)
data=0;
}
void run_data()
{
int i,r,q;
p=1;
i=n;
while(i>=2)
{
if(data==1&&data[i-1]==0)
{
data=0;
data[i-1]=1;
q=0;
for(r=i;r<=n;r++)
if(data[r]==1) q++;
for(r=i;r<=n;r++)
data[r]=0;
for(r=n;r>n-q;r--)
data[r]=1;
i=n;
p++;
}else{
i--;
}
}
}
void main()
{
int i;
while(1){
scanf("%d %d\n",&n,&k);
if(n==0&&k==0) break;
clear_data();
for(i=n;i>n-k;i--)
data=1;
run_data();
printf("%ld\n",p);
}
}