HDU 6285 (图论+计数)

题意:
在一个n个点的完全图中,第i个点的权值为2^i,选择一些边,需要选择一些点使得所有边至少有一个端点被覆盖,同时权值之和最小。
在上述情况下,给出选择的点的权值和,问有多少种选择边的方案符合这种选点。

思路:
按照权值从大到小枚举每个点,当一个点被选中时,因为贪心的做法,这个点一定是某一条边中的权值较小的点。
所以这个点至少和之前没有选择的点中的一个有一条边,即有(2^(cnt0))-1种方案,(2^(cnt0))是每个没有选中的点可以连边和不连边,-1是减去了全部都不连的情况。
同时,这个选中的点可以和权值比它小的点任意连边,即有(2^(i-1))种方案。
每种方案的乘积就是种的方案数。

代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double eps=1e-8;
const int maxn=100005;
const int maxm=10005;

int n;
char str[maxn];
ll pre[maxn];
void solve() {
    pre[0]=1;
    for(int i=1;i<maxn;i++){
        pre[i]=(pre[i-1]<<1)%mod;
    }
    while(~sd(n)) {
        scanf("%s",str);
        int len=strlen(str);
        ll ans=1;
        int cnt=0;
        int lim=len>>1;
        for(int i=0;i<lim;i++){
            swap(str[i],str[len-i-1]);
        }
        for(int i=n-1;i>=0;i--){
            if(i>=len||str[i]=='0'){
                cnt++;
            }else{
                ans=ans*(pre[cnt]-1+mod)%mod*pre[i]%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return ;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#else
    //    freopen("","r",stdin);
    //    freopen("","w",stdout);
#endif
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38378637/article/details/81368011