luogu5616(状压DP)

题目链接

https://www.luogu.org/problem/P5616

题解

这个题让窝想到了 NOI 的寿司晚宴,然后发现那题窝还没补(X)

那么同样很容易想到对小于 $\sqrt{300}$ 的数进行状压,然后再考虑大质数的贡献。。

然后就没想到怎么处理大质数的贡献了。。

题解是对所有数按最大素因子进行排序,然后分段考虑当前质数有没有取到,这样就可以把大质数的问题解决了。。




代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define mid (x+y>>1)
#define eps 1e-8
#define succ(x) (1ll<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 305
#define nm 300005
using namespace std;
const double pi=acos(-1);
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



const int p[]={2,3,5,7,11,13,17};
ll pre[7][9];
ll inf;
inline void reduce(ll&x){x+=x>>63&inf;}
inline void upd(ll&x,ll y){reduce(x+=y-inf);}
int a[NM],tmp[NM],c[7],_t,mn[NM],n;
ll p2[nm],d[2][9][6][4][3][3][3][3][2],ans;


int main(){
int _=read();inf=read();
p2[0]=1;inc(i,1,_)p2[i]=p2[i-1]*2%inf;
while(_--)a[read()]++;
n=300;
inc(i,1,n)tmp[i]=i;
mn[1]=1;
inc(i,2,n)if(!mn[i])for(int j=i;j<=n;j+=i)mn[j]=i;
sort(tmp+1,tmp+1+n,[&](int x,int y){return mn[x]<mn[y];});
d[0][0][0][0][0][0][0][0][1]=1;
inc(i,1,n)if(mn[i]<=17)mn[i]=0;
inc(k,1,n){
int i=tmp[k];
_t^=1;
if(mn[i]==mn[tmp[k-1]])memcpy(d[_t],d[_t^1],sizeof(d[_t^1]));
else{
mem(d[_t]);
inc(t0,0,8)inc(t1,0,5)inc(t2,0,3)inc(t3,0,2)inc(t4,0,2)inc(t5,0,2)inc(t6,0,2)inc(v,0,1)
upd(d[_t][t0][t1][t2][t3][t4][t5][t6][0],d[_t^1][t0][t1][t2][t3][t4][t5][t6][v]);
}
mem(c);
inc(j,0,6)for(int t=i;t%p[j]==0;t/=p[j])c[j]++;
if(mn[i]!=mn[tmp[k-1]]){
inc(t0,0,8)inc(t1,0,5)inc(t2,0,3)inc(t3,0,2)inc(t4,0,2)inc(t5,0,2)inc(t6,0,2)inc(v,0,1)
upd(d[_t][max(t0,c[0])][max(t1,c[1])][max(t2,c[2])][max(t3,c[3])][max(t4,c[4])][max(t5,c[5])][max(t6,c[6])][1]
,d[_t^1][t0][t1][t2][t3][t4][t5][t6][v]*(p2[a[i]]-1+inf)%inf*mn[i]%inf);
}else{
inc(t0,0,8)inc(t1,0,5)inc(t2,0,3)inc(t3,0,2)inc(t4,0,2)inc(t5,0,2)inc(t6,0,2)
upd(d[_t][max(t0,c[0])][max(t1,c[1])][max(t2,c[2])][max(t3,c[3])][max(t4,c[4])][max(t5,c[5])][max(t6,c[6])][1]
,d[_t^1][t0][t1][t2][t3][t4][t5][t6][0]*(p2[a[i]]-1+inf)%inf*mn[i]%inf);
inc(t0,0,8)inc(t1,0,5)inc(t2,0,3)inc(t3,0,2)inc(t4,0,2)inc(t5,0,2)inc(t6,0,2)
upd(d[_t][max(t0,c[0])][max(t1,c[1])][max(t2,c[2])][max(t3,c[3])][max(t4,c[4])][max(t5,c[5])][max(t6,c[6])][1]
,d[_t^1][t0][t1][t2][t3][t4][t5][t6][1]*(p2[a[i]]-1+inf)%inf);
}
}
inc(i,0,6){pre[i][0]=1;inc(j,1,8)pre[i][j]=pre[i][j-1]*p[i];}
inc(t0,0,8)inc(t1,0,5)inc(t2,0,3)inc(t3,0,2)inc(t4,0,2)inc(t5,0,2)inc(t6,0,2)inc(v,0,1)
upd(ans,d[_t][t0][t1][t2][t3][t4][t5][t6][v]*pre[0][t0]*pre[1][t1]*pre[2][t2]%inf*pre[3][t3]*pre[4][t4]*pre[5][t5]%inf*pre[6][t6]%inf);
printf("%lld\n",ans);
return 0;
}