HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square
For each test case:
A single line contains two positive integers n,k(1≤n,k≤500).
A single line contains a nonnegative integer, denoting the answer.
4 2
6 4
19
#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 + 分组背包的更多相关文章
- HDU 6125 Free from square (状压DP+分组背包)
题目大意:让你在1~n中选择不多于k个数(n,k<=500),保证它们的乘积不能被平方数整除.求选择的方案数 因为质数的平方在500以内的只有8个,所以我们考虑状压 先找出在n以内所有平方数小于 ...
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3681 Prison Break(状态压缩dp + BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- HDU 1074 Doing Homework (状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 6125 Free from square (状压DP+背包)
题意:问你从 1 - n 至多选 m 个数使得他们的乘积不能整除完全平方数. 析:首先不能整除完全平方数,那么选的数肯定不能是完全平方数,然后选择的数也不能相同的质因子. 对于1-500有的质因子至多 ...
- HDU 1074 Doing Homework ——(状态压缩DP)
考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- 初学划分树,小见解之!POJ-2104/HDU-2665
划分树 本来是学主席树的,可怜我等巨弱观群巨博客难解fotle主席的思想精髓.于是学了一下划分树,嗯,花了一下午时间理解build(其实自己模拟一遍就通了),我很难理解为什么划分树会看不懂而能学会主席 ...
- Apple Pay强势来袭,开发者应做的事情(转)
"iOS8.1就已经有这个功能了,只是木有现在这么的火,现在的趋势是要火的节奏,因此很多电商平台B2B,P2P,C2C,X2X都有可能需要这个屌丝的付款功能了,在此简单的研究一下." ...
- 【Luogu】P1417烹调方案(排序01背包)
题目链接 对食材进行排序,重载运算符代码如下: struct food{ long long a,b,c; bool operator <(const food &a)const{ re ...
- BZOJ 1113 Wall ——计算几何
凸包第一题. 自己认为自己写的是Andrew 其实就是xjb写出来居然过掉了测试. 刚开始把pi定义成了int,调了半天 #include <map> #include <cmath ...
- Ionic 如何把左上角的按钮去掉?
代码实现: <ion-header > <ion-toolbar> <ion-buttons start> <a href="#"> ...
- UVa10214 Trees in a Wood.
先算第一象限能看到的树,答案乘以4就是四个象限的数的总数,再加上坐标轴上四棵树,就是总共能看到的树. 树的总数为(2*a+1)*(2*b+1)-1 ←矩形面积除去原点位置 设一棵树的坐标是(x,y) ...
- 树状数组求第K大(From CLJ)
; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;
- Python入门--11--自定义函数
使用def定义自定义函数 举个栗子: def myfristFunction(): print "we are 伐木累!" #输入myfristFunction() 会输出:we ...
- Kail命令
启动/关闭无线网卡 ifconfig wlan0 up / ifconfig wlan0 down 更改Mac地址: macchanger -A wlan0 启动监听 airmon-ng star ...
- [Bzoj3206][Apio2013]道路费用(kruscal)(缩点)
3206: [Apio2013]道路费用 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 536 Solved: 252[Submit][Status ...