然后,double肯定是不能过的。于是傻傻用结构体存了个分数。然后分母乘起来爆long long了。然后和GT,喵呜三个debug了一下午 =。= 怪我不懂逆元,坑了两位。
逆元:http://cn.bing.com/ 大致就是除个数取模等于成该数的逆元再取模吧...
所以转移变成了:
然后dp就是在整数里面转移了。
/*********************************************** ** problem ID : hdu_5378.cpp ** create time : Wed Aug 12 11:14:30 2015 ** auther name : xuelanghu ** auther blog : blog.csdn.net/xuelanghu407 **********************************************/ #include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 1000 + 5; const long long MOD = (long long)(1e9 + 7); struct Node { long long a, b; Node() {a = 0; b = 1;} Node(long long _a, long long _b): a(_a), b(_b) {} }; long long _gcd(long long a, long long b) { if (b == 0) return a; else return _gcd(b, a%b); } inline Node add (Node x, Node y) { long long rec, res, tmp; rec = x.a*y.b + x.b*y.a; res = x.b*y.b; tmp = _gcd(rec, res);// if (tmp == 0) tmp = 1; return Node(rec/tmp, res/tmp); } inline Node mul (Node x, Node y) { long long rec, res, tmp; rec = x.a * y.a; res = x.b * y.b; tmp = _gcd(rec, res);// if (tmp == 0) tmp = 1; return Node(rec/tmp, res/tmp); } int n, k; vector<int> v[MAXN]; long long dp[MAXN][MAXN]; long long c[MAXN]; long long dfs(int t, int fa) { long long cnt = 1L; for (int i=0; i<v[t].size(); i++) { if (v[t][i] == fa) continue; cnt += dfs(v[t][i], t); } return c[t] = cnt; } long long power(long long a, long long p) { long long res=1L; while(p) { if(p&1) res = (res*a)%MOD; a = (a*a)%MOD; p >>= 1; } return res; } long long Inv(long long a) { return power(a, MOD-2); } long long _In[MAXN]; void init() { for (int i=1; i<=1002; i++) { _In[i] = Inv(i); } } long long f(int n) { long long res = 1; for (int i=1; i<=n; i++) { res = (res * (long long) (i)) % MOD; } return res; } int main () { int T_case; init(); scanf("%d", &T_case); for (int i_case=1; i_case<=T_case; ++i_case) { scanf ("%d%d", &n, &k); for (int i=0; i<=n; i++) v[i].clear(); // memset(c, 0,sizeof(c)); int a, b; for (int i=1; i<n; i++) { scanf ("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } dfs(1, -1); dp[1][0] = ((c[1]-1) * _In[c[1]])%MOD; dp[1][1] = 1 * _In[c[1]]; // cout << dp[1][0].a << "/" << dp[1][0].b << " "; // cout << dp[1][1].a << "/" << dp[1][1].b << endl; for (int i=2; i<=n; i++) { // dp[i][0] = mul(dp[i-1][0], Node(c[i]-1, c[i])); dp[i][0] = ((dp[i-1][0] * (c[i]-1)) % MOD * _In[c[i]]) % MOD; for (int j=1; j<=min(i, k); j++) { // dp[i][j] = add(mul(dp[i-1][j], Node(c[i]-1, c[i])), mul(dp[i-1][j-1], Node(1, c[i]))); dp[i][j] = ((((dp[i-1][j] * (c[i]-1)) % MOD) * _In[c[i]]) % MOD + (dp[i-1][j-1] * _In[c[i]]) % MOD) % MOD; // if (i == n && j == k) cout << "hahahaha~" << endl; // cout << dp[i][j].a << "/" << dp[i][j].b << " "; // if(dp[i][j].b <= 0) dp[n+100][k+1].b = 100; }// cout << endl; } long long res = dp[n][k]; // cout << res.a << "/" << res.b << endl; long long ans = (f(n) * res) % MOD; printf ("Case #%d: %d\n", i_case, (int)ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/xuelanghu407/article/details/47627475