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. 【JavaScript 8—基础知识点】:DOM

    一.总体概述 1.1,什么是DOM DOM(Document Object Model):D(文档):整个web加载的网页文档:O(对象):类似于window对象之类的东西,可以调用属性和方法,在这里 ...

  2. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  3. Java统计程序运行时间

    代码如下: 第一种是以毫秒为单位计算的. long startTime = System.currentTimeMillis();    //获取开始时间 doSomething();    //测试 ...

  4. bzoj4002 [JLOI2015]有意义的字符串 快速幂

    Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...

  5. linux 查看自己所在的公网ip

    curl members.3322.org/dyndns/getip 其他的方法还有: curl icanhazip.com curl ifconfig.me curl curlmyip.com cu ...

  6. 【shell】shell编程(一)-入门

    如今,不会Linux的程序员都不意思说自己是程序员,而不会shell编程就不能说自己会Linux.说起来似乎shell编程很屌啊,然而不用担心,其实shell编程真的很简单.背景 什么是shell编程 ...

  7. elasticsearch起步

    elasticsearch教程 elastic入门教程 阮一峰的elasticsearch教程 elasticsearch官网 kibana用户手册 elasticsearch安装步骤 参考:http ...

  8. NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]

    我只能说,珍爱生命,远离卡常数的题...感谢陈老师和蔡神,没有他们,,,我调一个星期都弄不出来,,,, 哈罗哈的大披萨 [淡蓝] 时间限制(普通/Java) : 1000 MS/ 3000 MS   ...

  9. git多人协作--分支

    分支: 创建分支: git checkout -b 新分支 切换分支: git checkout 目标分支 删除分支: git branch -d 待删除分支 推送到远程分支: git checkou ...

  10. 湘潭oj1203/邀请赛A题 数论+java大数

    求 n%1+n%2+n%3+n%4+.........n%n=,n<=10^12次. 开始时盲目地找规律,结果一无所获.后来经学长点拨,天资愚钝,搞了半天才明白. 先上图: 对于该题,在求区间( ...