/**
题目: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 分组背包+状态压缩的更多相关文章

  1. hdu6149 Valley Numer II 分组背包+状态压缩

    /** 题目:hdu6149 Valley Numer II 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意: 众所周知,度度熊非常喜欢图. ...

  2. POJ-2923 Relocation---01背包+状态压缩

    题目链接: https://vjudge.net/problem/POJ-2923 题目大意: 有n个货物,给出每个货物的重量,每次用容量为c1,c2的火车运输,问最少需要运送多少次可以将货物运完 思 ...

  3. POJ 2923 【01背包+状态压缩/状压DP】

    题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...

  4. POJ 2923 Relocation(01背包+状态压缩)

    题意:有人要搬家,有两辆车可以运送,有若干家具,车有容量限制,而家具也有体积,那么如何运送会使得运送车次最少?规定两车必须一起走,两车一次来回只算1躺. 思路:家具怎么挑的问题,每趟车有两种可能:1带 ...

  5. HDU 6125 Free from square 状态压缩DP + 分组背包

    Free from square Problem Description There is a set including all positive integers that are not mor ...

  6. hdu 6125 -- Free from square(状态压缩+分组背包)

    题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...

  7. HDU 6125 Free from square(状态压缩+分组背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=6125 题意: 在${1,2,3,...n}$的数中选择1~k个数,使得它们的乘积不能被平方数整除(1除外),计算 ...

  8. POJ 2923 Relocation (状态压缩,01背包)

    题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...

  9. POJ 2923 Relocation(01背包变形, 状态压缩DP)

    Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...

随机推荐

  1. Android倒计时案例展示

    1. Handler 与Message方法实现倒计时功能 关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析 这个设计思路也是最经常 ...

  2. 如何用 js 递归输出树型

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. Hibernate中load与get,update与merge方法的区别

    1.load()与get()的区别: (1)load()读取 User user = (User)session.load(User.class, userId); (2)get()读取 User u ...

  4. 从错误中学python(2)————字符串转浮点数

    题目 自己编写函数:利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: 题目来源--致敬廖雪峰 解决过程 初步的解决方法 def str2f ...

  5. 搭建ssm+maven环境遇到的问题

    1. @RunWith(SpringJUnit4ClassRunner.class) gives error 在使用junit测试过程中 2.com.google.gson.JsonIOExcepti ...

  6. 父元素没有设置定位 position absolute 解析

    1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...

  7. 查看 apache、nginx、php、mysql 的编译参数

    查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...

  8. win7编程接口的一些变化

    原文链接:http://www.nirsoft.net/articles/windows_7_kernel_architecture_changes.html Windows 7 introduces ...

  9. c# 隐藏Tab控件的标签

    public void HideTabcontrolLabel(TabControl tabControl1) { tabControl1.Appearance = TabAppearance.Fla ...

  10. 解决Enter键与input 、a标签触发的事件的冲突

    无论是 <button type="button" onclick="console.log('123');">123</button> ...