题目链接

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)的数放在一组,保证这样的数只能每次取一个,也就是分组背包。

代码如下:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <vector>
  6. using namespace std;
  7. const int mod=1e9+;
  8. const int N=;
  9. int dp[N][];
  10. int r[N],st[N];
  11. int p[]={,,,,,,,};
  12. vector<int>v[N];
  13.  
  14. int main()
  15. {
  16. int T; cin>>T;
  17. while(T--)
  18. {
  19. int n,m; scanf("%d%d",&n,&m);
  20. for(int i=;i<N;i++)
  21. {
  22. v[i].clear();
  23. r[i]=i;
  24. st[i]=;
  25. }
  26. memset(dp,,sizeof(dp));
  27. dp[][]=;
  28. for(int i=;i<=n;i++)
  29. {
  30. for(int j=;j<;j++)
  31. {
  32. if(i%p[j]== && i%(p[j]*p[j])!=) st[i]|=<<j,r[i]/=p[j];
  33. else if(i%(p[j]*p[j])==){
  34. st[i]=-; break;
  35. }
  36. }
  37. }
  38. for(int i=;i<=n;i++)
  39. {
  40. if(st[i]==-) continue;
  41. if(r[i]==) v[i].push_back(i);
  42. else v[r[i]].push_back(i);
  43. }
  44. // for(int i=1;i<=n;i++)
  45. // {
  46. // for(int j=0;j<v[i].size();j++)
  47. // cout<<v[i][j]<<" ";
  48. // cout<<endl;
  49. // }
  50. for(int i=;i<=n;i++)
  51. {
  52. if(st[i]==- || v[i].size()==) continue;
  53. for(int j=m-;j>=;j--)
  54. {
  55. for(int s=;s<;s++)
  56. {
  57. for(int k=;k<v[i].size();k++)
  58. {
  59. int d=st[v[i][k]];
  60. if((s&d)!=) continue;
  61. dp[j+][s|d]=(dp[j+][s|d]+dp[j][s])%mod;
  62. }
  63. }
  64. }
  65. }
  66. int ans=;
  67. for(int i=;i<=m;i++)
  68. {
  69. for(int j=;j<;j++)
  70. ans=(ans+dp[i][j])%mod;
  71. }
  72. printf("%d\n",ans);
  73. }
  74. return ;
  75. }

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. JavaScript中闭包实现的私有属性的getter()和setter()方法

    注意: 以下的输出都在浏览器的控制台中 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  2. LInux基础命令分类

    1. 命令的概念 命令的执行过程 系统第一次执行外部命令时Hash缓存表为空,系统会先从PTAH路径下寻找命令,找到后会将路径加入到Hasa缓存中,当再次执行此命令时会直接从Hash的路径下执行,如果 ...

  3. 使用软件开发的部分思想,帮助HR处理Excel。

    前言 上周末,XX给我抱怨:因为计算绩效奖金,把2个人的工资发错了,还被扣了500元.问的缘由得知,她每个月要处理十来个excel表格,每次都要手动修改里面的值,如果修改了一处,其他地方也要修改,然后 ...

  4. Android 自定义 permission

    Android 自定义 permission Android 添加自定义权限 permission-tree 权限的根节点,3个成员都要定义 name 一般来说需要2个".":比如 ...

  5. nyoj_7:街区最短路径问题

    做这题时,先假设目标点在某个位置,然后对其稍微移动dx,dy,分析对ans的影响.最终得,选点时,使一半的横坐标比目标点横坐标小,一半的纵坐标比目标点小,这样得到的ans最小. 题目链接: http: ...

  6. Elasticsearch,Kibana,Logstash,NLog实现ASP.NET Core 分布式日志系统

    Elasticsearch - 简介 Elasticsearch 作为核心的部分,是一个具有强大索引功能的文档存储库,并且可以通过 REST API 来搜索数据.它使用 Java 编写,基于 Apac ...

  7. selenium3.x 踏坑记

    Selenium 3.x 出来也有段时间了,有哪些坑呢? 有好长一段时间没有用selenium了.最近想用来做个web自动化的小工具.根据以往经验,firefox是不需要下载driver的.启动fir ...

  8. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  9. PHP通过phpmailer批量发送邮件功能

    前端页面代码: 注意:目前发送人使用的qq邮箱支持的不是特别友好.建议使用网易 新浪 163等其他邮箱. 需要用到phpmailer包 下载地址:https://sourceforge.net/pro ...

  10. 14. leetcode 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...