codeforces 715c
题目大意:
给定一个有N个点的树,问其中有多少条路径满足他们的边权连成的数对M取余为0。
其中gcd(M,10)=1。
题解:
很亲民的点分治题目,对每一层点分治,预处理每个点到当前根的数字并对m取余,和当前根到每个点的数字取余。
我们得到公式:x1*10dep2+x2=0 mod(m)
dep2指终点的深度,x2指从根到终点数字,通过exgcd即可算出,若想使结果mod m=0,则出发点到根的数字必须是x1。
从公式还可以看出,若枚举出发点,我还需要知道终点的dep,不好做。
所以解法就出来了,先将所有出发点的值丢进一个map(或hash),然后枚举每个子树,先将该子树的出发点值去掉,然后根据终点的值和map计算答案,最后再将该子树出发点的值加进去。
这样可以保证计算答案的不重不漏。还有就是注意根节点的处理。
时间cf上1s2,如果map改hash,应该能更快一些。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<map>
#include<queue>
using namespace std;
#define mem1(i,j) memset(i,j,sizeof(i))
#define mem2(i,j) memcpy(i,j,sizeof(i))
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);++i)
#define down(i,n,j) for(int i=n;i>=j;--i)
#define Auto(i,x) for(int i=linkk[x];i;i=e[i].next)
#define FILE "dealing"
#define poi vec
#define db double
#define eps 1e-10
#define mid ((l+r)>>1)
const int maxn=101000,inf=1000000000;
int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0',ch=getchar();}
return f*x;
}
inline bool cmax(int& a,int b){return a<b?a=b,true:false;}
inline bool cmin(int& a,int b){return a>b?a=b,true:false;}
LL n,m,fac[maxn],ans=0;
void exgcd(LL a,LL b,LL& d,LL& x,LL& y){
if(b==0){x=1,y=0,d=a;return;}
exgcd(b,a%b,d,x,y);
LL t=x;x=y;y=t-a/b*x;
}
struct node{int y,next,v;}e[maxn<<1];int len=0,linkk[maxn<<1];
void insert(int x,int y,int v){e[++len].y=y;e[len].v=v;;e[len].next=linkk[x];linkk[x]=len;}
int siz[maxn],fa[maxn],root,q[maxn],vis[maxn],Max[maxn],Min=0,dep[maxn],head,tail;
bool b[maxn];LL w[maxn],r[maxn];
void getsize(int x){
head=tail=0;q[++tail]=x;Min=inf;
while(++head<=tail){
int x=q[head];b[x]=1;Max[x]=0;
siz[x]=1;
for(int i=linkk[x];i;i=e[i].next)
if(!b[e[i].y]&&!vis[e[i].y])fa[e[i].y]=x,q[++tail]=e[i].y;
}
down(j,tail,1){
int x=q[j];
for(int i=linkk[x];i;i=e[i].next){
if(e[i].y==fa[x]||vis[e[i].y])continue;
siz[x]+=siz[e[i].y];
cmax(Max[x],siz[e[i].y]);
}
cmax(Max[x],tail-siz[x]);
if(cmin(Min,Max[x]))root=x;//求出根节点为root
}
up(i,1,tail)b[q[i]]=0;
}
map<int,int> t;//一只桶
void getans(LL x1,LL x2,LL c){
LL d,x,y;
exgcd(x1,x2,d,x,y);
x=(x*c/d%m+m)%m;
if(t.count(x))ans+=t[x];
}
void add(int x,int num){
head=tail=0;q[++tail]=x;r[x]=w[x]=num;dep[x]=1;
while(++head<=tail){
int x=q[head];b[x]=1;
if(t.count(r[x]))t[r[x]]++;
else t[r[x]]=1;
for(int i=linkk[x];i;i=e[i].next){
if(b[e[i].y]||vis[e[i].y])continue;
dep[e[i].y]=dep[x]+1;
w[e[i].y]=(w[x]*10+e[i].v)%m;//接收端
r[e[i].y]=(r[x]+fac[dep[x]]*e[i].v)%m;//发出端
q[++tail]=e[i].y;
}
}
up(i,1,tail)b[q[i]]=0;
}
void cal(int x,int num){
head=tail=0;q[++tail]=x;r[x]=w[x]=num;dep[x]=1;
while(++head<=tail){
int x=q[head];b[x]=1;t[r[x]]--;
for(int i=linkk[x];i;i=e[i].next){
if(b[e[i].y]||vis[e[i].y])continue;
q[++tail]=e[i].y;
}
}
up(i,1,tail)b[q[i]]=0,getans(-fac[dep[q[i]]],m,w[q[i]]);
up(i,1,tail)t[r[q[i]]]++;
}
void solve(int rt){
getsize(rt);
t.clear();vis[root]=1;b[root]=1;
t[0]=1;
for(int i=linkk[root];i;i=e[i].next){
if(vis[e[i].y])continue;
add(e[i].y,e[i].v%m);
}
for(int i=linkk[root];i;i=e[i].next){
if(vis[e[i].y])continue;
cal(e[i].y,e[i].v%m);
}
ans+=t[0]-1;
for(int i=linkk[root];i;i=e[i].next){
if(vis[e[i].y])continue;
solve(e[i].y);
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(),m=read();
fac[0]=1;up(i,1,n)fac[i]=fac[i-1]*10%m;
up(i,1,n-1){
int x=read()+1,y=read()+1,v=read();
insert(x,y,v);insert(y,x,v);
}
solve(1);
cout<<ans<<endl;
return 0;
}
codeforces 715c的更多相关文章
- [Codeforces 715C] Digit Tree
[题目链接] https://codeforces.com/contest/715/problem/C [算法] 考虑点分治 一条路径(x , y)合法当且仅当 : d(x) * 10 ^ dep(x ...
- 【Codeforces 715C】Digit Tree(点分治)
Description 程序员 ZS 有一棵树,它可以表示为 \(n\) 个顶点的无向连通图,顶点编号从 \(0\) 到 \(n-1\),它们之间有 \(n-1\) 条边.每条边上都有一个非零的数字. ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- Boost Asio介绍--之一
原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39 CSDN博客 原文 http:/ ...
- 具体一些的博弈论 sqrstone
Description 你有n个盒子用来放石头,每个盒子都有最大容量与初始的石头数, 两个人轮流放石头,每次必须选择一个盒子往里放数量不超过当前盒子中石头数的平方的石头 比如一个盒子当前有3个石头,你 ...
- 《SQL Server 监控和诊断》
http://jimshu.blog.51cto.com/ http://www.mssqlmct.cn/
- eclipse中通过search打开第二个文件时 第一个文件就自己关闭了
原文:http://blog.csdn.net/u014079773/article/details/66971053 问题:eclipse中通过search打开第二个文件时第一个文件就自己关闭了 问 ...
- Unity -- 入门教程三
进入这个页面,按编译器版本进行下载,我用的是2010,所以要下载这个. 安装就不用我教了,下面开始看我是如何导入Unity VS的. 点击Import之后我们会发现并没有发生什么,但是接下来我们按一下 ...
- gulp安装+一个超简单入门小demo
gulp安装參考.gulp安装參考2. 一.NPM npm是node.js的包管理工具.主要功能是管理.更新.搜索.公布node的包. Gulp是通过npm安装的. 所以首先,须要安装node.js. ...
- SilverLight:基础控件使用(1)
ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...
- linux下脚本监控网络流量
linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...
- vue2.0 + vux (四)Home页
1.综合页(首页) Home.vue <!-- 首页 --> <template> <div> <!-- 顶部 标题栏 --> <app-head ...
- poj 1651 Multiplication Puzzle【区间DP】
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...