[Prufer序列推论] SRM 697 div1 ConnectedStates

首先根据可图性判定定理,可以发现任意一个和为 2(n1) 的度数序列都存在方案,然后就是对于一个度数序列求生成树个数,根据Prufer推论,答案应该是

{di}((n2)!ni=1(di1)!×i=1nwdiidi)

这个直接做是 O(n3)

优化成 O(n2) ,参见大佬的题解

// BEGIN CUT HERE  
#include<conio.h>
#include<sstream>
// END CUT HERE  
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<set>
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;


const int N=2005;
const int P=1e9+7;
inline ll Pow(ll a,int b){
  ll ret=1; for (;b;b>>=1,a=a*a%P) if (b&1) ret=ret*a%P; return ret;
}

ll fac[N],inv[N];

inline void Pre(int n){
  fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%P;
  inv[1]=1; for (int i=2;i<=n;i++) inv[i]=(ll)(P-P/i)*inv[P%i]%P;
  inv[0]=1; for (int i=1;i<=n;i++) inv[i]=inv[i]*inv[i-1]%P;
}

int n,w[N];
int f[2][N];

inline void add(int &x,int y){
  x+=y; if (x>=P) x-=P;
}

class ConnectedStates{
public:
  int getSum(vector <int> a){
    int sum=0; ll pro=1;
    n=a.size(); Pre(n); for (int i=1;i<=n;i++) w[i]=a[i-1],add(sum,w[i]),pro=pro*w[i]%P;
    cl(f);f[0][0]=1; int t=0;
    for (int i=1;i<=n;i++,t^=1){
      memcpy(f[t^1],f[t],sizeof(f[t]));
      for (int j=0;j<i;j++)
    add(f[t^1][j+1],(ll)f[t][j]*w[i]%P);
    }
    ll ans=0;
    for (int k=0;k<n-1;k++)
      ans+=f[t][k]*Pow(sum,n-2-k)%P*inv[n-2-k]%P;
    return fac[n-2]*pro%P*(ans%P)%P;
  }


  // BEGIN CUT HERE
public:
  void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
private:
  template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
  void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
  void test_case_0() { int Arr0[] = {3, 10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 30; verify_case(0, Arg1, getSum(Arg0)); }
  void test_case_1() { int Arr0[] = {2, 2, 2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 96; verify_case(1, Arg1, getSum(Arg0)); }
  void test_case_2() { int Arr0[] = {1, 1, 1, 1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 60; verify_case(2, Arg1, getSum(Arg0)); }
  void test_case_3() { int Arr0[] = {205659656, 888625810}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 118040021; verify_case(3, Arg1, getSum(Arg0)); }
  void test_case_4() { int Arr0[] = {2, 1, 2, 1, 2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 27808; verify_case(4, Arg1, getSum(Arg0)); }
  void test_case_5() { int Arr0[] = {14204721, 921856626, 804287587, 304606131, 277490604, 
                     310739929, 900757841, 818413665, 155154829, 836327185, 
                     602928524, 26132484, 587345385, 936362852, 92603422}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 376924431; verify_case(5, Arg1, getSum(Arg0)); }

  // END CUT HERE

};

// BEGIN CUT HERE
int main(){
  ConnectedStates ___test;
  ___test.run_test(-1);
  getch() ;
  return 0;
}
// END CUT HERE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值