# 2021“MINIEYE杯”中国大学生算法设计超级联赛（1）个人题解

2021/7/21 17:34:36 浏览：

### 文章目录

• HDU6950 Mod, Or and Everything
• HDU6954 Minimum spanning tree
• HDU6958 KD-Graph
• HDU6957 Maximal submatrix
• HDU6955 Xor sum

CSL中超可没巨佬们训练强度大

“什么是BSGS、莫队、KD tree”

## HDU6950 Mod, Or and Everything

( n   m o d   1 ) ∣ ( n   m o d   2 ) ∣ . . . ∣ ( n   m o d   ( n − 1 ) ) (n\ mod\ 1)|(n\ mod \ 2)|...|(n\ mod\ (n-1)) 的值

QAQ

#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;

signed main()
{
DDLC_ESCAPE_PLAN_FAILED;
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int x = 0, res = n;
while(res){
res >>= 1LL;
x++;
}
if(n == 1LL){
cout << 0 << endl;
continue;
}
if(n == (1LL << (x - 1LL))) x--;
cout << (1LL << (x - 1LL)) - 1LL << endl;
}
return 0;
}


## HDU6954 Minimum spanning tree

#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn = 1e7 + 10; // 比赛的时候看成1e8，导致多了一次MLE
bool p[maxn];
int pr[maxn];
int pre[maxn];
void Prime(){
int cnt = 0;
mem(p);
p[0] = p[1] = 1;
for(int i = 2; i < maxn; ++i){
if(!p[i]) pr[cnt++] = i;
for(int j = 0; j < cnt && pr[j] * i < maxn; ++j){
p[pr[j] * i] = 1;
if(i % pr[j] == 0) break;
}
}
pre[0] = pre[1] = pre[2] = 0;
for(int i = 3; i < maxn; ++i){
if(!p[i]) pre[i] = pre[i - 1] + 2 * i;
else pre[i] = pre[i - 1] + i;
}
}
signed main()
{
DDLC_ESCAPE_PLAN_FAILED;
int t;
cin >> t;
Prime();
while(t--)
{
int n;
cin >> n;
cout << pre[n] << endl;
}
return 0;
}


## HDU6958 KD-Graph

1. 每组中任取两个点 p , q p,q ，他们之间的路径中，最长边不大于 D D
2. 不同组中任取两个点 p , q p,q ，他们之间的路径中，所有边都大于 D D

#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define pii pair<int, int>
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
// #define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn= 100000 + 5;
int fa[maxn];
struct edge{
int l, r, val;
bool operator < (const edge& b)const{
return val < b.val;
}
};
vector<edge> e;
inline int find(int x)
{
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
bool join(int x, int y)
{
int ans = 0;
int fx = find(x), fy = find(y);
if(fx == fy) return 0;
fa[fy] = fx;
return 1;
}
signed main()
{
DDLC_ESCAPE_PLAN_FAILED;
int t;
cin >> t;
while(t--)
{
e.clear();
int n, m, k;
cin >> n >> m >> k;
fors(i, 1, n) fa[i] = i;
int u, v, c;
fors(i, 1, m){
cin >> u >> v >> c;
e.pb({u, v, c});
}
sort(e.begin(), e.end());
int now = n; // 并查集数量，初始为n
bool flag = 0;
int ans = 0;
for(int i = 0; i < m; ++i){
if(i == 0 || e[i].val != e[i - 1].val){
if(now == k) break;
}
if(!join(e[i].l, e[i].r)) continue;
now--;
ans = e[i].val;
}
cout << (now == k ? ans : -1) << endl;
}
return 0;
}


## HDU6957 Maximal submatrix

（比赛时自己凹的二维dp一直超时，应该是复杂度超过 O ( n 2 ) O(n^2) 了）

#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
// #define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn = 2005;
bool v[maxn][maxn];
int l[maxn][maxn];
int r[maxn][maxn];
int u[maxn][maxn];
signed main()
{
DDLC_ESCAPE_PLAN_FAILED;
int t;
cin >> t;
while(t--)
{
mem(v), mem(l), mem(r), mem(u);
int n, m;
cin >> n >> m;
fors(i, 1, n){
fors(j, 1, m){
cin >> u[i][j];
l[i][j] = j;
r[i][j] = j;
}
}
fors(i, 1, n){
fors(j, 1, m){
if(i == 1 || u[i][j] >= u[i - 1][j]){
v[i][j] = 1;
}
}
}
fors(i, 1, n){
fors(j, 2, m){
if(v[i][j] && v[i][j - 1]) l[i][j] = l[i][j - 1];
}
for(int j = m - 1; j >= 1; --j){
if(v[i][j] && v[i][j + 1]) r[i][j] = r[i][j + 1];
}
}
mem(u);
fors(i, 1, n){
fors(j, 1, m) u[i][j] = 1;
}
int ans = 0;
fors(i, 1, n){
fors(j, 1, m){
if(v[i][j] && v[i - 1][j]){
l[i][j] = max(l[i - 1][j], l[i][j]);
}
if(v[i][j]) u[i][j] = u[i - 1][j] + 1;
}
for(int j = m; j >= 1; --j){
if(v[i][j] && v[i - 1][j]){
r[i][j] = min(r[i - 1][j], r[i][j]);
}
}
}
fors(i, 1, n){
fors(j, 1, m){
ans = max(ans, u[i][j] * (r[i][j] - l[i][j] + 1));
}
}
cout << ans << endl;
}
return 0;
}


## HDU6955 Xor sum

/**
* @file    :vsDebug.cpp
* @brief   :
* @date    :2021-07-21
* @Motto   :Love Sakurai Yamauchi Forever
*/
#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
// #define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn = 1e5 + 10;
const int mnx = (1LL << 24) + 10;
int p[mnx][2], res[mnx], a[maxn];
signed main()
{
DDLC_ESCAPE_PLAN_FAILED;
int t;
cin >> t;
while(t--)
{
int n, k;
cin >> n >> k;
fors(i, 1, n){
cin >> a[i];
a[i] ^= a[i - 1]; // 只存前缀和
}
int l = -1, r = n, ans = 1;
res[1] = -1;
p[1][0] = p[1][1] = 0; // init
fors(i, 0, n){
int x = 1;
int tmp = -1;
for(int j = 29; j >= 0; --j){
if(!x) break;
int u = (a[i] >> j) & 1; // 从上往下第j位
if(!((k >> j) & 1)){
if(p[x][u ^ 1]){
tmp = max(tmp, res[p[x][u ^ 1]]);
}
x = p[x][u];
}
else x = p[x][u ^ 1];
}
if(x) tmp = max(tmp, res[x]);
if(tmp >= 0 && i - tmp < r - l) l = tmp, r = i;
x = 1;
for(int j = 29; j >= 0; --j){
int u = (a[i] >> j) & 1;
if(!p[x][u]){
ans++;
p[x][u] = ans, res[ans] = -1;
p[ans][0] = p[ans][1] = 0;
}
x = p[x][u];
res[x] = max(res[x], i);
}
}
if(l >= 0) cout << l + 1 << ' ' << r << endl;
else cout << -1 << endl;
}
return 0;
}