树形Dp的题,根据题意建树。

DP[i][0] 表示以i为根节点的树的包含i的时候的所有状态点数的总和

Dp[i][1] 表示包含i结点的状态数目

对于一个子节点v

Dp[i][0] = (Dp[v][1]+1)*Dp[i][0]+Dp[v][0]*Dp[i][1]

表示子节点的所有状态与i的所有的状态之间的组合(可以不组合,所以DP[v][1]+1),

接下来更新i的状态数目

DP[i][1] = Dp[i][1]*(Dp[v][1]+1)

这样就可以算出来i结点为根结的所以状态,树中的所有点的状态和就是答案。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime> using namespace std; typedef long long LL; typedef vector<int>VI; const int Max = 1e5+10; const LL Mod = 1e9+7; LL Dp[Max][2]; LL a[Max]; VI M[Max]; void DFS(int u, int fa)
{
Dp[u][0] = Dp[u][1] = 1; for(int i = 0;i < M[u].size(); i++)
{
if(M[u][i] == fa) continue; DFS(M[u][i], u); int v = M[u][i]; Dp[u][0] = (((Dp[v][1] + 1) * Dp[u][0]) % Mod + (Dp[u][1] * Dp[v][0]) % Mod) % Mod; Dp[u][1] = (Dp[u][1] * (Dp[v][1]+1)) % Mod;
}
} class SubtreesCounting {
public:
int sumOfSizes(int, int, int, int, int);
}; int SubtreesCounting::sumOfSizes(int n, int a0, int b, int c, int m)
{
a[0] = a0; for(int i = 1;i <= n-1; i++)
{
a[i] = (b * a[i-1] + c) % m;
} for(int i = 1 ;i <= n-1; i++)
{
int j = a[i-1] % i; M[i].push_back(j); M[j].push_back(i);
} LL ans = 0; for(int i = 0; i < n; i++)
{
if(!Dp[i][0]) DFS(i,-1); ans = (ans + Dp[i][0]) % Mod;
} return ans;
}

Topcoder SRM 683 Div2 - C的更多相关文章

  1. Topcoder SRM 683 Div2 B

    贪心的题,从左向右推过去即可 #include <vector> #include <list> #include <map> #include <set&g ...

  2. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  3. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  4. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  5. Topcoder srm 632 div2

    脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...

  6. topcoder SRM 628 DIV2 BracketExpressions

    先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...

  7. topcoder SRM 628 DIV2 BishopMove

    题目比较简单. 注意看测试用例2,给的提示 Please note that this is the largest possible return value: whenever there is ...

  8. Topcoder SRM 626 DIV2 SumOfPower

    本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { ; ; ...

  9. Topcoder SRM 626 DIV2 FixedDiceGameDiv2

    典型的条件概率题目. 事件A在另外一个事件B已经发生条件下的发生概率.条件概率表示为P(A|B),读作“在B条件下A的概率”. 若只有两个事件A,B,那么, P(A|B)=P(AB)/P(B) 本题的 ...

随机推荐

  1. github for window的代理设置方法

    修改 .gitconfig 文件,主要是针对http 和 https进行修改,设置代理 [user] name = name email = mail@.com [http] proxy = 配置文件 ...

  2. 常见的java类

    String System StringBuilder Thread Math ArrayList LinkedList HashMap HashSet Scanner Calendar Date F ...

  3. EasyUI分页

    $("#tanModelBox1").css("display","none"); $('#dg').datagrid({ fitColum ...

  4. Unix时间戳

    Unix时间戳 http://www.linuxidc.com/Linux/2016-01/127572.htm 这里的时间戳,指的就是Unix时间戳(Unix timestamp).它也被称为Uni ...

  5. python正则

    1.. 匹配任意除换行符"\n"外的字符:2.*表示匹配前一个字符0次或无限次:3.+或*后跟?表示非贪婪匹配,即尽可能少的匹配,如*?重复任意次,但尽可能少重复:4. .*? 表 ...

  6. tomcat,zookeeper,activeMQ,Kafka设置jvm参数

    1,tomcat设置jvm参数 设置方法:   在tomcat bin 目录增加配置:setenv.sh   #add tomcat pid CATALINA_PID="$CATALINA_ ...

  7. Molile App(HTTP/HTML)—Record and Analyze Traffic

  8. 戴尔3542安装ubuntu时出现:failed to lead ldlinux.c32

    解决办法: 1. 开机未进入系统是连续敲击F2,进入BIOS2.在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled3. 将Boot List Option 改为 Lega ...

  9. 2.5 C#的数据类型

    在我们定义变量的时候需要使用数据类型,不同数据类型定义的变量,它的值的表现形式不同.比如整型主要表示整数,浮点型表示小数等等. C#中的数据类型有很多同C语言的相同,先学习一些简单的数据类型,其他的以 ...

  10. Responsive设计——不同设备的分辨率设置

    下面我们一起来看看CSS3 Meida Queries在标准设备上的运用,大家可以把这些样式加到你的样式文件中,或者单独创建一个名为“responsive.css”文件,并在相应的条件中写上你的样式, ...