题意:问你从 1 - n 至多选 m 个数使得他们的乘积不能整除完全平方数。

析:首先不能整除完全平方数,那么选的数肯定不能是完全平方数,然后选择的数也不能相同的质因子。

对于1-500有的质因子至多出现一次,有的可能出现多次,比如23,对于一个数最多出现一次,因为出现两次就超出500了。

而对于比较小的质因子,比如2,3,这样的,可以出现多次,这样的话我们就可以分开来计算。

对于出现多次的,一共只有8个,我们可以用状压,2, 3, 5, 7, 11, 13, 17, 19,

对于最多出现一次的,我们把它的倍数都放到一组去,因为它或者它的倍数最多只能出现一个,当然可以不出现。

然后就可以进行计算了,就是一个背包问题,就是分成了几组,一组是只有质因子的,其他的都是含有较大的质因子。

然后我们可以对每个数进行枚举,然后进行状态转移。

dp[i][s] 表示选 i 个数,状态是 s 有多少种,状态转移也是比较好转移的,我是进行两种转移。

第一种能够完全整除较小质因子的,那么这样的数,只要不出现重复因子,是可以选多次的。

第二种是不能够完全整除较小质因子的,所以每组至少选一个。

转移方程:dp[i+1][s|k] += dp[i][k]  ((k&s) == 0)

对于前面说的分组,对于所有的数据都是成立的,可以先进行预处理。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <assert.h>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 0xffffffffffLL;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 500 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL dp[maxn][1<<8];
const int p[] = {2, 3, 5, 7, 11, 13, 17, 19};
int st[maxn];
vector<int> v[maxn]; void init(){
for(int i = 1; i <= 500; ++i){
bool ok = true;
int x = i;
for(int j = 0; j < 8 && ok; ++j){
int cnt = 0;
while(x % p[j] == 0){
x /= p[j]; ++cnt;
st[i] |= 1<<j;
}
if(cnt > 1) ok = false;;
}
if(ok) v[x].pb(i);
}
} LL solve(){
ms(dp, 0);
dp[0][0] = 1;
int all = 1<<8;
for(int l = 1; l <= n; ++l){
if(v[l].empty()) continue;
if(l == 1){
for(int i = 0; i < v[l].size() && v[l][i] <= n; ++i){
int x = v[l][i];
for(int j = m-1; j >= 0; --j)
for(int k = 0; k < all; ++k){
if(k&st[x]) continue;
dp[j+1][k|st[x]] = (dp[j+1][k|st[x]] + dp[j][k]) % mod;
}
}
continue;
}
for(int j = m-1; j >= 0; --j)
for(int k = 0; k < all; ++k)
for(int i = 0; i < v[l].size() && v[l][i] <= n; ++i){
int s = st[v[l][i]];
if(k&s) continue;
dp[j+1][k|s] = (dp[j+1][k|s] + dp[j][k]) % mod;
}
} LL ans = 0;
for(int i = 1; i <= m; ++i)
for(int j = 0; j < all; ++j)
ans = (ans + dp[i][j]) % mod;
return ans;
} int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
printf("%I64d\n", solve());
}
return 0;
}

  

HDU 6125 Free from square (状压DP+背包)的更多相关文章

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

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

  2. HDU 6149 Valley Numer II 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...

  3. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  4. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

  5. HDU 4906 Our happy ending (状压DP)

    HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...

  6. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  7. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  8. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  9. HDU 4899 Hero meet devil (状压DP, DP预处理)

    题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...

随机推荐

  1. 第十三章 hadoop机架感知

    背景 分布式的集群通常包含非常多的机器,由于受到机架槽位和交换机网口的限制,通常大型的分布式集群都会跨好几个机架,由多个机架上的机器共同组成一个分布式集群.机架内的机器之间的网络速度通常都会高于跨机架 ...

  2. tomcat:A docBase * inside the host appBase has been specifi, and will be ignored

    警告: A docBase  D:\apache-tomcat-8.5.12\webapps\webapps\projectname inside the host appBase has been ...

  3. Docker学习总结(一)—— namespace,cgroup机制

    1.namespace: Linux Namespaces机制提供一种资源隔离方案.PID,IPC,Network等系统资源不再是全局性的,而是属于特定的Namespace.每个 Namespace里 ...

  4. 华为交换机S5700 vty 0 4

    最佳答案   vty 0 4 代表有5条VTY线路 由0到4 这个要看设备的版本有的设备会有更多条VTY线路比如一些企业版的设备 显示用户界面 进入TELNET 设置模式 user-interface ...

  5. [Cpp primer] Library string Type

    In order to use string type, we need to include the following code #include<string> using std: ...

  6. java中Thursday 05 September 2002类型时间的转化

    package config; import Java.text.DateFormat; import java.text.ParseException; import java.text.Simpl ...

  7. 网络编程基础之Socket套接字

    一.Socket介绍 1.什么是socket? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族 ...

  8. iPhone与Android手机 各个型号的UserAgent

    摘要:userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值.一般来讲,它是在navigator.appCodeName 的值之后加上斜线和navigator.a ...

  9. built-in SpecularType of Unity

    [built-in SpecularType of Unity] 1.声明变量. 注意并没有在Shader中声明_SpecColor,因为Lighting.cginc中已经帮我们声明. 2.声明使用B ...

  10. python's fnmatch&glob&os.listdir

    [python's fnmatch&glob&os.listdir] fnmatch: fnmatch只有4种special character,用于提供和shell中一样的文件名的匹 ...