bzoj 2734 [HNOI2012]集合选数 状压DP+预处理
这道题很神啊……
神爆了……
思路大家应该看别的博客已经知道了,但大部分用的插头DP。我加了预处理,没用插头DP,一行一行来,速度还挺快。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 100100
#define M 50
#define yu 1000000001
#define inf 1<<29
using namespace std;
int n;
long long ans=;
long long f[][N/M]={};
int keneng[N/M],kenengnum;
int sushu[N],sushunum; void make_sushu(int limit) // 这里不是筛素数,而是把不是2,3的倍数的筛出来
{ // 如果筛素数,25不是素数,但是5的矩阵不能包含25
int i,j,k;
int vis[N]={};
sushunum=;
for (i=;i<=limit;i++)
if (i%!=&&i%!=)
sushu[++sushunum]=i;
} bool shifou(int now) // 预处理的判断,判断是否有两个1相连
{ // 即是否取连续的两个数
int i,j,k;
i=now&; now>>=;
while (now!=)
{
j=now&;
if (i==j&&i&&j)
return false;
i=j;
now>>=;
}
return true;
} void chuli(int limit) //预处理,把可以选的状态存起来,省时间
{
int i,j,k;
int maxn=;
kenengnum=;
for (i=;i<=limit;i++)
maxn*=;
for (i=;i<maxn;i++)
{
if (shifou(i))
{
keneng[kenengnum++]=i;
}
}
} int pd_long(int x) // 由于矩阵每一行长度不同,所以预处理某些不能用
{ // 这个判断每一行长度
int i=,zanshi=;
while (i<=x)
{
zanshi++;
i*=;
}
return zanshi;
} long long make_juzhen(int now)
{
int i,j,k,x=,y,z;
int limit,lnow,lnowbefore=inf;
limit=pd_long(n/now);
chuli(limit);
f[][]=;
for (i=;x*now<=n;i++,x*=)
{
memset(f[i%],,sizeof(f[i%]));
lnow=pd_long(n/now/x); // 当前行的长度
for (j=;j<kenengnum;j++)
{
if (keneng[j]>>lnow>) //如果有选超过当前行长度的,不选
{
f[i%][j]=;
continue;
}
for (k=;k<kenengnum;k++) // 和上一行比较,是否有相连的1选了
{
if (keneng[k]>>lnowbefore>) // 上一行状态不能超过上一行长度
continue;
if (keneng[j]&keneng[k])
continue;
f[i%][j]+=f[(i+)%][k];
f[i%][j]%=yu;
}
}
lnowbefore=lnow; // 上一行长度
}
return (f[(i+)%][]+f[(i+)%][])%yu; //最后一行一定只有一个,答案只有这两种情况加起来,最后一个选或不选
} int main()
{
int i,j,k,x,y,z;
cin>>n;
make_sushu(n);
for (i=;i<=sushunum;i++)
{
ans*=make_juzhen(sushu[i]);
ans%=yu;
}
cout<<ans<<endl;
}
bzoj 2734 [HNOI2012]集合选数 状压DP+预处理的更多相关文章
- bzoj 2734: [HNOI2012]集合选数 状压DP
2734: [HNOI2012]集合选数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 560 Solved: 321[Submit][Status ...
- BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2734 题解 嗯早就想写的题,昨天因为某些不可告人的原因(大雾)把这题写了,今天再来写题解 ...
- [HNOI2012]集合选数 --- 状压DP
[HNOI2012]集合选数 题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出\({1,2,3,4,5}\)的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x ...
- 洛谷$P3226\ [HNOI2012]$集合选数 状压$dp$
正解:$dp$ 解题报告: 传送门$QwQ$ 考虑列一个横坐标为比值为2的等比数列,纵坐标为比值为3的等比数列的表格.发现每个数要选就等价于它的上下左右不能选. 于是就是个状压$dp$板子了$QwQ$ ...
- $HNOI2012\ $ 集合选数 状压$dp$
\(Des\) 求对于正整数\(n\leq 1e5\),{\(1,2,3,...,n\)}的满足约束条件:"若\(x\)在该子集中,则\(2x\)和\(3x\)不在该子集中."的子 ...
- 【BZOJ-2732】集合选数 状压DP (思路题)
2734: [HNOI2012]集合选数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1070 Solved: 623[Submit][Statu ...
- 【BZOJ-2734】集合选数 状压DP (思路题)
2734: [HNOI2012]集合选数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1070 Solved: 623[Submit][Statu ...
- BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]
传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...
- bzoj 2734: [HNOI2012]集合选数
题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中. 同学们不喜 ...
随机推荐
- Uploadify 3.2 参数属性、事件、方法函数详解
一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...
- Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...
- mysql replication inside
http://keithlan.github.io/2015/07/16/mysql_replication_inside/
- PAT 1013
1013. Battle Over Cities (25) It is vitally important to have all the cities connected by highways i ...
- spring Transaction Management --官方
原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12. ...
- Requirements
Requirements The framework requirements are limited. PHP 5.5 or greater. Apache Web Server or equiva ...
- Internationalization
Internationalization If you are building a site for an international audience, you will likely want ...
- windows7怎么共享文件夹
http://jingyan.baidu.com/article/d45ad148f06fef69552b80e6.html
- View绘制详解(四),谝一谝layout过程
上篇博客我们介绍了View的测量过程,这只是View显示过程的第一步,第二步就是layout了,这个我们一般译作布局,其实就是在View测量完成之后根据View的大小,将其一个一个摆放在ViewGro ...
- CentOS7 发布 ASP.NET MVC 4 --- mono 4.6.0 + jexus 5.8.1
yum -y install gcc gcc-c++ yum -y install bison pkgconfig glib2-devel gettext make libpng-devel libj ...