| 题目编号 | T1 | T2 | T3 | T4 | 总结 |
|---|---|---|---|---|---|
| pts | 50 | 0 | 0 | 15 | 65 |
| 耗时 | 60min | 20min | 30min | 10min | 2h |
T1
特判写错了导致100pts->50pts
解析:
,所以和一定互质,那么由于题目求的是和,所以题目可以转化为:给定两个数和,找到最大的二元组使得和互质且。
那么何时和最大呢?容易发现,由于相邻的两个整数一定互质,所以一定是最大的,但时,明显更优。
注意时输出-1。
Code
#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很方便,因为转移实在是太好写了。
因为题目没有芒果去重,所以桶统计每个数字数量非常重要,定义状态表示以作为子序列中最大公倍数的序列长度,容易想到初始化,因为自己肯定是自己的倍数。
接下来设计转移,类似于埃氏筛的过程,对于所有,都一定会存在转移。
最后确定答案为
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;
}