TG5 epidemic prevention work arrangements

 

Title Description

H outbreak of pneumonia province a total of  n  prefecture-level cities, in order to maximize slow down the spread of disease, these prefecture-level city with 1 to  n  numbered starting from  n connected -1 roads, and ensure Unicom. W co-ordinate epidemic prevention and control of the capital city as a root node, numbered 1.

For prevention, we first need to arrange each prefecture-level city an important degree. Specifically, for each municipality, its degree of importance is [. 1, m an integer (which may be repeated)] in. At the same time there is a special request: for each path from the root node to the leaf nodes, all numbers on the path must be the greatest common divisor 1.

Now the number of legal arrangements for the importance of the method you need to find. The answer to 10 ^ 7 + 9 mod.

Input Format

The first line of two integers  n- , m  , Significance shown as the title.

Next,  n- -1 lines of two integers  U , V  , represents a path.

Output Format

Line An integer that represents the answer

[Agreed] with the data range

……

To 100% of the data satisfies 1 ≦ n- ≦ 10 ^ 5,1≤ m ≦ 20

 

Range of observational data, found m is small, and the prime number 20 has 8 or less

Subtree DP [i] [j] denotes the point i, j represents the number of binary quality satisfies the following conditions

Meet the conditions means that this point down to the leaves each path has at least one point excluding the prime number

That is, the above position can fill in the number of prime numbers contain this

A large number of redundant state using the memory search, the request is dp [1] [255], each number enumerated current fill, j to obtain the child's request, and then multiplying the number of children of each program.

 

Embodied in code comments presented in a way

Talk is cheap,shou me the code

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define mod 1000000007
 4 using namespace std;
 5 int n,m,f[25],p[8]={2,3,5,7,11,13,17,19};
 6 ll dp[257][100005];
 7 vector<int> g[100005];
 8 ll dfs(intnow, int FA, int mask) {
 . 9      IF ! (DP [mask] [now] = - . 1 ) // node may be accessed repeatedly performs search memory 
10        return DP [mask] [now];
 . 11    LL = RES 0 ;
 12 is    for ( int I = . 1 ; I <= m; I ++ ) {
 13 is      IF (G [now] .size () == . 1 ! && now = . 1 )
 14        ! RES + = (mask & F [I]); / / leaf node i is a legal filling program, if the program number is incremented by one, plus or zero (non-calculation characteristic) 
15          the else {
 16              LL T = . 1 ;
 . 17             for ( int J = 0 ; J <G [now] .size (); J ++ ) {
 18 is                  int to = G [now] [J];
 . 19                  IF (to FA ==) Continue ;
 20 is                  T = T * DFS ( to, now, mask & F [I]) MOD%; // conditions enumerated child node and calculate the answer after backtracking, updating the child to meet the required
 21 is                                                                              // . 1:                                                                                                                  
22 is              }
 23 is              RES = (T + RES) MOD%; // case where the node i is a contribution to the selected answers 
24          }
 25      }
 26 is      return DP [mask] [now] = RES;//更新dp值并回溯 
27 }
28 int main(){
29     cin>>n>>m;
30     for(int i=1;i<=n-1;i++){
31         int u,v;
32         cin>>u>>v;
33         g[u].push_back(v);
34         g[v].push_back(u);
35     }
36     for(int i=1;i<=20;i++)
37       for(int j=0;j<. 8 ; J ++ )
 38 is            IF ((i%! ) P [J])
 39              F [i] | = . 1 << J; // pretreatment i prime factors which, in order to transfer 
40      Memset (DP, - . 1 , the sizeof (DP));
 41 is      COUT << DFS ( . 1 , 0 , 255 );
 42 is      return  0 ;    
 43 is }
View Code

 

Guess you like

Origin www.cnblogs.com/vv123/p/12334099.html