求C(N,K)

Posted by Aspe on April 24, 2015
#include<iostream>

using namespace std;

long long ans[10000];

int gys(int a,int b);
void mut(int x);

int main()
{
    int n,k,i,a[1000];
    cin>>n>>k;
    for (i=0; i<k;i++,n--) 
     a[i]=n;
    for (i=1; i<=k; i++)
    {
        int temp=i;
       for (int j=0; j<k; j++)
       {
           int t=gys(temp,a[j]);
           a[j]=a[j]/t;
           temp=temp/t;
       }
    }
    ans[1]=1; ans[0]=1;
    for (i=0; i<k; i++)
      mut(a[i]);
    for (i=ans[0]; i>=1; i--)
      cout<<ans[i];
    cout<<endl;
}

int gys(int a,int b)
{
    if (a%b==0) return b;
    return gys(b,a%b);
}

void mut(int x)
{
    int i,c=0;
    for (i=1;i<=ans[0];i++)
    {
        ans[i]=ans[i]*x;
    }
    for (i=1;i<=ans[0];i++)
    {
        ans[i]=ans[i]+c;
        c=ans[i]/10;
        ans[i]=ans[i]%10;
    }
    for (i=ans[0];c>0;i++,ans[0]++)
    {
        ans[i+1]=ans[i+1]+c;
        c=ans[i+1]/10;
        ans[i+1]=ans[i+1]%10;
    }
}
CC 原创文章采用CC BY-NC-SA 4.0协议进行许可,转载请注明:“转载自:求C(N,K)