:C(,n m)不超时的:
#include <stdio.h> #include <math.h> #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <cmath> #define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1 #define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0 #define sf scanf #define pf printf #define INF 1 << 29 #define eps 1e-6 const double PI = acos(-1.0); #define lint __int64 #define LL long long #define MAX 1e9 + 7 #define maxn 40005 //101^110=011 异或 #define ULLint unsigned long long //2^64-1>1.8*10^19 #define clr(x) memset(x, 0, sizeof(x)) #define Clr(x) memset(x, -1, sizeof(x)) using namespace std; char str[105]; __int64 n,k; __int64 C(__int64 n,__int64 k) { __int64 ans = 1; for(__int64 i = 1;i <= k;i++) { ans *= (n - i + 1); ans /= i; } return ans; } int main() { while(scanf("%I64d%I64d",&n,&k) != EOF) { if(n == 0 && k == 0) break; if(2*k > n) k = n - k; printf("%I64d\n",C(n,k)); } return 0; }
简单的易超时(递归)
#include <stdio.h> #include <math.h> #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <cmath> #define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1 #define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0 #define sf scanf #define pf printf #define INF 1 << 29 #define eps 1e-6 const double PI = acos(-1.0); #define lint __int64 #define LL long long #define MAX 1e9 + 7 #define maxn 40005 //101^110=011 异或 #define ULLint unsigned long long //2^64-1>1.8*10^19 #define clr(x) memset(x, 0, sizeof(x)) #define Clr(x) memset(x, -1, sizeof(x)) using namespace std; lint C (int n, int m) { if(0 == m) return 1; else return n * C(n - 1, m - 1) / m; } int main () { int n, m; while (2 == sf("%d%d", &n, &m)) { if(0 == n && 0 == m) break; pf("%I64d\n", C(n,m)); } return 0; }