zoukankan      html  css  js  c++  java
  • soj1221- 光棍

    1221: 光棍

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 67  Solved: 19 [Submit][Status][Web Board]

    Description

    光棍都想参加这样的节目。

    规则如下:

    每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光 棍依次抽取一张纸条,如果上面的名字就是自己心仪的女生,那么主持人就在现场给该女生打电话,告诉这个光棍对她的爱慕之情,并让光棍当场表白,并得到现场所有人的祝福,没抽到的,嘿嘿就可以幸免了。

    假设一共有N个光棍,其中有M个没有抽到自己的纸条,求发生这种情况一共有多少种可能。

    Input

    每行包含两个整数N和M(1<M<=N<=20),以EOF结尾。

    Output

    对于每个测试实例,请输出一共有多少种发生这种情况的可能,每个实例的输出占一行。

    Sample Input

    2 2
    3 2
    

    Sample Output

    1
    3
    ---------------------------------------------------------------------------------------------

    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    long long jc[22],f[22];
    int n,m;
    void predeal()
    {
        int i,j;
        jc[0]=1;
        for (i=1;i<=20;i++) jc[i]=i*jc[i-1];
    }
    long long cc(int a,int b)
    {
        long long t=1;
        int i,j,x;
      //  cout<<a<<b<<endl;
        x=b;
        if (a-b>x) x=a-b;
       // cout<<t<<"!"<<endl;;
      //  cout<<"x="<<x<<endl;
        for (i=x+1;i<=a;i++) t*=i;//cout<<t<<"!!";
        for (i=2;i<=a-x;i++) t/=i;
      //  cout<<t<<endl;
        return t;
    }
    long long deal(int a)
    {
        long long tot;
        int i;
        if (f[a]) return f[a];
        tot=jc[a];
        for (i=1;i<=a;i++)
            tot-=cc(a,i)*deal(a-i);
        f[a]=tot;
        return tot;
    }
    int main()
    {
        int i;
        predeal();
        while (scanf("%d%d",&n,&m)!=EOF)
        {
          memset(f,0,sizeof(f));
          f[0]=1;f[1]=0;
          cout<<cc(n,m)*deal(m)<<endl;
       //  for (i=1;i<=20;i++) cout<<i<<" "<<deal(i)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    UVA 254 Towers of Hanoi
    UVA 701 The Archeologists' Dilemma
    UVA 185 Roman Numerals
    UVA 10994 Simple Addition
    UVA 10570 Meeting with Aliens
    UVA 306 Cipher
    UVA 10160 Servicing Stations
    UVA 317 Hexagon
    UVA 10123 No Tipping
    UVA 696 How Many Knights
  • 原文地址:https://www.cnblogs.com/mbcxm/p/3194102.html
Copyright © 2011-2022 走看看