• [NOI Online #3 提高组]优秀子序列


    传送


    这题刚开始看到那么多式子,确实没啥思路。
    但是再仔细想一想会发现挺有意思的。


    因为(b)序列的限制,每一个(b_i)的二进制中的1必定只有他自己有,那么(sum b_i)就是把他们按位与起来。
    我们令(dp[S])表示(sum b_i=S)(b)序列个数,那么答案就是(dp(i) * sum phi(i+1))


    考虑转移,我们枚举(S)(S)的范围是(0)(a)中最大的数所在的二进制位全是1的数。找到另一个数(S'),且(S' & S=0),表示一个符合(b)序列构造方式的另一个数加入到了(b)中。那么转移就是(dp[S | S'] += dp[S] * num[S'])(num[S'])表示数值为(S')(a_i)个数。


    但这么做,(S')并不是很好找,不能保证复杂度。所以我们换一种枚举方法:枚举(S | S'),然后枚举他的子集(X),那么(X)就是原来的(S)(S|S' - X)就是原来的(S')
    不过这么做还要防止重复枚举,那么循环的时候规定(X geqslant S|S'-S)就行啦。


    最后还剩一个尾巴,就是0怎么办,因为(0)可以加到任意序列中,那么如果有(t)个0,就把答案乘以(2^t)

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<queue>
    #include<assert.h>
    #include<ctime>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    #define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e6 + 5;
    const int maxN = 4e5 + 5;
    const int mod = 1e9 + 7;
    In ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), las = ' ';
    	while(!isdigit(ch)) las = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(las == '-') ans = -ans;
    	return ans;
    }
    In void write(ll x)
    {
    	if(x < 0) x = -x, putchar('-');
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
    	freopen(".in", "r", stdin);
    	freopen(".out", "w", stdout);
    #endif
    }
    
    int n, Max = 0;
    
    int prm[maxN], v[maxN], pcnt = 0;
    int phi[maxN];
    In void init()
    {
    	for(int i = 2; i < maxN; ++i)
    	{
    		if(!v[i]) v[i] = i, prm[++pcnt] = i, phi[i] = i - 1;
    		for(int j = 1; j <= pcnt; ++j)
    		{
    			if(prm[j] >= maxN / i || prm[j] > v[i]) break;
    			if(i % prm[j] == 0) phi[i * prm[j]] = phi[i] * prm[j];
    			else phi[i * prm[j]] = phi[i] * phi[prm[j]];
    			v[i * prm[j]] = prm[j];	
    		}
    	}
    }
    
    ll num[maxN], dp[maxN];
    In ll ADD(ll a, ll b) {return a + b > mod ? a + b - mod : a + b;}
    
    int main()
    {
    //	MYFILE();
    	init();
    	n = read();
    	for(int i = 1; i <= n; ++i)
    	{
    		int x = read();
    		num[x]++, Max = max(Max, x);
    	}
    	int s = 0;
    	while((1 << s) < Max) ++s;
    	dp[0] = 1;
    	for(int i = 1; i <= (1 << s); ++i)
    		for(int j = i; j >= i - j; j = (j - 1) & i)
    			dp[i] = ADD(dp[i], dp[i - j] * num[j] % mod);
    	ll ans = 1;
    	for(int i = 1; i <= (1 << s); ++i) ans = ADD(ans, dp[i] * phi[i + 1] % mod);
    	for(int i = 1; i <= num[0]; ++i) ans = (ans << 1) % mod;
    	write(ans), enter;
    	return 0;
    }
    
  • 相关阅读:
    bzoj 1588: [HNOI2002]营业额统计 treap
    Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
    cdoj 851 方老师与素数 bfs
    hdu 5150 Sum Sum Sum 水
    Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和
    POJ 1984 Navigation Nightmare 带全并查集
    POJ 1655 Balancing Act 树的重心
    POJ 3140 Contestants Division 树形DP
    HDU 3586 Information Disturbing 树形DP+二分
    HDU 1561 The more, The Better 树形DP
  • 原文地址:https://www.cnblogs.com/mrclr/p/13839503.html
Copyright © 2020-2023  润新知