1. 程式人生 > >bzoj2734【HNOI2012】集合選數

bzoj2734【HNOI2012】集合選數

pan calc lib break memset 圖論 一道 mod algorithm

2734: [HNOI2012]集合選數

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 831 Solved: 487
[Submit][Status][Discuss]

Description

《集合論與圖論》這門課程有一道作業題,要求同學們求出{1, 2, 3, 4, 5}的全部滿足以 下條件的子集:若 x 在該子集中,則 2x 和 3x 不能在該子集中。

同學們不喜歡這樣的具有枚舉性 質的題目。於是把它變成了下面問題:對於隨意一個正整數 n≤100000,怎樣求出{1, 2,..., n} 的滿足上述約束條件的子集的個數(僅僅需輸出對 1,000,000,001 取模的結果),如今這個問題就 交給你了。

Input

僅僅有一行,當中有一個正整數 n。30%的數據滿足 n≤20。

Output


僅包括一個正整數。表示{1, 2,..., n}有多少個滿足上述約束條件 的子集。



Sample Input


4

Sample Output

8

【例子解釋】

有8 個集合滿足要求,各自是空集,{1},{1。4}。{2}。{2,3},{3}。{3,4},{4}。

HINT

Source

search=day2" style="color:blue; text-decoration:none">day2




狀壓DP思路好題

寫出這樣一個矩陣

1 3 9 27 …

2 6 18 54 …

4 12 36 108 …

能夠發現最多有12行。

這樣我們僅僅要枚舉左上角的數x,就能夠得到一個不同的矩陣。對於每個矩陣須要選一些數,但不能選相鄰的數,狀壓DP解決。

對於每個矩陣,把方案數相乘即為答案。




#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 100005
#define mod 1000000001
using namespace std;
int n;
ll ans=1,f[20][4100],num[20];
bool vst[maxn];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll calc(int x)
{
	int cnt=0;
	memset(num,0,sizeof(num));
	memset(f,0,sizeof(f));
	f[0][0]=1;
	while (x<=n)
	{
		cnt++;
		int tmp=x;
		while (tmp<=n)
		{
			num[cnt]++;
			vst[tmp]=true;
			tmp*=3;
		}
		F(i,0,(1<<num[cnt])-1)
		{
			int p;
			for(p=1;p<num[cnt];p++) if ((i&(1<<(p-1)))&&(i&(1<<p))) break;
			if (p!=num[cnt]) continue;
			F(j,0,(1<<num[cnt-1])-1) if (!(i&j)) (f[cnt][i]+=f[cnt-1][j])%=mod;
		}
		x*=2;
	}
	ll t=0;
	F(i,0,(1<<num[cnt])-1) (t+=f[cnt][i])%=mod;
	return t;
}
int main()
{
	memset(vst,false,sizeof(vst));
	n=read();
	F(i,1,n) if (!vst[i]) (ans*=calc(i))%=mod;
	printf("%lld\n",ans);
	return 0;
}


bzoj2734【HNOI2012】集合選數