CF 852E Casinos and travel

题目链接

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

\(n≤10^5\)


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

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

\(ans=(n-tot)\times 2^{n-tot}+tot\times 2^{n-tot+1}=(n+tot)\times 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",(n+tot)*Fpow(2,n-tot)%mod);
    return 0;
}

转载于:https://www.cnblogs.com/LeTri/p/10318669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值