跳到正文

个人记录

DP tricks

gala_xk约 9 分钟
原文

这是 2025年10月31日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月2日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

CF1043F Make It One

题解

题解

首先,有一个显然的 n2log⁡nn^2\log n 的 DP,大概就是设 dp[i]dp[i] 表示公约数 ii 可以得到的最小值,枚举每一个 a[i]a[i] 进行转移。

考虑优化,发现我们想破脑壳都只能做到 n2n^2。

应该是我们状态出了问题。

观察题面,不难注意到,ans≤7ans\le 7 或无解。

证明

对于最优策略,我们可以设想其在每一次 gcd⁡\gcd 中都消掉了原数中的一个质因数。而 2×3×5×7×11×13×17≥3×1062\times3\times5\times7\times11\times13\times17\ge3\times10^6,原数显然只能有六个不同质因数,每一次都筛掉了一个,总共 gcd⁡\gcd 了六次,也就是 ans=7ans=7。

此时一个 dp 的结果只有 0∼70\sim7 似乎太浪费了。

我们考虑将答案设为状态。考虑之前的转移存在一个缺点:无法记录后面对前面的影响而需要 O(n)O(n) 的一次递推。我们重设 dp[i][j]dp[i][j] 表示答案为 ii,公约数为 jj 的方案数,为 00 即无解。

转移如下:

f[i][j]=(ctji)−∑k=13×105jdp[i][jk]f[i][j]=\binom{ct_j}{i}-\sum^{\frac{3\times10^5}{j}}_{k=1}dp[i][jk]

其中 ctjct_j 表示 aa 中可以被 jj 整除的数的数量。

std

代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int n,m;
int s[300005],f[300005],cs[300005];
ll dp[10][300005];
ll fact[2000005],inv[2000005],ct[300005];
ll qpow(ll x,ll y){
	if(y==0)return 1;
	ll z=qpow(x,y/2);
	z=z*z%mod;
	if(y&1)z=z*x%mod;
	return z;
}
ll C(ll x,ll y){
	if(y>x)return 0;
	return fact[x]*inv[y]%mod*inv[x-y]%mod;
}
ll gcd(ll x,ll y){
	if(y==0)return x;
	return gcd(y,x%y);
}
vector<int>vt;
void work(){
	fact[0]=1;
	for(int i=1;i<=2000000;i++)fact[i]=fact[i-1]*i%mod;
	inv[2000000]=qpow(fact[2000000],mod-2);
	for (int i=1999999;i>=1;i--)inv[i]=inv[i+1]*(i+1)%mod;
	inv[0]=1;
	/*fill your code here*/
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i];
	}
	memset(f,0,sizeof(f));
	for(int i=1;i<=n;i++){
		f[s[i]]++;
	}
	for(int i=1;i<=3e5;i++){
		for(int j=1;j*i<=3e5;j++){
			ct[i]+=f[i*j];
		}
//		cout<<ct[i]<<"\n";
	}
	for(int p=1;p<=7;p++){
		for(int q=3e5;q>=1;q--){
			dp[p][q]=C(ct[q],p);
			for(int k=3e5/q;k>=2;k--){
				dp[p][q]-=dp[p][k*q];
				dp[p][q]=(dp[p][q]+mod)%mod;
			}
//			if(q<=60)
//			cout<<dp[p][q]<<" ";
		}
//		puts("");
	}
//	for(int i=1;i<=n;i++){
//		for(int j=1;j<=10;j++){
//			if(pp[i]){
//				
//			}
//		}
//	}
	for(int i=1;i<=1;i++){
		vt.clear();
		for(int j=1;j*i<=3e5;j++){
			for(int k=1;k<=f[i*j];k++)
			vt.push_back(j);
		}
		int c=0;
		for(auto v:vt){
			c=gcd(v,c);
		}
		if(c>1){
			cout<<"-1\n";
			continue;
		}
		int ans=1;
		while(dp[ans][i]==0){
			ans++;
			if(ans>7)break;
		}
		if(ans==8){
			cout<<"-1\n";
			continue;
			
		}
		cout<<ans<<endl;
	}
	return;
}

#ifdef int
signed main()
#else
int main()
#endif
{
	
//	freopen("sor.in","r",stdin);
//	freopen("sor.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}



/* Code written by
		gala_xk (MarchWind)*/