SPOJ MARBLES(简单组合)
暑期个人赛第四场
题意:把n个求分给k个颜色,
思路:转化一下,将球的n-1个空隙插上隔板,那么答案就是c(n-1,k-1);
代码如下:
#include <cstdio> long long c(int n, int k) { if(n-k<k) k = n-k; double ans = 1; for(int i = 1; i <= k; i++) ans*=1.0*(n-i+1)/i; return (long long)(ans+0.5); } int main () { int n, k, t; scanf("%d", &t); while(t--) { scanf("%d %d",&n,&k); printf("%lld\n", c(n-1,k-1)); } return 0; }
代码如下:
#include <cstdio> double c(int n, int k) { if(n-k<k) k = n-k; double c = 1.0; for(int i = 1; i <= k; ++i) c = c*(n-i+1)/i; return c; } int main () { int n, k, t; scanf("%d", &t); while(t--) { scanf("%d %d",&n,&k); printf("%lld\n", c(n-1,k-1)); } return 0; }