Topcoder SRM 683 Div2 - C
树形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的更多相关文章
- Topcoder SRM 683 Div2 B
贪心的题,从左向右推过去即可 #include <vector> #include <list> #include <map> #include <set&g ...
- Topcoder Srm 673 Div2 1000 BearPermutations2
\(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...
- Topcoder Srm 671 Div2 1000 BearDestroysDiv2
\(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- Topcoder srm 632 div2
脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...
- topcoder SRM 628 DIV2 BracketExpressions
先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...
- topcoder SRM 628 DIV2 BishopMove
题目比较简单. 注意看测试用例2,给的提示 Please note that this is the largest possible return value: whenever there is ...
- Topcoder SRM 626 DIV2 SumOfPower
本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { ; ; ...
- Topcoder SRM 626 DIV2 FixedDiceGameDiv2
典型的条件概率题目. 事件A在另外一个事件B已经发生条件下的发生概率.条件概率表示为P(A|B),读作“在B条件下A的概率”. 若只有两个事件A,B,那么, P(A|B)=P(AB)/P(B) 本题的 ...
随机推荐
- Unity WebGL MoonSharp崩溃问题
当前Unity的代码更新方案基本都选择的ULua,而我们项目还需要考虑Web平台,ULua不支持WebGL,所以决定选择MoonSharp.MoonSharp(http://www.moonsharp ...
- 关于SequeezeNet中的Fire Module
在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...
- MXNET手写体识别的例子
安装完MXNet之后,运行了官网的手写体识别的例子,这个相当于深度学习的Hello world了吧.. http://mxnet.io/tutorials/python/mnist.html 运行的过 ...
- 延时调用的php代码
比如我们想做一个类似于康盛uchome的定时触发任务,任务靠用户访问触发的,但是你触发任务是不能影响用户本身对页面的访问速度(也就是说不能任务执行十秒钟你就让用户等待十秒钟)刚好昨天把这个弄完了.拿出 ...
- sqlserver日志的备份与还原
----------完整备份与还原---------- --完整备份数据库--backup database studb to disk='e:\stu.bak'back ...
- EXC_ARM_DA_ALIGN
ios 版本上的问题 armv7 ipad2 int64 t = *(int64*)pBuff; 如果pBuff不是8字节对齐的地址就 crash 变通的方法是通过memcpy __sync_fe ...
- ios 配置https
一般来讲如果app用了web service , 我们需要防止数据嗅探来保证数据安全.通常的做法是用ssl来连接以防止数据抓包和嗅探 其实这么做的话还是不够的 . 我们还需要防止中间人攻击(不明白的自 ...
- Design and Analysis of Algorithms_Brute Froce
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- python numpy包
在numpy包中我们可以用数组来表示向量,矩阵和高阶数据结构 首先导入numpy包: from numpy import* 初始化numpy数组有多种方式,比如说 1.python列表或元祖 2.使用 ...
- centos查看实时网络带宽占用情况方法
Linux中查看网卡流量工具有iptraf.iftop以及nethogs等,iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等. centos安装iftop的命令如下: ...