https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1632&judgeId=593084
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
B国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条交通线将其连接。A国是B国的敌国企图秘密发射导弹打击B国的交通线,现假设每条交通线都有50%的概率被炸毁,B国希望知道在被炸毁之后,剩下联通块的个数的期望是多少?
Input
一个数n(2<=n<=100000) 接下来n-1行,每行两个数x,y表示一条交通线。(1<=x,y<=n) 数据保证其交通系统构成一棵树。
Output
一行一个数,表示答案乘2^(n-1)后对1,000,000,007取模后的值。
Input示例
3 1 2 1 3
Output示例
8
期望都快忘了了,像他有n个点,最多被炸(n-1)条线,每次被炸的概率(1/2)^(n-1) 而结果乘了2^(n-1), 期望就是:1+2*C(n-1,1)+3*C(n-1,2).....n*C(n-1,n-1); 乘号前面表示的是能炸出的板块数,而后面的是(n-1)条边选中 (板块数-1)的概率。
然后就卡到这里了.........,然后在网上看到把n带进去,可以求出通项式 :a[1]=1 a[i]=a[i-1]*2+pow(2,i-2);
#include<iostream>
#define ll long long
#define mod 1000000007
using namespace std;
ll a[100005];
int pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b/=2;
}
return ans;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
}
a[1]=1;
for(ll i=2;i<=n;i++)
{
a[i]=a[i-1]*2+pow(2,i-2);
a[i]%=mod;
}
cout<<a[n]<<endl;
return 0;
}