题目链接

Problem Description
There is a set including all positive integers that are not more then n. HazelFan wants to choose some integers from this set, satisfying: 1. The number of integers chosen is at least 1 and at most k. 2. The product of integers chosen is 'free from square', which means it is divisible by no square number other than 1. Now please tell him how many ways are there to choose integers, module 10^9+7.
 
Input
The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
A single line contains two positive integers n,k(1≤n,k≤500).
 
Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
 
Sample Input
2
4 2
6 4
 
Sample Output
6
19

题意:从1~n中任意取1~K个数(同一个数不能用多次),这些数的乘积不能被任意一个数的平方整除(除了 1 ),求有多少种取法?

思路:可以从以上题意分析出,取的多个数不能有相同的质数因子。由于n<=500,所以一个数小于sqrt(n)的质数因子可能有多个,但大于sqrt(n)的质数因子只可能有一个。而小于sqrt(n)的质数有2 、3、5、7、11、13、17、19,一共8个,所以对这8个质数因子进行状压。然后就是背包DP,因为成绩不能含有 质数因子的平方,所以需要进行分组,将含有相同大于sqrt(n)的数放在一组,保证这样的数只能每次取一个,也就是分组背包。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int mod=1e9+;
const int N=;
int dp[N][];
int r[N],st[N];
int p[]={,,,,,,,};
vector<int>v[N]; int main()
{
int T; cin>>T;
while(T--)
{
int n,m; scanf("%d%d",&n,&m);
for(int i=;i<N;i++)
{
v[i].clear();
r[i]=i;
st[i]=;
}
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<;j++)
{
if(i%p[j]== && i%(p[j]*p[j])!=) st[i]|=<<j,r[i]/=p[j];
else if(i%(p[j]*p[j])==){
st[i]=-; break;
}
}
}
for(int i=;i<=n;i++)
{
if(st[i]==-) continue;
if(r[i]==) v[i].push_back(i);
else v[r[i]].push_back(i);
}
// for(int i=1;i<=n;i++)
// {
// for(int j=0;j<v[i].size();j++)
// cout<<v[i][j]<<" ";
// cout<<endl;
// }
for(int i=;i<=n;i++)
{
if(st[i]==- || v[i].size()==) continue;
for(int j=m-;j>=;j--)
{
for(int s=;s<;s++)
{
for(int k=;k<v[i].size();k++)
{
int d=st[v[i][k]];
if((s&d)!=) continue;
dp[j+][s|d]=(dp[j+][s|d]+dp[j][s])%mod;
}
}
}
}
int ans=;
for(int i=;i<=m;i++)
{
for(int j=;j<;j++)
ans=(ans+dp[i][j])%mod;
}
printf("%d\n",ans);
}
return ;
}

hdu 6125 -- Free from square(状态压缩+分组背包)的更多相关文章

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

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

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

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

  3. HDU 1170 Shopping Offers 离散+状态压缩+完全背包

    题目链接: http://poj.org/problem?id=1170 Shopping Offers Time Limit: 1000MSMemory Limit: 10000K 问题描述 In ...

  4. HDU 6125 Free from square (状压DP+背包)

    题意:问你从 1 - n 至多选 m 个数使得他们的乘积不能整除完全平方数. 析:首先不能整除完全平方数,那么选的数肯定不能是完全平方数,然后选择的数也不能相同的质因子. 对于1-500有的质因子至多 ...

  5. HDU 6125 - Free from square | 2017 Multi-University Training Contest 7

    思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...

  6. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  7. 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理 状态压缩dp+背包dp

    题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...

  8. HDU 6125 Free from square (状压DP+分组背包)

    题目大意:让你在1~n中选择不多于k个数(n,k<=500),保证它们的乘积不能被平方数整除.求选择的方案数 因为质数的平方在500以内的只有8个,所以我们考虑状压 先找出在n以内所有平方数小于 ...

  9. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

随机推荐

  1. Java开发从初级到中级

    本人正统软件工程专业毕业,虽然实力垫底,但是大学的时候,整个学校非常热衷于OJ,ACM之类,耳濡目染,自以为基础的知识是有的.但是 一直觉得学的东西都是一团浆糊,按照老师的话说,我是那种看书只看目录, ...

  2. java IO之 编码 (码表 编码 解码 转换流)

    编码 什么是编码? 计算机中存储的都是二进制,但是要显示的时候,就是我们看到的却可以有中国 ,a  1 等字符 计算机中是没有存储字符的,但是我们却看到了.计算机在存储这些信息的时候,根据一个有规 则 ...

  3. 利用powershell反弹shell到metasploit

    一.使用msfvenom生成PS1文件: msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST= -f psh-reflection >.p ...

  4. 集合用法笔记-Map用法

    一.Map遍历 Map<String, String> map = new HashMap<String, String>(); map.put("1", ...

  5. CSS3-loading动画(五)

    CSS3-loading加载动画 在线示例demo:http://liyunpei.xyz/loading.html 之前发了四篇,二十二个效果,今天再分享六个效果,总计二十八个效果. 二十三.效果二 ...

  6. Vulkan Tutorial 29 Loading models

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 应用程序现在已经可以渲染纹理3D模型,但是 vertice ...

  7. .net 自动摘要等算法 HanLP.net

    参考资料: http://www.hankcs.com/nlp/call-hanlp-in-csharp.html 目前自动摘要算法似乎没有.net 版本,而以java,python 居多 自动摘要算 ...

  8. CSS学习笔记一:css 画平面图形

    最近在学习CSS,先从CSS画点平面图形入手,发现除了正方形.长方形此类比较简单,只要有长宽设置恰当即可,画圆要涉及radius,然后恢复到做界面的最讨厌的状态了,不断的修改设值,调整数据,所幸并不多 ...

  9. nopCommerce 3.9 大波浪系列 之 路由扩展 [多语言Seo的实现]

    一.nop种的路由注册 在Global.asax,Application_Start()方法中会进行路由注册,代码如下. public static void RegisterRoutes(Route ...

  10. 移动端JS事件、移动端框架

    一.移动端的操作方式和PC端是不同的,移动端主要是用手指操作,所以有特殊的touch事件,touch事件包括如下几个事件: 1.手指放到屏幕上时触发   touchstart 2.手指放在屏幕上滑动式 ...