zoukankan      html  css  js  c++  java
  • 重排问题 (Arrange the Numbers,UVa 11481)

     1 #include <iostream>
     2 #include <string.h>
     3 #include <string>
     4 #include <fstream>
     5 #include <algorithm>
     6 #include <stdio.h>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 using namespace std;
    12 const double eps = 1e-8;
    13 const double pi=acos(-1.0);
    14 const int INF=0x7fffffff;
    15 unsigned long long uINF = ~0LL;
    16 #define MAXN 1007
    17 #define mod 1000000007
    18 typedef long long LL;
    19 LL c[MAXN][MAXN];
    20 LL a[MAXN];
    21 void init()
    22 {
    23     memset(c,0,sizeof(c));
    24     c[0][0]=a[0]=1;
    25     for(LL n=1;n<=1000;n++)
    26     {a[n]=(a[n-1]*n)%1000000007;
    27         for(LL k=0;k<=n;k++)
    28         {
    29             if(k){c[n][k]=(c[n-1][k]+c[n-1][k-1])%mod;}
    30             else {c[n][k]=1;}
    31             //cout<<c[n][k]<<' ';
    32         }
    33         //cout<<endl;system("pause");
    34     }
    35     //cout<<c[1000][1000]<<endl;
    36 }
    37 int main()
    38 {
    39     //freopen("0.in","r",stdin);
    40     init();
    41     int n,m,k,T,t=1;
    42     scanf("%d",&T);
    43     while(T--)
    44     {
    45         scanf("%d%d%d",&n,&m,&k);
    46         LL ans=0;
    47         LL tempn,tempa,sum=0;
    48         for(int i=1;i<=m-k;i++)
    49         {
    50             tempn=c[m-k][i];
    51             tempa=a[n-k-i];
    52             if(i&1)
    53             {
    54             sum+=((tempn%mod)*(tempa%mod))%mod;
    55             sum+=mod;
    56             sum%=mod;
    57             }
    58             else
    59             {
    60             sum-=((tempn%mod)*(tempa%mod))%mod;
    61             sum+=mod;
    62             sum%=mod;
    63             }
    64         }
    65         ans=a[n-k]-sum+mod;
    66         ans%=mod;
    67 
    68         ans*=c[m][k];
    69         ans%=mod;
    70         //if(ans<0)ans+=mod;
    71         printf("Case %d: %lld
    ",t++,ans);
    72     }
    73 
    74     return 0;
    75 }

    容斥原理~

    #include <iostream>
    #include <string.h>
    #include <string>
    #include <fstream>
    #include <algorithm>
    #include <stdio.h>
    #include <vector>
    #include <queue>
    #include <set>
    #include <cmath>
    using namespace std;
    const double eps = 1e-8;
    const double pi=acos(-1.0);
    const int INF=0x7fffffff;
    unsigned long long uINF = ~0LL;
    #define MAXN 1007
    #define mod 1000000007
    typedef long long LL;
    LL c[MAXN][MAXN];
    LL a[MAXN];
    void init()
    {
        memset(c,0,sizeof(c));
        c[0][0]=a[0]=1;
        for(LL n=1;n<=1000;n++)
        {a[n]=(a[n-1]*n)%1000000007;
            for(LL k=0;k<=n;k++)
            {
                if(k){c[n][k]=(c[n-1][k]+c[n-1][k-1])%mod;}
                else {c[n][k]=1;}
                //cout<<c[n][k]<<' ';
            }
            //cout<<endl;system("pause");
        }
        //cout<<c[1000][1000]<<endl;
    }
    int main()
    {
        //freopen("0.in","r",stdin);
        init();
        int n,m,k,T,t=1;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&m,&k);
            LL ans=0;
            LL tempn,tempa,sum=0;
            for(int i=1;i<=m-k;i++)
            {
                tempn=c[m-k][i];
                tempa=a[n-k-i];
                if(i&1)
                {
                sum+=((tempn%mod)*(tempa%mod))%mod;
                sum+=mod;
                sum%=mod;
                }
                else
                {
                sum-=((tempn%mod)*(tempa%mod))%mod;
                sum+=mod;
                sum%=mod;
                }
            }
            ans=a[n-k]-sum+mod;
            ans%=mod;

            ans*=c[m][k];
            ans%=mod;
            //if(ans<0)ans+=mod;
            printf("Case %d: %lld ",t++,ans);
        }

        return 0;
    }

  • 相关阅读:
    BootStrap .row-cols 类的用法
    苹果手机浏览器$(document).on(“click”,function(){})点击无效的问题
    $("节点名").html("字符串")和$("节点名").text("字符串")区别
    linux 安装Nginx
    linux安装nginx
    vue dev开发环境跨域和build生产环境跨域问题解决
    正在载入中......loading页面的几种方法
    浏览器断点调试js
    vue组件之间传值方式解析
    基于Vue + Vuex + Vue-router + Webpack 2.0打造微信界面
  • 原文地址:https://www.cnblogs.com/TO-Asia/p/3218943.html
Copyright © 2011-2022 走看看