Code: Select all
nC0 = 1
Moderator: Board moderators
Code: Select all
nC0 = 1
Code: Select all
#include <stdio.h>
int main()
{
int n = 0;
int r = 0;
unsigned int Result = 1;
double Temp = 1.0;
int i = 0;
while (1)
{
scanf("%d %d", &n, &r);
if (n == 0 && r == 0)
break;
r = n - r < r ? n - r : r;
for (i = r, Temp = 1.0; i >= 1; i--)
Temp = Temp * ((double)(n--) / i);
Result = (int)Temp;
printf("%d\n", Result);
}
return 0;
}
Code: Select all
/*problem for solving nCk
Author :-Vivek Sharma */
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
long double n,k;//binomial coefficient
while(cin>>n>>k)
{
if(n==0&&k==0)
return 0;
long double num=1,den=1;
if(n-k<k)
k=n-k;
for(int i=k;i>0;i--)
{
num=(n+i-k)*num;
den=(k-i+1)*den;
//cout<<num<<" "<<den<<endl;
if(fmod(num,den)==0)
{
num=num/den;
den=1;
}
}
printf("%.0lf\n",num);
}
return 0;
}
Code: Select all
//530 C0DED by RED-C0DE ~ 07-11-06 11:09 pm
#include <cstdio>
#include <iostream>
using namespace std;
__int64 f2(int n,int m)
{
static __int64 cache[1000][1000]={0};
if(n<1000 && m<1000 && cache[n][m])
return cache[n][m];
if(m==n || m==0)
return 1;
return n<1000 && m<1000 ? (cache[n][m] = f2(n-1,m) + f2(n-1,m-1)) : f2(n-1,m) + f2(n-1,m-1) ;
}
int main()
{
int n ,m;
while(cin >> n >> m && n )
printf("%ld\n" , f2(n,m));
return 0;
}
20000 20000 is a valid case. And your will return nothing but RTE.Warning: Don't underestimate the problem. The result will fit into an integer - but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. The test cases will go to the limit.
Code: Select all
i have done the following code to solve this problem but it wont run properly, and breaks. I am totally confused as to why this is. Can anyone help please? i take the system("pause") out before submitting.
// Binomial Showdown - 530.cpp : Defines the entry point for the console application.
#include <iostream>
#include <math.h>
using namespace std;
int factorial(int input){
int answer = 0, current = 1;
while (current < input) {
answer *= current;
current++;
}
return answer;
}
int main(int argc, char* argv[])
{
a:
int n=1,k=1;
int o,q,w,answer;
scanf("%d%d",&n,&k);
if (n!=0 && k !=0)
{
q=factorial(n);
w=factorial(k);
o = q /( w * ( q - w ) );
cout<<o<<endl;
system("PAUSE");
goto a;
}
else
{
return 0;
}
}
main () should return a value tooThozz wrote: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"); }
Code: Select all
main ()
{
...
return 0;
}
YOU SHOULD initialize answer=1;scott1991 wrote:my code doesn't work but i have no idea why. can anyone help please?
ThanksCode: Select all
i have done the following code to solve this problem but it wont run properly, and breaks. I am totally confused as to why this is. Can anyone help please? i take the system("pause") out before submitting. // Binomial Showdown - 530.cpp : Defines the entry point for the console application. #include <iostream> #include <math.h> using namespace std; int factorial(int input){ int answer = 0, current = 1; while (current < input) { answer *= current; current++; } return answer; } int main(int argc, char* argv[]) { a: int n=1,k=1; int o,q,w,answer; scanf("%d%d",&n,&k); if (n!=0 && k !=0) { q=factorial(n); w=factorial(k); o = q /( w * ( q - w ) ); cout<<o<<endl; system("PAUSE"); goto a; } else { return 0; } }
Code: Select all
answer *= current;
Code: Select all
#include<stdio.h>
int main()
{
long int sum=1,sum1=1,sum2=1,i,m,n,num;
while(scanf("%ld %d",&m,&n)==2)
{
for(i=1;i<=m;i++)
{
sum=sum*i;
}
for(i=1;i<=n;i++)
{
sum1=sum1*i;
}
for(i=1;i<=(m-n);i++)
{
sum2=sum2*i;
}
num=sum/(sum1*sum2);
printf("%d\n",num);
}
return 0;
}
Code: Select all
else
{
r=n+1;
for(i=r;i<=m;i++)
{
sum=sum*i;
}
for(i=1;i<=n;i++)
{
sum2=sum2*i;
}
num=sum/sum2;