Week9练习赛总结

· · 个人记录

题目编号 T1 T2 T3 T4 总结
pts 50 0 0 15 65
耗时 60min 20min 30min 10min 2h

T1

特判写错了导致100pts->50pts

解析:

那么何时$x$和$y$最大呢?容易发现,由于相邻的两个整数一定互质,所以$(\lfloor \displaystyle\frac{c}{n} \rfloor,\lfloor \displaystyle\frac{c}{n} \rfloor - 1)$一定是最大的,但$\lfloor \displaystyle\frac{c}{n} \rfloor == 1$时,$(c,c)$明显更优。 注意$c > n$时输出```-1```。 ### Code ```cpp #include<bits/stdc++.h> #define int unsigned long long using namespace std; int c, n, a, b; signed main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> c >> n; int MaxG = n / c; if(c > n){ cout << -1; return 0; } if(MaxG == 1){ cout << (c * MaxG) * (c * MaxG); return 0; } cout << c * MaxG * c * (MaxG - 1); return 0; } ``` ## T2 很好的DP,使我的芒果旋转。 ### 解析: 容易发现,这道题写扩散型DP很方便,因为转移实在是太好写了。 因为题目没有芒果去重,所以桶统计每个数字数量非常重要,定义状态$dp_i$表示以$i$作为子序列中最大公倍数的序列长度,容易想到初始化$dp_i=cnt_i$,因为自己肯定是自己的倍数。 接下来设计转移,类似于埃氏筛的过程,对于所有$j|i$,都一定会存在转移$dp_j=max(dp_j,dp_i)$。 最后确定答案为$max(dp_i)

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int n, dp[N], ans, cnt[N], MaxNum;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;
    for(int i = 1, x; i <= n; i ++) {
        cin >> x;
        cnt[x] ++, MaxNum = max(MaxNum, x);
    }
    for(int i = 1; i <= MaxNum; i ++) {
        if(cnt[i]) {
            dp[i] += cnt[i];
            for(int j = 2 * i; j <= MaxNum; j += i){
                dp[j] = max(dp[j], dp[i]);
            }
            ans = max(ans, dp[i]);
        }
    }
    cout << ans;
    return 0;
}

T3

仍然DP

解析:

  1. 设计状态

用dp_{i,cnt}表示表示当前在i这个坐标的时候设置一个特殊的城池, 并且在 i城池的前面已经设置了cnt座城池的答案。

2.设计转移

枚举j和cnt,由于从j到j+(i-j)/2的答案不会更优,所以要更改的只有j+(i-j)/2+1到255的答案,容易列出转移: dp_{i, cnt} = \min_{j=0}^{j=i-1} \left\{ dp_{j, cnt-1} + \sum_{x=(i+j)/2 +1}^{255} pos_x \cdot \left[ (x-i)^2 - (x-j)^2 \right] \right\}

  1. 设计答案
\min_{i=0}^{i=255}{ dp_{i,k}}

最后再加上神奇的前缀和优化就从O(n^4)->O(n^3)过了。

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5.12e2 + 10;
int n, k, dp[N][N], pos[N], sum[N], sumx[N], ans = 1e18;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> k;
    for(int i = 0; i < 256; i ++){
        for(int j = 0; j < 256; j ++){
            dp[i][j] = 1e18;
        }
        dp[i][1] = 0;
    }
    for(int i = 0, x, c; i < n; i ++){
        cin >> x >> c;
        pos[x] = c;
        for(int j = 0; j < 256; j ++){
            dp[j][1] += (x - j) * (x - j) * c;
        }
    }
    for(int i = 255; i >= 0; i --){
        sum[i] = pos[i] + sum[i + 1];
        sumx[i] = pos[i] * i + sumx[i + 1];
    }
    for(int i = 0; i < 256; i ++){
        for(int j = 0; j < i; j ++){
            for(int cnt = 2; cnt <= k; cnt ++){
                int mid = (i + j) / 2 + 1;
                dp[i][cnt] = min(dp[i][cnt], dp[j][cnt - 1] + 2 * (j - i) * sumx[mid] + (i * i - j * j) * sum[mid]);
            }
        }
    }
    for(int i = 0; i < 256; i ++){
        ans = min(ans, dp[i][k]);
    }
    cout << ans;
    return 0;
}

T4

ex

解析:

根据正约数和定理,可以用DFS去找约数和为n_i的,最后唯一分解定理还原n_i就可以了。

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int T, n, P[N], cnt;
bool vis[N];
vector<int> op;
void init() {
    vis[0] = vis[1] = 1;
    for(int i = 2; i < N; i ++) {
        if(!vis[i]) {
            P[cnt ++] = i;
            for(int j = i * 2; j < N; j += i) {
                vis[j] = 1;
            }
        }
    }
}
bool check(int x) {
    if(x <= 1) {
        return 0;
    }
    for(int i = 2; i * i <= x; i ++) {
        if(x % i == 0) {
            return 0;
        }
    }
    return 1;
}
void dfs(int now, int x, int sum) {
    if(now == 1) {
        op.push_back(sum);
        return ;
    }
    if(check(now - 1) && now > P[x]) {
        op.push_back(sum * (now - 1));
    }
    for(int i = x; P[i] * P[i] <= now; i ++) {
        int tmp = P[i];
        for(int j = P[i] + 1; j <= now; tmp *= P[i], j += tmp) {
            if(now % j == 0) {
                dfs(now / j, i + 1, sum * tmp);
            }
        }
    }
    return ;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    init();
    cin >> T;
    while(T --) {
        cin >> n;
        op.clear();
        dfs(n, 0, 1);
        cout << op.size() << '\n';
        sort(op.begin(), op.end());
        for(int i = 0; i < op.size(); i ++) {
            cout << op[i] << ' ';
        }
        if(op.size() > 0) cout << '\n';
    }
    return 0;
}