hdu6125 Free from square 分组背包+状态压缩
/**
题目:hdu6125 Free from square
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6125
题意:
从不大于n的所有正整数中选出至少1个且至多k个使得乘积不包含平方因子,对10^9+7取模。
1≤n,k≤500。
思路:
分组背包+状态压缩
把n个数分成若干组,互斥的放在同一组。 一开始把所有含平方因子的数去除掉,剩下的进行分组。 <sqrt(500)的八个素因子,编成八组,分别为包含2,3,5,7,11,13,17,19素因子的数。 注意包含的数不能重复。
>sqrt(500)的素因子,每一组为包含该素因子的数。
1这个数为一组. 因为其他组可能包含2,3,5,7,11,13,17,19;也就是这八个素因子在其他组也可能出现。 为了判断用状态压缩处理。 dp[k][s]表示选k个,前8个素因子选择状态为s时候的方法数。 dp[k][s] += dp[k-1][s-s1]; (s&s1==s1) memset(dp, 0, sizeof dp);
dp[0][0] = 1; ans = sigma[1<=i<=m]sigma[0<=s<(1<<8)]dp[i][s];
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = ;
int prime[maxn], tot;
int pos[];
vector<P> v[maxn];
int vis[maxn];
LL dp[maxn][<<];
void init()
{
for(int i = ; i < maxn; i++){
if(prime[i]==){
for(int j = i*; j < maxn; j+=i){
prime[j] = ;
}
}
}
tot = ;
for(int i = ; i < maxn; i++){
if(prime[i]==){
prime[++tot] = i;
}
}
for(int i = ; i <= ; i++){
pos[prime[i]] = i-;
}
for(int i = ; i < maxn; i++){
int x = i;
for(int j = ; j <= tot&&prime[j]<=tot; j++){
int cnt = ;
if(x%prime[j]==){
while(x%prime[j]==){
x/=prime[j];
cnt++;
}
if(cnt>){
vis[i] = ; break;
}
}
}
}
v[].push_back(P(,));///表示1这个数。
for(int i = ; i <= tot; i++){
for(int j = prime[i]; j < maxn; j+=prime[i]){
if(vis[j]) continue;
int s = ;
for(int k = ; k <= ; k++){
if(j%prime[k]==){
s |= (<<pos[prime[k]]);
}
}
v[i].push_back(P(j,s));
vis[j] = ;
}
}
for(int i = ; i <= ; i++){
for(int j = prime[i]; j < maxn; j+=prime[i]){
if(vis[j]) continue;
int s = ;
for(int k = ; k <= ; k++){
if(j%prime[k]==){
s |= (<<pos[prime[k]]);
}
}
v[i].push_back(P(j,s));
vis[j] = ;
}
} }
LL solve(int n,int m)
{
ms(dp,);
dp[][] = ;
int len = <<;
for(int i = ; i <= tot; i++){
if(prime[i]>n) break;
for(int j = m; j >= ; j--){
for(int k = ; k < (int)v[i].size(); k++){
if(v[i][k].first>n) continue;
for(int s = ; s < len; s++){
if((s&v[i][k].second)==v[i][k].second){
dp[j][s] = (dp[j][s]+dp[j-][s-v[i][k].second])%mod;
}
}
}
}
}
LL ans = ;
for(int i = ; i <= m; i++)
for(int s = ; s < len; s++)
ans = (ans+dp[i][s]) %mod;
return ans;
}
int main()
{
init();
int T;
int n, k;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&k);
printf("%lld\n",solve(n,k));
} return ;
}
hdu6125 Free from square 分组背包+状态压缩的更多相关文章
- hdu6149 Valley Numer II 分组背包+状态压缩
/** 题目:hdu6149 Valley Numer II 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意: 众所周知,度度熊非常喜欢图. ...
- POJ-2923 Relocation---01背包+状态压缩
题目链接: https://vjudge.net/problem/POJ-2923 题目大意: 有n个货物,给出每个货物的重量,每次用容量为c1,c2的火车运输,问最少需要运送多少次可以将货物运完 思 ...
- POJ 2923 【01背包+状态压缩/状压DP】
题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...
- POJ 2923 Relocation(01背包+状态压缩)
题意:有人要搬家,有两辆车可以运送,有若干家具,车有容量限制,而家具也有体积,那么如何运送会使得运送车次最少?规定两车必须一起走,两车一次来回只算1躺. 思路:家具怎么挑的问题,每趟车有两种可能:1带 ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- HDU 6125 Free from square(状态压缩+分组背包)
http://acm.hdu.edu.cn/showproblem.php?pid=6125 题意: 在${1,2,3,...n}$的数中选择1~k个数,使得它们的乘积不能被平方数整除(1除外),计算 ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...
随机推荐
- 搜集整理一些Cron表达式例子
1.cronExpression配置说明 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / ...
- android.content.res.Resources$NotFoundException: String resource ID #0x0
仔细检查是不是在settext的时候设置进去的时int属性的值,所以android会认为这是在strings中的值,所以会拿着这个int值当做string的id值去找,结果当然是找不到的.
- QtGui.QLineEdit
A QtGui.QLineEdit is a widget that allows to enter and edit a single line of plain text. There are u ...
- JS 运算、判断优化
1.二级制操作优于Math 如: //函数向下取整 console.log(Math.floor(8.9)); //二进制向下取整 console.log(8.9>>0); 2.慎用|| ...
- centos7 yum安装遇到报错:Head V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYer
centos7 yum安装时遇到错误:Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY 无法安装时,可按如下方法解决: This mini ...
- HDU 2647--Reward【拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- ORA-12519:数据的连接池访问过多
今天服务部同事问我一个问题,客户处的报表一半能打开,一半报错如下: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=185599744)(ERR ...
- 局域网Ubuntu与WinXP实现文件共享
时间:2008-11-28 11:27:55 从新立得软件包管理器中安装Samba和Smbfs.Samba是在Unix系统中用于共享文件和打印机的应用软件.Smbfs is a filesyste ...
- Solr 4.0部署
http://www.blogjava.net/xiaohuzi2008/archive/2012/12/03/392373.html
- 树莓派/RaspberryPi 内核源码下载
树莓派的源码有两种下载方式:压缩包下载和git clone指令下载. 1.压缩包下载 选择对应分支,点击Github界面的 下载按钮即可,如下图: 测试发现,同样的分支,用压缩包方式下载后编译会出错, ...