树形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. ORM系列之二:EF(2)Code First

    目录 1. Code First是什么? 2. Code First 简单示例 3. 数据存储 4. 迁移 Code First是什么 Code First 顾名思义就是先写代码,当然不是乱写,而是按 ...

  2. java 基础三 下雪

    通过repaint()方法进行重画. import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; p ...

  3. json转bean对象

    一下为个人收藏,以便下次使用. 前端传的json格式为: [{"suppliercode":"gylhld_gycqlt3_gycqlt1","pro ...

  4. SqList *L 和 SqList * &L的区别/学习数据结构突然发现不太懂 小祥我查找总结了一下

    小祥在学习李春葆的数据结构教程时发现一个小问题,建立顺序表和输出线性表,这两个函数的形参是不一样的. 代码在这里↓↓↓ //定义顺序表L的结构体 typedef struct { Elemtype d ...

  5. CORS浏览器跨域

    在SO上发现一个解释跨域很棒的,忍不住拿过来 链接在此:http://stackoverflow.com/questions/10636611/how-does-access-control-allo ...

  6. C++学习笔记 四种新式类型转换

    static_cast ,dynamic_cast,const_cast,reinterpret_cast static_cast 定义:通俗的说就是静态显式转换,用于基本的数据类型转换,及指针之间的 ...

  7. setTimeout调用带参数的函数的方法

    function test(s){    alert(s);}window.setTimeout(function(){test('str');},1000);这样就可以了...为什么是这样呢.因为s ...

  8. Java提高篇——设计模式

    设计模式简介 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用.设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案.这些解决方案是众多软 ...

  9. Python学习笔记(基本功能的使用)

    整理了以前使用的几个笔记:上传到了github; python_notes 以后在慢慢更新吧:

  10. iOS,几种设计模式

    1.单例模式 2.观察者模式 3.委托代理 4.block回调 5.反射机制 单例模式 iOS单例模式的2种方式.根据线程安全的实现来区分,一种是使用@synchronized ,另一种是使用GCD的 ...