CF1043F Make It One 题解(反演,容斥)

博客详细解析了CF1043F Make It One的解题思路,强调质因子最多6个,并指出答案上界为7。通过讨论gcd的卷积和容斥原理,介绍反演在解决此类问题中的作用,提供了利用反演公式g(n)=i=1,i×n≤m∑g(i×m)×μ(i)的解题方法。" 53029204,5747559,Hadoop入门:理解HDFS和MapReduce,"['hadoop', '分布式计算', 'ubuntu']

CF1043F Make It One

CF1043F Make It One


首先看一下质因子个数最多只有 6 6 6 个,考虑答案上界是多少。

2 3 5 7 11 13
3 5 7 11 13 17

不要忘记 17 17 17

你们答案的上界就是 7 7 7

我们考虑对于 gcd ⁡ \gcd gcd 进行卷积,那么我们需要使用容斥。

考虑反演:
g = f × I f = g × μ g = f \times I \\ f = g \times \mu g=f×If=g×μ

原理就是 I × μ = e I \times \mu = e I×μ=e

但是反演其实还有一种形式。
g ( n ) = ∑ i = 1 , i × n ≤ m f ( i × n ) f ( n ) = ∑ i = 1 , i × n ≤ m g ( i × m ) × μ ( i ) g(n) = \sum_{i = 1, i \times n \le m} f(i \times n) \\ f(n) = \sum_{i = 1, i \times n \le m} g(i \times m) \times \mu(i) g(n)=i=1,i×nmf(i×n)f(n)=i=1,i×nmg(i×m)×μ(i)
我们直接使用后面一种即可。

#include <bits/stdc++.h>
using namespace std;

//#define Fread
//#define Getmod

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#define getchar gc
#endif // Fread

template <typename T>
void r1(T &x) {
	x = 0;
	char c(getchar());
	int f(1);
	for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
	for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
	x *= f;
}

template <typename T,typename... Args> inline void r1(T& t, Args&... args) {
    r1(t);  r1(args...);
}

//#define int long long
const int maxn = 3e5 + 5;
const int maxm = maxn << 1;

int tot(0);
int pr[maxn / 10], vis[maxn], mu[maxn];
int n, m;

void init(int x) {
    for(int i = 2; i <= x; ++ i) {
        if(!vis[i]) pr[++ tot] = i, mu[i] = -1;
        for(int j = 1, k; k = i * pr[j], j <= tot && k <= x; ++ j) {
            vis[k] = 1;
            if(!(i % pr[j])) {
                mu[k] = 0; break;
            }
            mu[k] = - mu[i];
        }
    }
}

vector<int> operator * (const vector<int>& a, const vector<int>& b) {
    vector<int> ans(m + 2, 0);
    int i, j;
    for(i = 1; i <= m; ++ i) {
        int t1(0), t2(0);
        for(j = i; j <= m; j += i)
            t1 += a[j], t2 += b[j];
        ans[i] = t1 * t2;
    }
    for(i = 1; i <= m; ++ i) {
        for(j = 2; j * i <= m; ++ j)
            ans[i] += mu[j] * ans[i * j];
    }
    return ans;
}
signed main() {
//    freopen("S.in", "r", stdin);
//    freopen("S.out", "w", stdout);
    int i, j;
    init(3e5);
    r1(n);
    vector<int> c(n + 1, 0);
    for(i = 1; i <= n; ++ i) {
        r1(c[i]);
        m = max(m, c[i]);
    }

    vector<int> a(m + 2, 0), b(m + 2, 0);
    for(i = 1; i <= n; ++ i) ++ a[c[i]], ++ b[c[i]];

    for(i = 1; i <= 7; ++ i) {
        if(b[1]) return printf("%d\n", i), 0;
        b = b * a;
    }
	return 0;
}

### 思路分析 “CF Edu Round 109 F Non - decreasing Array” 这道题通常是关于动态规划或者数据结构优化的问题。一般会给定一个数组,要求通过一些操作使得数组变成非递减数组,并且可能会有一些额外的限制条件,比如最小化操作的代价等。 ### 代码示例 以下是一个可能的 C++ 题解示例,由于没有具体题目描述,代码逻辑是一个通用的处理非递减数组的思路,假设需要通过修改数组元素使得数组非递减,并且要最小化修改的元素个数: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int minChangesToNonDecreasing(vector<int>& arr) { int n = arr.size(); vector<int> dp(n, 1); for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { if (arr[j] <= arr[i]) { dp[i] = max(dp[i], dp[j] + 1); } } } int lisLength = *max_element(dp.begin(), dp.end()); return n - lisLength; } int main() { vector<int> arr = {3, 1, 2, 4, 5, 2, 6}; int result = minChangesToNonDecreasing(arr); cout << "Minimum changes to make the array non - decreasing: " << result << endl; return 0; } ``` ### 代码解释 1. **`minChangesToNonDecreasing` 函数**: - 首先初始化一个 `dp` 数组,`dp[i]` 表示以第 `i` 个元素结尾的最长非递减子序列的长度。 - 通过两层循环,外层循环遍历数组,内层循环遍历当前元素之前的所有元素。如果当前元素大于等于之前的元素,则更新 `dp[i]`。 - 最后找到 `dp` 数组中的最大值,即最长非递减子序列的长度。 - 用数组的长度减去最长非递减子序列的长度,得到需要修改的最少元素个数。 2. **`main` 函数**: - 定义一个测试数组 `arr`。 - 调用 `minChangesToNonDecreasing` 函数计算最少修改次数。 - 输出结果。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值