hdu6125 Free from square 分组背包+状态压缩
/**
题目:hdu6125 Free from square
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6125
题意:
从不大于n的所有正整数中选出至少1个且至多k个使得乘积不包含平方因子,对10^9+7取模。
1≤n,k≤500。
思路:
分组背包+状态压缩
把n个数分成若干组,互斥的放在同一组。 一开始把所有含平方因子的数去除掉,剩下的进行分组。 <sqrt(500)的八个素因子,编成八组,分别为包含2,3,5,7,11,13,17,19素因子的数。 注意包含的数不能重复。
>sqrt(500)的素因子,每一组为包含该素因子的数。
1这个数为一组. 因为其他组可能包含2,3,5,7,11,13,17,19;也就是这八个素因子在其他组也可能出现。 为了判断用状态压缩处理。 dp[k][s]表示选k个,前8个素因子选择状态为s时候的方法数。 dp[k][s] += dp[k-1][s-s1]; (s&s1==s1) memset(dp, 0, sizeof dp);
dp[0][0] = 1; ans = sigma[1<=i<=m]sigma[0<=s<(1<<8)]dp[i][s];
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = ;
int prime[maxn], tot;
int pos[];
vector<P> v[maxn];
int vis[maxn];
LL dp[maxn][<<];
void init()
{
for(int i = ; i < maxn; i++){
if(prime[i]==){
for(int j = i*; j < maxn; j+=i){
prime[j] = ;
}
}
}
tot = ;
for(int i = ; i < maxn; i++){
if(prime[i]==){
prime[++tot] = i;
}
}
for(int i = ; i <= ; i++){
pos[prime[i]] = i-;
}
for(int i = ; i < maxn; i++){
int x = i;
for(int j = ; j <= tot&&prime[j]<=tot; j++){
int cnt = ;
if(x%prime[j]==){
while(x%prime[j]==){
x/=prime[j];
cnt++;
}
if(cnt>){
vis[i] = ; break;
}
}
}
}
v[].push_back(P(,));///表示1这个数。
for(int i = ; i <= tot; i++){
for(int j = prime[i]; j < maxn; j+=prime[i]){
if(vis[j]) continue;
int s = ;
for(int k = ; k <= ; k++){
if(j%prime[k]==){
s |= (<<pos[prime[k]]);
}
}
v[i].push_back(P(j,s));
vis[j] = ;
}
}
for(int i = ; i <= ; i++){
for(int j = prime[i]; j < maxn; j+=prime[i]){
if(vis[j]) continue;
int s = ;
for(int k = ; k <= ; k++){
if(j%prime[k]==){
s |= (<<pos[prime[k]]);
}
}
v[i].push_back(P(j,s));
vis[j] = ;
}
} }
LL solve(int n,int m)
{
ms(dp,);
dp[][] = ;
int len = <<;
for(int i = ; i <= tot; i++){
if(prime[i]>n) break;
for(int j = m; j >= ; j--){
for(int k = ; k < (int)v[i].size(); k++){
if(v[i][k].first>n) continue;
for(int s = ; s < len; s++){
if((s&v[i][k].second)==v[i][k].second){
dp[j][s] = (dp[j][s]+dp[j-][s-v[i][k].second])%mod;
}
}
}
}
}
LL ans = ;
for(int i = ; i <= m; i++)
for(int s = ; s < len; s++)
ans = (ans+dp[i][s]) %mod;
return ans;
}
int main()
{
init();
int T;
int n, k;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&k);
printf("%lld\n",solve(n,k));
} return ;
}
hdu6125 Free from square 分组背包+状态压缩的更多相关文章
- hdu6149 Valley Numer II 分组背包+状态压缩
/** 题目:hdu6149 Valley Numer II 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意: 众所周知,度度熊非常喜欢图. ...
- POJ-2923 Relocation---01背包+状态压缩
题目链接: https://vjudge.net/problem/POJ-2923 题目大意: 有n个货物,给出每个货物的重量,每次用容量为c1,c2的火车运输,问最少需要运送多少次可以将货物运完 思 ...
- POJ 2923 【01背包+状态压缩/状压DP】
题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...
- POJ 2923 Relocation(01背包+状态压缩)
题意:有人要搬家,有两辆车可以运送,有若干家具,车有容量限制,而家具也有体积,那么如何运送会使得运送车次最少?规定两车必须一起走,两车一次来回只算1躺. 思路:家具怎么挑的问题,每趟车有两种可能:1带 ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
- hdu 6125 -- Free from square(状态压缩+分组背包)
题目链接 Problem Description There is a set including all positive integers that are not more then n. Ha ...
- HDU 6125 Free from square(状态压缩+分组背包)
http://acm.hdu.edu.cn/showproblem.php?pid=6125 题意: 在${1,2,3,...n}$的数中选择1~k个数,使得它们的乘积不能被平方数整除(1除外),计算 ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...
随机推荐
- JQuery EasyUI 动态改变表单项的验证守则
//JQuery EasyUI 动态改变表单项的验证规则 $(document).ready(function(){ $('#FILE_QUALITY').combobox({ onChange:fu ...
- 通过Intel XDK编写跨平台app(一)
Intel XDK 是一个新的跨平台手机应用开发工具.它努力把整个开发流程变的简单,尽可能把所有的平台都封装到一个包中,通过收集各种开发工具来使你的开发变的简单. 在这篇文章中,我将会向你介绍什么是I ...
- js 判断进入可视区域
js 判断进入可视区域 CreateTime--2018年4月14日08:17:41 Author:Marydon 1.使用场景 图片懒加载时候需要用到,其他情况暂时没有遇到,欢迎留言补充! 2. ...
- index.js
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- easyui框架Date日期类型以json形式显示到前台datagrid时,显示为[object Object]
如下图,easyui当后台把时间数据返回转换成json然后加载在easyui的datagrid里面,显示为[object Object] 需要对时间格式添加格式的显示方法 /** * 时间格 ...
- 随笔小问题(一)--mac打开class文件
本来不想写这个东西的.但是这个却费了我一番周折. 我要先声明一点的是,我从来不讲iOS当成一个单独的系统,而是将这个操作系统归位unix内核的系统. 简单来说,我把它当成linux在用. 但是,mac ...
- jquery 属性操作 attr( ) prop()css( )区别
一 attr () 和 prop( ) 操作属性 谈谈我的总结: 1 2 1 属性的定义,根据W3C手册所述:属性包括,标准属性:id class style title 语言属性 lang dir以 ...
- Android+手势识别详解
今天就来把以前的学习文章与经验简单总结中出来吧,在这里我就直接把代码贴下来了,希望能给初学者做最佳的学习参考,也希望有更多的开发人员来加入 ANDROID开发团队,参与更多的创新方式的开发,好了, ...
- 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
非变动性算法代码分析与示例: 一.for_each C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // TEMPLATE FUNCTION for_eac ...
- hdu 4779 Tower Defense (思维+组合数学)
Tower Defense Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...