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. 九度oj 题目1369:字符串的排列

    题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...

  2. Python之FTP传输

    访问FTP,无非两件事情:upload和download,最近在项目中需要从ftp下载大量文件,然后我就试着去实验自己的ftp操作类,如下(PS:此段有问题,别复制使用,可以参考去试验自己的ftp类! ...

  3. 性能学习之--loaderunner中run-time setting常用功能

    先打开run-time setting界面 一.Run Logic   设置迭代次数,只控制action的迭代次数,init和end只执行一次. 如果迭代次数设置10,10个并发用户,那么init和e ...

  4. 对拍程序(Win)

    代码如下: @echo off :again rand.exe echo "rand finish" asd.exe echo "1.exe finish" 未 ...

  5. 如何将文件上传到ftp

    方法1(推荐,炒鸡简单):双击我的电脑,在地址栏里输入你的ftp地址回车(比如: ftp://220.103.86.96),然后会弹出一个输入登录账号和密码的对话框,输入你的ftp账号和密码回车便进入 ...

  6. Mac快速查看隐藏文件

    使用终端 显示隐藏文件的最简单方法是使用终端.只要打开终端(位于应用程序--实用工具),将以下代码复制进去然后回车 defaults write com.apple.finder AppleShowA ...

  7. c字符和字符数组/字符串

    一维和二维的都可以:一维的情况如下:1,char string0[10];2,char string1[]="prison break";3,char string2[100]=& ...

  8. jfinal使用idea启动 访问报404 action not found

    公司一个项目,在eclipse里面启动正常,换到idea里面启动后,启动没有报错,但是访问的时候会提示404 action not found. 百度了很多种解决方法 都没有解决. 今天脑子一转,想到 ...

  9. 《Java虚拟机原理图解》 1.2、class文件中的常量池

    了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...

  10. HDD磁盘,非4K无以致远

    机械硬盘的未来要靠高容量作为依托,在财报中,希捷表示未来18个月内它们将推出14和16TB机械硬盘,而2020年20TB机械硬盘就将诞生.也有资料显示,3.5英寸100TB硬盘大概在2025年就能面世 ...