树形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. 精简的javascript下throttle和debounce代码

    //频率控制 函数连续调用时,fn 执行频率限定为 1次/waitMs.立即执行1次 function throttle(fn, waitMs) { var lastRun = 0; return f ...

  2. Ant 学习及常用任务

    安装与配置 下载地址:http://ant.apache.org/bindownload.cgi,解压. 添加系统环境变量: ANT_HOME,该变量指向Ant解压后的根目录. PATH,追加path ...

  3. python爬虫学习-爬取某个网站上的所有图片

    最近简单地看了下python爬虫的视频.便自己尝试写了下爬虫操作,计划的是把某一个网站上的美女图全给爬下来,不过经过计算,查不多有好几百G的样子,还是算了.就首先下载一点点先看看. 本次爬虫使用的是p ...

  4. Notification中使用Glide

    之前一直在琢磨Glide自定义使用中的一些经验:今天简单的分享下Notification中使用Glide去加载icon方法: 我们都知道使用android通知时,一般可能会有如下代码: Notific ...

  5. Ehcache Demo

    转自: https://my.oschina.net/zb0423/blog/60957http://www.cnblogs.com/fsjin/articles/3521261.html Ehcac ...

  6. form 提交数组的一些trick

    在给服务器传值时form利用 $.post( "/member/member/book/" + event_id, { tickets: tickets, csrf_ppw_tok ...

  7. linux udev 自动挂载 SD卡/U盘

    本文记录使用udev自动挂载SD卡和U盘的方法. 参考链接 http://blog.chinaunix.net/uid-26119896-id-5211736.html 添加udev规则 创建文件/e ...

  8. 如何理解C#委托

    一:从下面的例子开始,理解委托变量本质 如上图,Condition是我定义的委托变量.这个委托变量的本质就是地址变量(即C语言当中的指针变量),它保存的是方法的入口地址. 当函数的调用者传递实参给这个 ...

  9. 利用pip安装模块(以安装pyperclip为例)

    >任务:利用pip安装pyperclip模块 >前提:你已经在你的电脑里面安装啦Python2.7的Windows版本,并且已经配置了环境变量 >实现步骤 >>打开你的P ...

  10. Python —基本数据类型

    运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31- ...