B4197 [Noi2015]寿司晚宴 状压dp
这个题一开始想到了唯一分解定理,然后状压。但是显然数组开不下,后来想到每个数(n<500)大于19的素因子只可能有一个,所以直接单独存就行了。
然后正常状压dp就很好搞了。
题干:
Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴。小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴。
在晚宴上,主办方为大家提供了 n− 种不同的寿司,编号 ,,,…,n−,其中第 i 种寿司的美味度为 i+ (即寿司的美味度为从 到 n)。
现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 x 的寿司,小 W 品尝的寿司中存在一种美味度为 y 的寿司,而 x 与 y 不互质。
现在小 G 和小 W 希望统计一共有多少种和谐的品尝寿司的方案(对给定的正整数 p 取模)。注意一个人可以不吃任何寿司。
Input
输入文件的第 行包含 个正整数 n,p,中间用单个空格隔开,表示共有 n 种寿司,最终和谐的方案数要对 p 取模。
Output
输出一行包含 个整数,表示所求的方案模 p 的结果。
Sample Input Sample Output HINT
≤n≤
<p≤
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
const int mod = ;
typedef double db;
typedef long long ll;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
{x = x * + c - '';}
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
ll pri[] = {,,,,,,,,,};
struct node
{
ll val,big,s;
void init()
{
ll tmp = val;
big = -;
duke(i,,)//only
{
if(tmp % pri[i]) continue;
s |= ( << (i - ));
while(tmp % pri[i] == )
tmp /= pri[i];
}
if(tmp != )
big = tmp;
}
}a[];
bool cmp(node a,node b)
{
return a.big < b.big;
}
ll n;
ll p;
ll pl(ll l,ll r)
{
l += r;
return l >= p ? l - p : l;
}
ll f1[][],f2[][];
ll dp[][];
int main()
{
read(n);read(p);
duke(i,,n)
a[i - ].val = i,a[i - ].init();
sort(a + ,a + n,cmp);
dp[][] = ;
duke(i,,n - )
{
if(i == || a[i].big != a[i - ].big || a[i].big == -)
{
memcpy(f1,dp,sizeof(f1));
memcpy(f2,dp,sizeof(f2));
}
lv(j,,)
{
lv(k,,)
{
if(j & k) continue;
if((a[i].s & j) == )
f2[j][k | a[i].s] = pl(f2[j][k | a[i].s],f2[j][k]);
if((a[i].s & k) == )
f1[j | a[i].s][k] = pl(f1[j | a[i].s][k],f1[j][k]);
}
}
if(i == n - || a[i].big != a[i + ].big || a[i].big == -)
{
duke(j,,)
{
duke(k,,)
{
if(j & k) continue;
dp[j][k] = pl(f1[j][k],pl(f2[j][k],p - dp[j][k]));
}
}
}
}
ll ans = ;
duke(j,,)
{
duke(k,,)
{
if((j & k) == && dp[j][k])
ans = pl(ans,dp[j][k]);
// cout<<dp[j][k]<<" ";
}
// cout<<endl;
}
printf("%lld\n",ans);
return ;
}
B4197 [Noi2015]寿司晚宴 状压dp的更多相关文章
- [NOI2015]寿司晚宴 --- 状压DP
[NOI2015]寿司晚宴 题目描述 为了庆祝NOI的成功开幕,主办方为大家准备了一场寿司晚宴. 小G和小W作为参加NOI的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了n−1种不同的寿 ...
- 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数
[BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...
- bzoj4197 [Noi2015]寿司晚宴——状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4197 首先,两个人选的数都互质可以看作是一个人选了一个数,就相当于选了一个质因数集合,另一个 ...
- BZOJ 4197: [Noi2015]寿司晚宴 状压dp+质因数分解
挺神的一道题 ~ 由于两个人选的数字不能有互质的情况,所以说对于一个质因子来说,如果 1 选了,则 2 不能选任何整除该质因子的数. 然后,我们发现对于 1 ~ 500 的数字来说,只可能有一个大于 ...
- [NOI2015]寿司晚宴——状压dp
题目转化:将2~n的数分成两组,可以不选,使得这两组没有公共的质因子.求方案数. 选择了一个数,相当于选择了它的所有质因子. 30分: 发现,n<=30的时候,涉及到的质因子也就10个.2,3, ...
- 【BZOJ-4197】寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- BZOJ 4197 NOI 2015 寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- NOI 2015 寿司晚宴 (状压DP+分组背包)
题目大意:两个人从2~n中随意取几个数(不取也算作一种方案),被一个人取过的数不能被另一个人再取.两个人合法的取法是,其中一个人取的任何数必须与另一个人取的每一个数都互质,求所有合法的方案数 (数据范 ...
- [NOI2015][bzoj4197] 寿司晚宴 [状压dp+质因数]
题面 传送门 思路 首先,要让两个人选的数字全部互质,那么有一个显然的充要条件:甲选的数字的质因数集合和乙选的数字的质因数集合没有交集 30pt 这种情况下n<=30,也就是说可用的质数只有10 ...
随机推荐
- Java基础(九)--反射
什么是反射? 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性 这种动态获取的信息以及动态调用对象的方法的功能称为反射机制. 反射的前 ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- wpf 自定义单选按钮 RadioButton
新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...
- Linux kernel memory-faq.txt
## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...
- org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: xxxxxxx 原因是 ...
- 移动端placeholder不能垂直居中解决方案
1.问题描述 问题如图:手机端placeholder文字偏上,垂直方向不居中,input光标显示偏上解决IE下不支持placeholder属性 2.解决方案 css .phoneNumber inpu ...
- Android 找不到资源的问题
偶尔会遇到R.layout.***或R.id.***找不到资源的问题,明明在文件夹中有啊,那为什么嘞? 结合我自己遇到的情况和网上的资料,总结出以下几点可能的原因: 导入了android.R.这个是最 ...
- HDU 1159 LCS最长公共子序列
#include <cstdio> #include <cstring> using namespace std; ; #define max(a,b) a>b?a:b ...
- FZU 2105 (线段树)
Problem 2105 Digits Count Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we h ...
- [ZJOI2009]硬币游戏
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 920 Solved: 406[Submit][Status][Discuss] Descriptio ...