树形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. Git & Gitlab 使用指南

    2016-02-23   |   9,129字   |   分类于 工具  |   3条评论 去年小组在从 SVN 和 TFS 迁移到 Git 的过程中整理了这份文档,面向的用户是对 Git 和 SV ...

  2. Visual Studio 2013中因Browser Link引起的Javascript错误

    参考文章:http://www.cnblogs.com/daxnet/archive/2013/10/27/3391146.html Browser Link功能会引起js异常.在调试右侧的三角下拉选 ...

  3. VC 6中配置OpenGL开发环境

    2010,2012中配置类似 http://hi.baidu.com/yanzi52351/item/f9a600dffa4caa4ddcf9be1d VC 6中配置OpenGL开发环境 这里,我习惯 ...

  4. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  5. busybox rootfs 启动脚本分析(二)

    上次分析了busybox的启动脚本,这次分析一下init.d中一些脚本的内容. 参考链接 http://www.cnblogs.com/helloworldtoyou/p/6169678.html h ...

  6. tomcat由浅入深

    零.服务器.Servlet容器.web容器 Servlet容器:能够运行Servlet的环境叫做Servlet容器 web容器:能够运行web应用的环境就叫做web容器 weblogic websph ...

  7. Hadoop基础知识

    摘要:Hadoop的安装目录了解.etc的核心配置项.hadoop的启动.HDFS文件的block块级副本的存放策略.checkpoint触发设置. 1.hadoop目录了解 bin:可执行文件,命令 ...

  8. Maven 实战

    http://www.cnblogs.com/chowmin/category/599392.html

  9. java代码打包成jar以及转换为exe

    教你如何把java代码打包成jar文件以及转换为exe可执行文件 1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有 ...

  10. python numpy包

    在numpy包中我们可以用数组来表示向量,矩阵和高阶数据结构 首先导入numpy包: from numpy import* 初始化numpy数组有多种方式,比如说 1.python列表或元祖 2.使用 ...