← Back to topics
Topic

o-loto

j
ja_bre
Moze neko da mi kaze kako da popravim ovaj kod da prodje sve testove?

#include <stdio.h>
int main()
{
int n,k;
scanf("%d%d", &k, &n);
double rez=1;
int i=1;
while(k+i <= n)
{
rez *= (double)(k+i)/(n-k-(i-1));
i++;
}
printf("%.0lf", rez);
getchar();
getchar();
return 0;
}


m
matteo123
you must talk on english on this part of forum!!!

you can solve this tak on many ways.

http://free-zg.t-com.hr/Vesna_Erceg/Kombinatorika/KOMB_uvod.htm

formula:
\frac{N!}{K!( N - K )!}

i have solved this task with pascal's triangle...you can google that proof!
j
ja_bre
can someone tell me why this code isnt woring

#include<iostream>
using namespace std;
int main()
{
int n,k;
cin >> k >> n;
for (int y = 0; y <= n; y++)
{
long long unsigned int c = 1;
for (int x = 0; x <= y; x++)
{
if(y==n && x==k)
cout << c;
c = c * (y - x) / (x + 1);
}

}

return 0;
}
d
demjan0001
k, here is why your code doesn't work ...

max unsigned long long = 2^64
and in task it is said that result won't go over 2^64, but in your code you have:

c = c * ( y - x ) / ( x + 1 );


and if you have in some state
c = 2^60
(y - x) = 2^20

if you multiply those two it will go over 2^64 ... even if after that you divide by (x+1) and result is then smaller than 2^64, you had in 1 moment solution over 2^64 and that is reason why you get WA ( wrong answer ).

to escape this, you can do following:

unsigned long long d = (unsigned long long)(x+1);
unsigned long long gcd = GCD( c, (unsigned long long)x+1 );
c /= gcd;
d /= (x+1)/gcd;
c *= d;

and here is function for GCD ( greatest common divisor )

unsigned long long GCD( unsigned long long a, unsigned long long b ) {
if ( b == 0 ) return a;
return GCD( b, a%b );
}