Free from square

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
 
题解:
  n个数
  首先你明白,1~n个数,没有数是包含超过两个 大于根号n 的质因子的,
  小于根号n的质因子只有8个,所以做这个题的思路就有了
  对于只含有小于根号n的 那些质因子的那些数,我们状态压缩DP就好了
  对于包含大于根号n 的 那些质因子的 那些数,我们分组背包, 也就是说 某些包含同一个 大于根号n的 因子 放在一组里边
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e2+, M = 1e3+,inf = 2e9,mod = 1e9+; int p[] = {,,,,,,,};
vector<int > fi,se[N];
int dp[][N][(<<)+],f[N]; int solve(int n,int K) {
fi.clear();
memset(dp,,sizeof(dp));
for(int i = ; i <= n; ++i) se[i].clear(),f[i] = ;
fi.push_back();
for(int i = ; i <= n; ++i) {
int tmp = i,now = ,ok = ;
for(int j = ; j < ; ++j) {
int _ = ;
while(tmp % p[j] == ) _++,now|=(<<j),tmp/=p[j];
if(_ >= ) ok = ;
}
if(ok) {
f[i] = now;
if(tmp!=) se[tmp].push_back(i);
else fi.push_back(i);
}
}
int now = ;
dp[][][] = ;
for(int i = ; i < fi.size(); ++i) { now ^= ;memset(dp[now],,sizeof(dp[now]));
for(int k = ; k <= K; ++k) {
for(int j = ; j < (<<); ++j) { dp[now][k][j] += dp[now^][k][j];
dp[now][k][j] %= mod; if((j&f[fi[i]])) continue; dp[now][k+][j|f[fi[i]]] += dp[now^][k][j];
dp[now][k+][j|f[fi[i]]] %= mod; }
}
} for(int i = ; i <= n; ++i) {
if(se[i].size() == ) continue;
// cout<<"shit"<<endl;
now^=;memset(dp[now],,sizeof(dp[now]));
for(int h = ; h <= K; ++h) {
for(int k = ; k < (<<); ++k) { dp[now][h][k] += dp[now^][h][k];
dp[now][h][k] %= mod; for(int j = ; j < se[i].size(); ++j) {
if((f[se[i][j]]&k)) continue;
dp[now][h+][f[se[i][j]]|k] += dp[now^][h][k];
dp[now][h+][f[se[i][j]]|k] %= mod;
}
}
}
} int ans = ;
for(int i = ; i <= K; ++i) {
for(int j = ; j < (<<); ++j)
ans += dp[now][i][j],ans %= mod;
}
return ans;
} int main() {
int T,n,k;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&k);
printf("%d\n",solve(n,k));
}
return ;
}
/*
2
4 2
6 4
*/

HDU 6125 Free from square 状态压缩DP + 分组背包的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  6. HDU 1074 Doing Homework (状态压缩DP)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

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

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

  8. HDU 1074 Doing Homework ——(状态压缩DP)

    考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. [转]常见linux命令用法介绍

    su switch user 用途:用于用户之间的切换 格式: su - USERNAME切换用户后,同时切换到新用户的工作环境中 su USERNAME切换用户后,不改变原用户的工作目录,及其他环境 ...

  2. POJ-2318 TOYS,暴力+叉积判断!

                                                                 TOYS 2页的提交记录终于搞明白了. 题意:一个盒子由n块挡板分成n+1块区 ...

  3. jenkins换端口号

    两个地方 1,检查 /etc/init.d/jenkins 脚本,修改 do_start 函数的 check_tcp_port 命令,端口号从 8080 换成 8082: 2,修改 /etc/defa ...

  4. 背单词(bzoj 4567)

    Description Lweb 面对如山的英语单词,陷入了深深的沉思,“我怎么样才能快点学完,然后去玩三国杀呢?”.这时候睿智 的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计 ...

  5. STL中heap用法

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]={,, ...

  6. 军训分批(codevs 2751)

    题目描述 Description 某学校即将开展军训.共有N个班级. 前M个优秀班级为了保持学习优势,必须和3位任课老师带的班级同一批. 问共有几批? 输入描述 Input Description N ...

  7. Java游戏服务器搭建

    一.前言 此游戏服务器架构是一个单服的形式,也就是说所有游戏逻辑在一个工程里,没有区分登陆服务器.战斗服务器.世界服务器等.此架构已成功应用在了多款页游服务器 .在此框架中没有实现相关业务逻辑,只有简 ...

  8. Entity Farmework领域建模方式 3种编程方式

    一个业务领域由各个实体和各个相互关联且有格子的属性和行为的实体组成,每个实体都有其状态和验证规则需要维护,Entity Framework (后面简称EF)实体框架设计的出现是为了允许开发人员着重关注 ...

  9. 2017-10-23学大伟业Day1

    T1 叉叉 题目名称 叉叉 程序文件名 cross 输入文件名 cross.in 输出文件名 cross.out 每个测试点时限 1秒 内存限制 128MB 测试点数目 10 每个测试点分值 10 是 ...

  10. Apache Beam 传 大数据杂谈

    1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著名的大数据三篇论文,史称三驾马车:G ...