状压+根号分治 代码清奇可爱,一点也不毒瘤
题目思路和代码借鉴神犇 mrsrz 的题解
有个位置,每个位置可以取也可以取。问对于可能的中序列,其lcm的和是多少,对质数p取模.
从子任务开始
- 子任务3 , 中不同的正整数不超过 18 个。
直接爆搜是不可能的。但是我们可以离散化之后爆搜。记录每个数出现的次数,它对答案的贡献就是.同样的道理,我们就可以把n的规模缩小到了s的规模
- 子任务5
一个序列的LCM可以用其质因子极其指数表示。对于100以内的数,我们发现2、3、5、7的指数可能超过1,其余的质数指数只可能为1 or 0.所以我们设表示,暴力转移即可。
40分代码
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int N = 302;
int n , mod , max_val;
int vis[N] , pow2[500002] , tot;
pair<int , int> valid[N];
int gcd(int x , int y) {return y ? gcd(y , x % y) : x;}
//int64 lcm(int x , int y) {return 1ll * x * y / gcd(x , y);}
int is[N] , prime[62] , cnt;
void sieve()
{
is[0] = is[1] = -1;
for(int i = 2 ; i <= 300 ; i ++)
{
if(~is[i]) prime[cnt++] = i;
for(int j = 0 ; j < cnt && i * prime[j] <= 300 ; j ++)
{
is[i * prime[j]] = -1;
if(i % prime[j] == 0) break;
}
}
for(int i = 0 ; i < cnt ; i ++)
is[prime[i]] = i;
}
struct Num
{
char s[62];
char& operator[] (int x)
{
return s[x];
}
Num(int x = 1)
{
memset(s , 0 , sizeof(s));
for(int i = 0 ; prime[i] * prime[i] <= x ; i ++)
while(x % prime[i] == 0)
s[i]++ , x /= prime[i];
if(x != 1) s[is[x]]++;
}
int val()
{
int res = 1;
for(int i = 0 ; i < 62 ; i ++)
for(int j = 0 ; j < s[i] ; j ++)
res = 1ll * res * prime[i] % mod;
return res;
}
Num operator *(Num &t) const
{
Num ans;
for(int i = 0 ; i < 62 ; i ++)
ans[i] = max(s[i] , t[i]);
return ans;
}
Num operator *(int t)
{
Num tmp(t);
return *this * tmp;
}
};
namespace tot_18
{
int64 ans = 0;
void dfs(int step , int64 repeat , Num lcm)
{
if(step > tot)
{
ans = ( ans + repeat * lcm.val() ) % mod;
return ;
}
dfs(step + 1 , repeat * (pow2[valid[step].second] - 1) % mod , lcm * valid[step].first);
dfs(step + 1 , repeat , lcm);
}
void solve()
{
dfs(1 , 1 , Num());
printf("%lld\n" , ans);
exit(0);
}
}
namespace maxval_100
{
int64 dp[7][5][3][3][1<<11];
//dp[2有几次幂][3][5][7][剩余11个数的状态]有多少种情况
int64 more = 1 , ans = 0;//最后答案要乘及
void solve()
{
dp[0][0][0][0][0] = 1;
for(int i = 1 ; i <= tot ; i ++)
{
if(valid[i].first == 1)
{
more = more * pow2[valid[i].second] % mod;
continue;
}
if(~is[valid[i].first] && valid[i].first > 50)
{
more = more * ((pow2[valid[i].second] - 1ll) * valid[i].first % mod + 1) % mod;
continue;
}
Num tmp(valid[i].first);
int _2 = tmp[0];
int _3 = tmp[1];
int _5 = tmp[2];
int _7 = tmp[3];
int _o = 0;
for(int i = 4 ; i < 15 ; i ++)
if(tmp[i]) _o |= (1 << (i - 4));
for(int _2_ = 6 ; ~_2_ ; _2_ --)
for(int _3_ = 4 ; ~_3_ ; _3_ --)
for(int _5_ = 2 ; ~_5_ ; _5_ --)
for(int _7_ = 2 ; ~_7_ ; _7_ --)
for(int _o_ = 2047 ; ~_o_ ; _o_--)
(dp[max(_2_ , _2)][max(_3_ , _3)][max(_5_ , _5)][max(_7_ , _7)][_o_ | _o] += dp[_2_][_3_][_5_][_7_][_o_] * (pow2[valid[i].second] - 1) ) %= mod;
}
for(int _2_ = 6 ; ~_2_ ; _2_ --)
for(int _3_ = 4 ; ~_3_ ; _3_ --)
for(int _5_ = 2 ; ~_5_ ; _5_ --)
for(int _7_ = 2 ; ~_7_ ; _7_ --)
for(int _o_ = 2047 ; ~_o_ ; _o_--)
{
Num tmp;
tmp[0] = _2_;
tmp[1] = _3_;
tmp[2] = _5_;
tmp[3] = _7_;
for(int i = 4 ; i < 15 ; i ++)
if(_o_ & (1 << (i - 4))) tmp[i] = 1;
ans = (ans + dp[_2_][_3_][_5_][_7_][_o_] * tmp.val()) % mod;
}
ans = ans * more % mod;
printf("%lld\n" , ans);
exit(0);
}
}
int main()
{
scanf("%d %d" , &n , &mod);
for(int i = 1 , t; i <= n ; i ++)
{
scanf("%d" , &t);
vis[t]++;
max_val = max(max_val , t);
}
pow2[0] = 1;
for(int i = 1 ; i <= n ; i ++)
pow2[i] = 2*pow2[i - 1] % mod;
for(int i = 1 ; i <= max_val ; i ++)
if(vis[i]) valid[++tot] = make_pair(i , vis[i]);
sieve();
if(tot <= 18) tot_18::solve();
if(max_val <= 100) maxval_100::solve();
}- 100分做法
子任务5的做法之所以劣,是因为我们对大于的质因数处理不善。因此,我们可以考虑不是直接状压,而是分开枚举的策略。
对于小于等于17的质数,我们暴力状压,记录每个状态的方案数而不是对答案的贡献
之后,我们对于每一个大于17的质数,我们在std::vector中记录拥有这个质因子的数(这个数显然只会出现1次)
先设表示LCM中含有且只含有质因子2、3、5、7、11、13、17的指数的方案数。也就是说,我们不会把有大质因子的数在此处转移。
初始值:
转移:采用刷表法+滚动数组,每次枚举一个数,求出旧状态与新数的LCM,然后乘上对应的贡献
再设表示对于某一个大于17的质因子,它出现的状态为,小质因子出现的状态时时,其所有状态下LCM的总和。
初始值:
转移:和上面类似,但是注意每次求出新值和旧值的商,然后把贡献乘上这个值。这意味这我们的LCM变大了,贡献也变多了
每次处理完一个质因子,我们就令,意味着下一个质因子与上一个质因子没有关系。
那么答案就是。注意剪枝,就可以过辣!。
常数巨大的代码,几乎是卡着时限过的QwQ(善用宏定义,使得代码变得不那么毒瘤)
#include <bits/stdc++.h>
using namespace std;
const int M = 301;
int n , mod , cnt[M] ;
const int pri[] = {2 , 3 , 5 , 7 , 11 , 13 , 17};
const int maxe[] = {8 , 5 , 3 , 2 , 2 , 2 , 2};
inline int read() {
int x = 0 ; char c = getchar(); bool f = 0;
while(c < '0' || c > '9') f |= (c=='-') , c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + (c^48) , c = getchar();
return f ? -x : x;
}
inline int qpow(int x , int y)
{
int res = 1;
for( ; y ; y >>= 1 , x = 1ll * x * x % mod)
if(y & 1) res = 1ll * res * x % mod;
return res;
}
int pfac[M][7];
vector<int> havep[M];
#define ALL_NUM(x) \
for(int x[7] = {0} ; x[0] <= maxe[0] ; x[0]++) \
for(x[1] = 0 ; x[1] <= maxe[1] ; x[1]++) \
for(x[2] = 0 ; x[2] <= maxe[2] ; x[2]++) \
for(x[3] = 0 ; x[3] <= maxe[3] ; x[3]++) \
for(x[4] = 0 ; x[4] <= maxe[4] ; x[4]++) \
for(x[5] = 0 ; x[5] <= maxe[5] ; x[5]++) \
for(x[6] = 0 ; x[6] <= maxe[6] ; x[6]++)
#define ____ [9][6][4][3][3][3][3]
int dp[2]____ , f[2][2]____;
int& at(int a ____ , int x[7]) {
return a[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]][x[6]];
}
int calc(int x[7]) {
int ans = 1;
for(int i = 0 ; i < 7 ; ++i)
ans = 1ll * ans * qpow(pri[i] , x[i]) % mod;
return ans;
}
int main()
{
n = read() , mod = read();
for(int i = 1 ; i <= n ; ++i) cnt[read()]++;
********dp = 1;
int cur = 0 , ans = 0;
for(int i = 1 ; i <= 300 ; ++i) if(cnt[i])
{
int val = i , sum = qpow(2 , cnt[i]) - 1 , y[7];;
for(int j = 0 ; j < 7 ; ++j)
while(val % pri[j] == 0) val /= pri[j] , ++pfac[i][j];
if(val > 1) {havep[val].push_back(i) ; continue;}
memcpy(dp[cur ^ 1] , dp[cur] , sizeof(*dp));
cur ^= 1;
ALL_NUM(x) {
int& from = at(dp[cur ^ 1] , x);
for(int j = 0 ; j < 7 ; ++j) y[j] = max(x[j] , pfac[i][j]);
int &to = at(dp[cur] , y);
to = (to + 1LL * from * sum) % mod;
}
}
ALL_NUM(x) at(f[0][0] , x) = 1LL * at(dp[cur] , x) * calc(x) % mod;
cur = 0;
for(int p = 1 ; p <= 300 ; ++p) if(havep[p].size())
{
for(int i : havep[p])
{
int sum = qpow(2 , cnt[i]) - 1 , y[7] , z[7];
memcpy(f[cur ^ 1] , f[cur] , sizeof(*f));
cur ^= 1;
for(int t = 0 ; t <= 1 ; ++t) ALL_NUM(x)
{
int &from = at(f[cur ^ 1][t] , x);
for(int j = 0 ; j < 7 ; ++j)
y[j] = max(x[j] , pfac[i][j]) , z[j] = y[j] - x[j];
int &to = at(f[cur][1] , y) , d = 1LL * calc(z) * qpow(p , t^1) % mod;
to = (to + 1LL * from * d % mod * sum) % mod;
}
}
ALL_NUM(x) {
(at(f[cur][0] , x) += at(f[cur][1] , x)) %= mod;
at(f[cur][1] , x) = 0;
}
}
ALL_NUM(x) (ans += at(f[cur][0] , x)) %= mod;
printf("%d\n" , ans);
return 0;
}