当前位置: 移动技术网 > IT编程>开发语言>C/C++ > bzoj2734【HNOI2012】集合选数

bzoj2734【HNOI2012】集合选数

2018年03月13日  | 移动技术网IT编程  | 我要评论

掌跖角化症王根会,深圳旅行社招聘,生物质颗粒机定制方案

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

状压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) if="" int="" ll="" n="read();" p="1;p<num[cnt];p++)" pre="" return="" t="0;"><p>
</p>
   
</num[cnt])-1)></algorithm></cstdlib></cmath></cstring></cstdio></iostream>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网