topcoder srm 640 div1
problem1 link
首先使用两个端点颜色不同的边进行连通。答案是$n-1-m$。其中$m$是联通分量的个数。
problem2 link
首先构造一个最小割的模型。左边的$n_{1}$个点与源点相连,右边的$n_{2}$个点与汇点相连。每个中间点最少有$d+1$条边(有一条到汇点/源点的边)。最小割为$ans$.
假设有$x$个割边出现在源点和$n_{1}$之间,那么$y=ans-x$个出现在$n_{2}$和汇点之间。其中$x,y$应该满足的关系为$0\leq y=ans-x \leq n_{2},0\leq x \leq n_{1},and\leq min(n_{1},n_{2})\rightarrow 0\leq x \leq ans$
左边剩下的$n_{1}-x$个点与右边剩下的$n_{2}-y$个点之间不能有边存在,否则最小割会变大。所以总的边的数量为$f(x)=n_{1}n_{2}-(n_{1}-x)(n_{2}-y)=n_{1}n_{2}-(n_{1}-x)(n_{2}-(ans-x))$
$f(x)$的最大值在$x=\frac{n_{1}-n_{2}+ans}{2}$时取得。
$0 \leq x=\frac{n_{1}-n_{2}+ans}{2} \leq ans\rightarrow -ans\leq n_{1}-n_{2}\leq ans$。当$n_{1}-n_{2}$不在这个范围时,枚举边界即可。
problem3 link
如果两个数字$x,y$的差$x-y$能被数$p$整除,那么有$x\equiv y(mod(p))$。
所以可以对每个素数计算有那些数对$(A_{i},B_{j})$的余数相等,他们的差值就含有素数$p$。最后就剩下那些素数特别大的。
这个题目应该有几组特别刁钻的测试数据,代码一直超时。
code for problem1
#include <vector>
class ChristmasTreeDecoration {
public:
int solve(const std::vector<int> &col, const std::vector<int> &x,
const std::vector<int> &y) {
int n = static_cast<int>(col.size());
int m = static_cast<int>(x.size());
father_.resize(n);
for (int i = 0; i < n; ++i) {
father_[i] = i;
}
int number = 0;
for (int i = 0; i < m; ++i) {
int u = x[i] - 1;
int v = y[i] - 1;
if (col[u] != col[v]) {
int pu = GetRoot(u);
int pv = GetRoot(v);
if (pu != pv) {
father_[pu] = pv;
++number;
}
}
}
return n - 1 - number;
}
private:
int GetRoot(int x) {
if (father_[x] == x) {
return x;
}
return father_[x] = GetRoot(father_[x]);
}
std::vector<int> father_;
};
code for problem2
#include <algorithm>
class MaximumBipartiteMatchingProblem {
public:
long long solve(int n1, int n2, int ans, int d) {
long long result = -1;
auto Update = [&](int x) {
if (x < d || ans - x < d) {
return;
}
result = std::max(result, 1ll * n2 * x + 1ll * (n1 - x) * (ans - x));
};
if (n1 > n2) {
std::swap(n1, n2);
}
if (n1 == ans) {
return 1ll * n1 * n2;
}
Update(0);
Update(d);
Update(ans - d);
Update(n1);
Update(n2);
Update((ans + n1 - n2) / 2);
Update((ans + n1 - n2) / 2 + 1);
return result;
}
};
code for problem3
#include <algorithm>
#include <vector> constexpr int kMax = 1000;
constexpr int kMaxPrime = 31622; int diff[kMax][kMax];
int values[kMax][kMax]; int nxt[kMax];
int head[kMaxPrime];
int tag[kMaxPrime]; int prime_tag[kMaxPrime]; class TwoNumberGroups {
static constexpr int kMod = 1000000007; public:
int solve(const std::vector<int> &A, const std::vector<int> &numA,
const std::vector<int> &B, const std::vector<int> &numB) {
int n = static_cast<int>(A.size());
int m = static_cast<int>(B.size());
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
diff[i][j] = std::abs(A[i] - B[j]);
}
}
int result = 0;
for (int p = 2; p < kMaxPrime; ++p) {
if (prime_tag[p] != 1) {
for (int x = p + p; x < kMaxPrime; x += p) {
prime_tag[x] = 1;
}
for (int i = 0; i < n; ++i) {
int t = A[i] % p;
if (tag[t] != p) {
head[t] = -1;
tag[t] = p;
}
nxt[i] = head[t];
head[t] = i;
}
for (int i = 0; i < m; ++i) {
int t = B[i] % p;
if (tag[t] == p) {
for (int j = head[t]; j != -1; j = nxt[j]) {
if (A[j] != B[i]) {
values[j][i] += p;
while (diff[j][i] % p == 0) {
diff[j][i] /= p;
}
}
}
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (diff[i][j] > 1) {
values[i][j] += diff[i][j];
}
result += 1ll * numA[i] * numB[j] % kMod * values[i][j] % kMod;
if (result >= kMod) {
result -= kMod;
}
}
}
return result;
}
};
topcoder srm 640 div1的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
随机推荐
- python遍历文件
#!/usr/local/bin/python # -*- coding: UTF-8 -*- #coding:gbk import re import os rootdir = 'src' def ...
- 洛谷P4640 王之财宝 [BJWC2008] 数论
正解:容斥+Lucas+组合数学 解题报告: 传送门! 和上一篇题解的题差不多,,,双倍经验趴大概算 还是说下还是有点儿区别的来着$QwQ$ 两个小差别分别港下$QwQ$ 首先有$m-n$件是无穷个的 ...
- [PCL]模型拟合方法——随机采样一致性
SACSegmentation封装了多种Ransac方法,包括: RandomSampleConsensus, LeastMedianSquares, MEstimatorSampleConsensu ...
- Django 分组 聚合
base_sql = Order.objects.filter(is_paid=True, merchant=merchant_id) # 如果aggregate前没有values,得到的结果是一个字 ...
- mybatisplus ssm配置要点
本以为不难,但也捣鼓了大半天,记录要点如下: 在pom中引入mybatis plus相关包 <!-- mybatis-plus框架包 start --> <dependency> ...
- Elasticsearch.安装插件(head)
Elasticsearch.安装插件(head) 环境: Linux 7.x jdk1.8 目录结构(跟目录多了两个文件) /resources ### 存放软件源 /u01/ ...
- python [[1,2],[3,4],[5,6]]一行代码展开该列表,得出[1,2,3,4,5,6]
#1)利用推导式运行过程:for i in a ,每个i是[1,2],[3,4],[5,6],for j in i,每个j就是1,2,3,4,5,6,合并后就是结果 a=[[1,2],[3,4],[5 ...
- Ubuntu16.04彻底删除PHP7.2
一.删除php的相关包及配置 apt-get autoremove php7* 二.删除关联 sudo find /etc -name "*php*" |xargs rm -rf ...
- web自动化测试python+selenium学习总结----python编辑器pycharm环境安装
下载安装文件 下载最新文件路径:https://www.jetbrains.com/pycharm/ 安装: 一直点击下一步即可 破解: 配置hosts文件.C:\Windows\System32\d ...
- springboot引入AOP
AOP是Aspect Oriented Programming的缩写,意为面向切面编程.通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是spring框架的一个重要内容,她通过对 ...