zoukankan      html  css  js  c++  java
  • CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K

    其实每个点的颜色只要和父亲不一样即可;

    所以至多 i 种颜色就是 ( i * (i-1)^{n-1} ),设为 ( f(i) ),设恰好 i 种颜色为 ( g(i) )

    那么 ( f(i) = sumlimits_{j=0}^{i} C_{i}^{j} * g(j) )

    二项式反演得到 ( g(i) = sumlimits_{j=0}^{k} (-1)^{k-j} * C_{k}^{j} * f(j) )

    代码如下:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    int rd()
    {
      int ret=0,f=1; char ch=getchar();
      while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
      while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
      return f?ret:-ret;
    }
    int const xn=2505,mod=1e9+7;
    int n,k,f[xn],c[xn][xn];
    int upt(int x){while(x>=mod)x-=mod; while(x<0)x+=mod; return x;}
    ll pw(ll a,int b){ll ret=1; for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod; return ret;}
    void init()
    {
      for(int i=0;i<=k;i++)c[i][0]=1;
      for(int i=1;i<=k;i++)
        for(int j=1;j<=i;j++)
          c[i][j]=upt(c[i-1][j]+c[i-1][j-1]);
    }
    int main()
    {
      n=rd(); k=rd(); init();
      for(int i=1;i<n;i++)rd();
      for(int i=1;i<=k;i++)f[i]=(ll)i*pw(i-1,n-1)%mod;
      int ans=0;
      for(int i=0;i<=k;i++)ans=upt(ans+(ll)f[i]*c[k][i]%mod*(((k-i)&1)?-1:1));
      printf("%d
    ",ans);
      return 0;
    }
  • 相关阅读:
    中国天气网数据获取
    雪球数据的定时爬取
    爬虫下载中间件
    简书全站CrawlSpider爬取 mysql异步保存
    房天下新房和二手房
    多线程
    selenium+chromdriver 动态网页的爬虫
    汽车之家下载文件和图片
    re正则
    532.数组中的K-diff数对
  • 原文地址:https://www.cnblogs.com/Zinn/p/10273563.html
Copyright © 2011-2022 走看看