DP tricks

· · 个人记录

CF1043F Make It One

题解

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

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

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

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

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

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

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

转移如下:

f[i][j]=\binom{ct_j}{i}-\sum^{\frac{3\times10^5}{j}}_{k=1}dp[i][jk]
其中 ct_j 表示 a 中可以被 j 整除的数的数量。

std

:::info[代码]

/* 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)*/





:::