zoukankan      html  css  js  c++  java
  • CF 852E Casinos and travel

    题目链接

    (Desccription)
    给定一棵树,John从任意一个点开始,每次走向一个未到达过的点。每个点都可以有或没有赌场,每经过一个赌场心情都会反转,旅行开始前心情很好。
    问有多少种方案使得旅行结束后心情很好。

    (n≤10^5)


    (Solution)
    把题目抽象一下:
    每个点随机一个(0/1)的权值,随机选择一个点作为根,有多少种方案使得根节点到所有叶节点路径上的异或和为(0)

    发现叶节点到根的路径的权值异或和可以由叶节点确定。记叶节点数为(tot)
    1.若所选的根节点不是叶节点,那么除叶节点外的(n-tot)个点的权值可以随便选,方案数为((n-tot) imes 2^{n-tot})
    2.若所选的根节点是叶节点,那么除其他(n-tot-1)个叶节点外的所有的点权值可以随便选,方案数为(tot imes 2^{n-tot+1})

    (ans=(n-tot) imes 2^{n-tot}+tot imes 2^{n-tot+1}=(n+tot) imes 2^{n-tot})

    #include<complex>
    #include<cstdio>
    using namespace std;
    const int mod=1e9+7;
    const int N=1e5+7;
    int n,tot;
    int in[N];
    int qread()
    {
    	int x=0;
    	char ch=getchar();
    	while(ch<'0' || ch>'9')ch=getchar();
    	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x;
    }
    long long Fpow(long long b,int p)
    {
    	long long res=1;
    	for(;p;p>>=1,b=b*b%mod)
    		if(p&1)res=res*b%mod;
    	return res;
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<n;i++)
    		in[qread()]++,in[qread()]++;
    	for(int i=1;i<=n;i++)
    		if(in[i]==1)tot++;
    	printf("%d
    ",(n+tot)*Fpow(2,n-tot)%mod);
    	return 0;
    }
    
  • 相关阅读:
    Kubernetes基础:Pod的详细介绍
    十分钟带你理解Kubernetes核心概念
    kubectl命令行工具用法详解
    GDPR给安全的影响
    开源软件会被云杀死吗 ?
    VMware前路难测,多个厂家群雄逐鹿
    如何实现linux+windows双系统启动
    IT行业——Linux
    i3 窗口管理器使 Linux 更美好
    在 Linux 中使用超级用户权限
  • 原文地址:https://www.cnblogs.com/LeTri/p/10318669.html
Copyright © 2011-2022 走看看