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 ...
随机推荐
- Oracle PL/SQL块 多表查询(emp员工表、dept部门表、salgrade工资等级表)
范例: 查询每个员工的编号,姓名,职位,工资,工资等级,部门名称 ●确定要使用的数据表 |- emp表:员工的编号.姓名.职位.工资 |- salgrade表:工资等级 |- dept表:部门名称 ● ...
- Java中Javadoc的{@link}与@see的简单区别
{@link}与@see这两个Javadoc注解都可以直接链接类和方法.用法基本一致. 但是@see必须顶头写,而{@link可以任意地方},如下所示: 参考: http://blog.csdn.ne ...
- CentOS 笔记
对安装CentOS安装使用过程中的问题做一个笔记,第一次安装,安装的是7.0版本,最小化安装. 安装环境 :Windows 2012 R2 Standard,Hyper-V Virstual Mach ...
- android -- 存储byte
public static String byteArrayToHexStr(byte[] byteArray) { if (byteArray == null){ return null; } ch ...
- linux删除空行操作:awk、grep、tr、sed
如下:如何删除空行 shen\nshen\\n sen seh sehe she she 真正删除空行,可以使用vim: 通过命令模式删除空行.vim在命令模式下(在vim里输入英文字符:进入命令模式 ...
- [转] RabbitMQ介绍
转自: http://lynnkong.iteye.com/blog/1699684 1 什么是RabbitMQ? RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初 ...
- 关于SharePoint 讨论板的一些知识
关于SharePoint 讨论板的一些知识 近期公司项目可能要用到讨论板.需求是这种: 怎样在回复中仅仅让查看登陆者和讨论主题公布者的信息. 比方我公布 ...
- angular 资源路径问题
1.templateUrl .component("noData",{ templateUrl:"components/noData.html" // 注意相对 ...
- PHP工作模型与运行机制
PHP的工作模型非常特殊.从某种程度上说,PHP和ASP.ASP.NET.JSP/Servlet等流行的Web技术,有着本质上的区别. 以Java为例,Java在Web应用领域,有两种技术:Jav ...
- python正则中的贪婪与非贪婪
当重复一个正则表达式时,如用 a*,操作结果是尽可能多地匹配模式.当你试着匹配一对对称的定界符,如 HTML 标志中的尖括号.匹配单个 HTML 标志的模式不能正常工作,因为 .* 的本质是“贪婪”的 ...