(这只是一部分,慢慢补==)
6950:Mod, Or and Everything
Problem Description
You are given an integer n.
You are required to calculate (n mod 1) or (n mod 2) or … or (n mod (n - 1)) or (n mod n).
The “or” operation means “bitwise OR”.
Input
The first line contains an integer T(1≤T≤5000)representing the number of test cases.
For each test case, there is an integer n(1≤n≤1012)in one line.
Output
For each test case, print the answer in one line.
Sample Input
5
1
2
3
4
5
Sample Output
0
0
1
1
3
分析:签到题,打个表就能看出规律,不细说了==
/*
* @Author: Shadowlove
* @Date: 2021-06-12 18:59:05
* @LastEditors: Shadowlove
* @LastEditTime: 2021-07-21 15:32:14
* @Description: file content
*/
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
long long pos;
cin >> pos;
if (pos == 1)
{
cout << 0 << endl;
continue;
}
for (int i = 0; i <= 40; i++)
{
if (pow(2, i) < pos && pow(2, i + 1) >= pos)
{
long long ans=pow(2,i)-1;
cout<<ans<<endl;
break;
}
}
}
return 0;
}
6954:Minimum spanning tree
Problem Description
Given n-1 points, numbered from 2 to n, the edge weight between the two points a and b is lcm(a, b). Please find the minimum spanning tree formed by them.
A minimum spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as small as possible.
lcm(a, b) is the smallest positive integer that is divisible by both a and b.
Input
The first line contains a single integer t (t<=100) representing the number of test cases in the input. Then t test cases follow.
The only line of each test case contains one integers n (2<=n<=10000000) as mentioned above.
Output
For each test case, print one integer in one line, which is the minimum spanning tree edge weight sum.
Sample Input
2
2
6
Sample Output
0
26
分析:
题意是说,有2—n这n-1个点,a,b两点间的权值是lcm(a,b),我们要建立出一棵最小生成树,求这颗树的权重和。
很显然,如果i是素数,已经形成的树上没有它的因子,我们就可以把i与2相连,得到的权重是2*i;
如果i是合数,树上必有i的因子,我们把i与它任意一因子相连,得到的权重是i;
我们可以计算3+4+…+n的值,再去遍历从3-n之间的素数,把它们加到刚才的值上就能得到答案。
说白了,就是一道素数筛的题嘛==
/*
* @Author: Shadowlove
* @Date: 2021-07-21 16:23:27
* @LastEditors: Shadowlove
* @LastEditTime: 2021-07-21 16:51:02
* @Description: file content
*/
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
int prime[10000000];
bool b[10000009];
int cnt = 0;
void init()
{
memset(b, 1, sizeof(b));
b[0] = b[1] = 0;
for (int i = 2; i <= 10000008; i++)
{
if (b[i])
prime[++cnt] = i;
for (int j = 1; j <= cnt && prime[j] * i <= 10000008; j++)
{
b[prime[j] * i] = 0;
if (i % prime[j] == 0)
break;
}
}
}
int main()
{
init();
long long t, n;
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
long long ans = 0;
if (n % 2 == 0)
ans = (n / 2) * (n + 1);
else
ans = (n + 1) / 2 * n;
ans -= 3;
for (int i = 2; prime[i] <= n && i <= cnt; i++)
ans += prime[i];
printf("%lld\n", ans);
}
return 0;
}
6957:Maximal submatrix
Problem Description
Given a matrix of n rows and m columns,find the largest area submatrix which is non decreasing on each column
Input
The first line contains an integer T(1≤T≤10)representing the number of test cases.
For each test case, the first line contains two integers n,m(1≤n,m≤2∗103)representing the size of the matrix
the next n line followed. the i-th line contains m integers vij(1≤vij≤5∗103)representing the value of matrix
It is guaranteed that there are no more than 2 testcases with n∗m>10000
Output
For each test case, print a integer representing the Maximal submatrix
Sample Input
1
2 3
1 2 4
2 3 3
Sample Output
4
分析:
题意是对给定矩阵,找出一个面积最大的子矩阵,使得每列都是非递减,求出这个矩阵的面积。
可以用dp做。
假设dp[ i ] [ j ]为以arr[ i ] [ j ]为右下角的满足题意的最大子矩阵的面积,则
dp[i][j]=max(rolen*colen,dp[i][j])
其中colen是前j列中满足非递减的最短列的元素数,这个求出的是得到的子矩阵的行数
对于arr[ i ] [ j ],如果它能使第j列满足非递减,rolen++,否则rolen=0,这个求出的是得到的子矩阵的列数
(好像命名写反了?嘛不要在意这些细节)
大致就是这样,代码里用cnt[i][j]进行辅助记录,具体可以看看代码==
/*
* @Author: Shadowlove
* @Date: 2021-07-21 16:47:33
* @LastEditors: Shadowlove
* @LastEditTime: 2021-07-22 17:49:54
* @Description: file content
*/
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int n, m;
int arr[2023][2023];
int dp[2023][2023];
int cnt[2023][2023];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int maxl = -1;
memset(cnt, 0, sizeof(cnt));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &arr[i][j]);
cnt[i][j] = 1;
dp[i][j] = j;
}
}
for (int i = 1; i <= n; i++)
{
int rolen = 0;
int colen = 9999999;
for (int j = 1; j <= m; j++)
{
if (arr[i][j] >= arr[i - 1][j])
{
rolen++;
cnt[i][j] = cnt[i - 1][j] + 1;
colen = min(cnt[i][j], colen);
if (j == 1)
dp[i][j] = cnt[i][j];
else
dp[i][j] = max(rolen * colen, dp[i][j]);
}
else
rolen = 0;
if (dp[i][j] > maxl)
maxl = dp[i][j];
}
}
printf("%d\n", maxl);
}
return 0;
}
6958:KD-Graph
Problem Description
Let’s call a weighted connected undirected graph of n vertices and m edges KD-Graph, if the
following conditions fulfill:
* n vertices are strictly divided into K groups, each group contains at least one vertice
* if vertices p and q ( p ≠ q ) are in the same group, there must be at least one path between p and q meet the max value in this path is less than or equal to D.
* if vertices p and q ( p ≠ q ) are in different groups, there can’t be any path between p and q meet the max value in this path is less than or equal to D.
You are given a weighted connected undirected graph G of n vertices and m edges and an integer K.
Your task is find the minimum non-negative D which can make there is a way to divide the n vertices into K groups makes G satisfy the definition of KD-Graph.Or −1 if there is no such D exist.
Input
The first line contains an integer T (1≤ T ≤5) representing the number of test cases.
For each test case , there are three integers n,m,k(2≤n≤100000,1≤m≤500000,1≤k≤n) in the first line.
Each of the next m lines contains three integers u,v and c (1≤v,u≤n,v≠u,1≤c≤109) meaning that there is an edge between vertices u and v with weight c.
Output
For each test case print a single integer in a new line.
Sample Input
2
3 2 2
1 2 3
2 3 5
3 2 2
1 2 3
2 3 3
Sample Output
3
-1
分析:
题意是说,对于一个有n个点,m条边的图,把n个点分成k组。如果p,q两个点在同一组,则p和q之间至少有一条路径满足此路径中的最大值小于等于D。
如果不在同一组,p和q之间不能有任何路径满足此路径中的最大值小于等于D。
我们需要求出最小的D。
看起来需要把边权从小到大排序,于是考虑用并查集+克鲁斯卡尔做。
最开始我们有n个连通块。
如果两个点在同一集合则continue;
如果不在同一集合,并且现在的联通块数>k+1,则直接合并两点,代表这两点在同一集合;
如果不在同一集合,并且现在的联通块数==k+1,则直接合并两点,并且记录D=这两点之间的边长,此时正好有k个连通块(也就是k组),且组中最大的边长是D;
如果不在同一集合,并且现在的联通块数<k+1,则需要进行判断。如果当前这两点的边长<=D,则说明p,q不在一组,且pq的长度小于等于D,是无解的情况。否则可以继续进行合并。(此时看的就是p和q不在同一分组的情况了,因为此时把p和q合并在一起分出的组数就小于k了。)
(其实按照克鲁斯卡尔的做法,我们已经把边从小到大排序了,所以这步其实判断边长==D就行 ,也能过的)
/*
* @Author: Shadowlove
* @Date: 2021-06-12 18:59:05
* @LastEditors: Shadowlove
* @LastEditTime: 2021-07-21 19:52:21
* @Description: file content
*/
#include <iostream>
#include <algorithm>
using namespace std;
int f[100331];
int findl(int x)
{
if (f[x] == x)
return x;
return f[x] = findl(f[x]);
}
struct node
{
int x, y, z;
} s[1000009];
bool cmp(node a, node b)
{
return a.z < b.z;
}
int main()
{
//cin.tie(0);
int n, m, k, x, y, t;
scanf("%d", &t);
while (t--)
{
int d = -1;
scanf("%d%d%d", &n, &m, &k);
int sum = n;
for (int i = 1; i <= n; i++)
f[i] = i;
for (int i = 0; i < m; i++)
scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].z);
sort(s, s + m, cmp);
int flag = 0;
if (k == n)
{
printf("0\n");
continue;
}
for (int i = 0; i < m; i++)
{
int x1 = findl(s[i].x);
int x2 = findl(s[i].y);
int l = s[i].z;
if (x1 == x2)
continue;
if (sum >= k + 1)
{
f[x1] = x2;
if (sum == k + 1)
d = l;
}
else
{
if (l <= d)
{
flag = 1;
break;
}
f[x1] = x2;
}
sum--;
}
if (flag == 1 || d == -1)
printf("-1\n");
else
printf("%d\n", d);
}
return 0;
}