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. BZOJ 3196 二逼平衡树 ——树套树

    [题目分析] 全靠运气,卡空间. xjb试几次就过了. [代码] #include <cmath> #include <cstdio> #include <cstring ...

  2. 刷题总结——学姐的逛街计划(vijos1891费用流)

    题目: doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪学姐去逛街呢. 后来, ...

  3. CodeForces 762D Maximum path

    http://codeforces.com/problemset/problem/762/D 因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步 并走完一列来替换 那么走的方法就大 ...

  4. POJ2486 Apple Tree

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Wshxzt is ...

  5. MySQL 游戏排行榜

    今天在坛子上看到了,顺便写下来. 有两种方法: 1.效率不高,因为有子查询.但是简洁.而且我对SOCRES表做了INDEX.所以性能上也差不了多少. mysql> show create tab ...

  6. 有关WebView开发问题(转)

    http://blog.sina.com.cn/s/blog_8241e8510101btvk.html 如何创建WebView: 1.添加权限:AndroidManifest.xml中必须使用许可& ...

  7. Currency Exchange(最短路)

    poj—— 1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29851   Ac ...

  8. Java并发编程关键字synchronized的总结

    一.对synchronized的了解 synchronized关键字解决的是多个线程之间访问资源的同步性,synchronized关键字可以保证被它修饰的方法或者代码块在任意时刻只能有一个线程执行. ...

  9. Nginx阻止DDoS攻击的教程收集(转)(待实践)

    DDoS估计是一个非常头痛的问题. 分布式拒绝服务攻击(DDoS)指的是通过多台机器向一个服务或者网站发送大量看似合法的数据包使其网络阻塞.资源耗尽从而不能为正常用户提供正常服务的攻击手段.随着互联网 ...

  10. Linux之时钟中断

    from:深入分析Linux内核源码(http://oss.org.cn/kernel-book/) 时钟中断的产生 Linux的OS时钟的物理产生原因是可编程定时/计数器产生的输出脉冲,这个脉冲送入 ...