Week9练习赛总结
| 题目编号 | T1 | T2 | T3 | T4 | 总结 |
|---|---|---|---|---|---|
| pts | 50 | 0 | 0 | 15 | 65 |
| 耗时 | 60min | 20min | 30min | 10min | 2h |
T1
特判写错了导致100pts->50pts
解析:
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
解析:
- 设计状态
用
2.设计转移
枚举
- 设计答案
最后再加上神奇的前缀和优化就从
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去找约数和为
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;
}