题意:给一个N个带权节点的树,权值以给定的K个素数为因子,求路径上节点乘积为立方数的路径条数

思路:立方数的性质是每个因子的个数为3的倍数,那么每个因子只需要保存0-2三个状态即可,然后路径就可以转化为一个K位3进制数,点分治后,便可以用一个map来查询路径经过根的答案。代码与上一题(poj1741)类似:http://www.cnblogs.com/jklongint/p/4960052.html

#pragma comment(linker,"/STACK:10240000,10240000")
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <map>
#include <vector>
using namespace std;
#define X first
#define Y second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define mset(a, x) memset(a, x, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(b))
#define cas() int T, cas = 0; cin >> T; while (T --)
template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;}
template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;}
typedef long long ll;
typedef pair<int, int> pii; #ifndef ONLINE_JUDGE
#include "local.h"
#endif const int N = 5e4 + 7;
const int M = N;
const int inf = 1e9 + 7; namespace Edge {
int last[N], to[M << 1], next[M << 1], cntE;
void init() {
cntE = 0;
memset(last, -1, sizeof(last));
}
void addEdge(int u, int v) {
to[cntE] = v;
next[cntE] = last[u];
last[u] = cntE ++;
}
} int n, K; struct Node {
char p[33];
char &operator[] (int x) {
return p[x];
}
ll getNum() {
ll ans = 0;
for (int i = 0; i < K; i ++) {
ans = ans * 3 + p[i];
}
return ans;
}
ll getComplement() {
ll ans = 0;
for (int i = 0; i < K; i ++) {
ans = ans * 3 + (p[i]? 3 - p[i] : 0);
}
return ans;
}
Node operator+ (Node &that) {
Node ans;
for (int i = 0; i < K; i ++) {
ans[i] = p[i] + that[i];
if (ans[i] >= 3) ans[i] -= 3;
}
return ans;
}
Node operator- (Node &that) {
Node ans;
for (int i = 0; i < K; i ++) {
ans[i] = p[i] - that[i];
if (ans[i] < 0) ans[i] += 3;
}
return ans;
}
}; namespace Center {
int root, siz, son[N];
void init() {
siz = inf;
}
void getRoot(int cur, int fa, int total, bool used[]) {
son[cur] = 0;
int buf = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (to != fa && !used[to]) {
getRoot(to, cur, total, used);
son[cur] += son[to] + 1;
buf = max(buf, son[to] + 1);
}
}
buf = max(buf, total - son[cur] - 1);
if (buf < siz || buf == siz && cur < siz) {
siz = buf;
root = cur;
}
}
} bool used[N];
Node r[N]; void getNode(int cur, int fa, Node sum, vector<Node> &vt, bool used[]) {
vt.pb(sum);
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (to != fa && !used[to]) getNode(to, cur, sum + r[to], vt, used);
}
} ll getAns(vector<Node> &vt, Node &s) {
ll ans = 0;
map<ll, int> mp;
for (int i = 0; i < vt.size(); i ++) {
mp[vt[i].getNum()] ++;
ans += mp[(vt[i] - s).getComplement()];
}
return ans;
} ll work(int cur) {
used[cur] = true;
vector<Node> total;
total.push_back(r[cur]);
ll ans = 0;
for (int i = Edge::last[cur]; ~i; i = Edge::next[i]) {
int to = Edge::to[i];
if (!used[to]) {
vector<Node> local;
getNode(to, cur, r[cur] + r[to], local, used);
ans -= getAns(local, r[cur]);
for (int j = 0; j < local.size(); j ++) {
total.push_back(local[j]);
}
Center::init();
Center::getRoot(to, cur, local.size(), used);
ans += work(Center::root);
}
}
return ans += getAns(total, r[cur]);
} ll p[N], a[N]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> n >> K) {
Edge::init();
Center::init();
mset(r, 0);
mset(used, 0);
for (int i = 0; i < K; i ++) {
scanf("%I64d", p + i);
}
for (int i = 1; i <= n; i ++) {
scanf("%I64d", a + i);
for (int j = 0; j < K; j ++) {
ll cur = p[j];
while (a[i] % cur == 0) {
r[i][j] ++;
if (r[i][j] == 3) r[i][j] = 0;
cur *= p[j];
}
}
}
int u, v;
for (int i = 1; i < n; i ++) {
scanf("%d%d", &u, &v);
Edge::addEdge(u, v);
Edge::addEdge(v, u);
}
Center::getRoot(1, 0, n, used);
cout << work(Center::root) << endl;
}
return 0;
}

  

[hdu4670 Cube number on a tree]点分治的更多相关文章

  1. HDU4670 cube number on a tree(点分治+三进制加法)

    The country Tom living in is famous for traveling. Every year, many tourists from all over the world ...

  2. HDU4670 Cube number on a tree 树分治

    人生的第一道树分治,要是早点学我南京赛就不用那么挫了,树分治的思路其实很简单,就是对子树找到一个重心(Centroid),实现重心分解,然后递归的解决分开后的树的子问题,关键是合并,当要合并跨过重心的 ...

  3. 【点分治】【map】【哈希表】hdu4670 Cube number on a tree

    求树上点权积为立方数的路径数. 显然,分解质因数后,若所有的质因子出现的次数都%3==0,则该数是立方数. 于是在模意义下暴力统计即可. 当然,为了不MLE/TLE,我们不能存一个30长度的数组,而要 ...

  4. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  5. HDU 4670 Cube number on a tree ( 树的点分治 )

    题意 : 给你一棵树 . 树的每一个结点都有一个权值 . 问你有多少条路径权值的乘积是一个全然立方数 . 题目中给了你 K 个素数 ( K <= 30 ) , 全部权值都能分解成这k个素数 思路 ...

  6. HDU 4670 Cube number on a tree

    divide and conquer on tree. #include <map> #include <vector> #include <cstdio> #in ...

  7. Square Number & Cube Number

    Square Number: Description In mathematics, a square number is an integer that is the square of an in ...

  8. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

  9. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

随机推荐

  1. Anadi and Domino--codeforces div2

    题目链接:https://codeforces.com/contest/1230/problem/C 题目大意:21枚多米诺牌,给你一个图,将多米诺牌放到图的边上,由同一个点发出的所有边,边上多米诺牌 ...

  2. 多线程高并发编程(6) -- Semaphere、Exchanger源码分析

    一.Semaphere 1.概念 一个计数信号量.在概念上,信号量维持一组许可证.如果有必要,每个acquire()都会阻塞,直到许可证可用,然后才能使用它.每个release()添加许可证,潜在地释 ...

  3. 【原创】Linux RCU原理剖析(二)-渐入佳境

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  4. JS流程图解决方案GoJS

    GoJs简介 一个实现交互类图表(比如流程图,树图,关系图,力导图等等)的JS库 GoJS依赖于HTML5,所以请保证您的浏览器版本支持HTML5,当然还要加载这个库. 首先个人建议先下载官方实例的 ...

  5. c++动态数组的使用

    在c++中,有的时候会遇到变长的数组(不管是一维的还是二维的),这个时候就需要用到动态数组了,并且要用new和delete两个操作符,这俩操作符一般成对使用. 先说一维的动态数组吧,直接上代码 #in ...

  6. php sprintf() 函数把格式化的字符串写入一个变量中。

    来源:https://blog.csdn.net/zxh1220/article/details/79709207 HP sprintf() 函数用到的参数 printf — 输出格式化字符串 spr ...

  7. PSD转HTML

    随着时间的推移,技术的进步,越来越多的人也越来越熟悉HTML和CSS以及不同的技术来将 Photoshop 设计转换成 HTML 或 CSS 格式.对于一个设计师或网页开发人员,知道如何将PSD文件转 ...

  8. 微信jssdk遇到的一些问题汇总

    1.用户手动去触发的接口可以直接调用比如wx.startRecord(); 但是写在页面加载完成里就无效,需要写在 wx.ready(function(){ wx.startRecord(); }); ...

  9. Kubernetes 命令行工具之kubctl

    目录 1.何为kubectl 2.Kubectl基本使用 2.1.命令补全 2.2.快速查找资源 2.3.使用自定义输出格式 3.陈述式管理资源 3.1.管理namespace资源 3.2.管理Dep ...

  10. beetl 模板语法

    如何定义临时变量 @var tmp = false; 如何给临时变量赋值 @tmp = true; 如何在判断中使用临时变量 @if(tmp){ ... @} 如何使用条件语句 if else @if ...