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的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  8. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  9. 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)其 ...

随机推荐

  1. jmeter数据库连接配置

    一.实际试过的mysql配置 1.导入一个JDBC jar包(我是直接把jar包放在了jmeter的lib目录),包:mysql-connector-java-5.1.7-bin.jar 2.设置JD ...

  2. tool 使用font-spider解决字体压缩问题

    开发页面时经常需要使用特殊字体,一个.ttf文件动则几M,字体文件需要优化 http://font-spider.org/ 安装好node环境后 1.全局安装font-spider npm insta ...

  3. linux sed 用法

    目录 Overview 命令行选项 Command-Line Options manual http://www.gnu.org/software/sed/manual/sed.html Overvi ...

  4. hisi35xx串口驱动的完善

    鉴于hisi的uart3还没有打通,ls /dev/ | grep ttyA* ,看到的只有ttyAMA0 和ttyAMA1,且使用应用程序打开ttyAMA1设备后,使用write函数,发送,示波器观 ...

  5. studio-3t 配置文件位置

    换电脑了,原来的studio-3t的配置 在 C:\Users\用户名\.3T. 将这个目录下的所有文件拷贝到 新电脑里的 相同文件夹,覆盖即可

  6. DS1-14

    #include <stdio.h> #define MAXSIZE 10000 int MaxSubseqSum4(int List[], int N); int main() { in ...

  7. httpappplication 和 httpmodule 的理解(转载,写的很好)

    第一部分:转载自Artech  IIS与ASP.NET管道 ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET ...

  8. hightopo学习之旅一 -- 节点动画

    参照官网 动画手册 1.引入所需HT文件 <script src="plugins/ht/core/ht.js"></script> <script ...

  9. linux 返回上次历史目录

    我们使用linux的转换目录命令 cd 时经常会遇到想回到cd之前目录的情况,比如不小心按了 cd 回车,跳出了工作目录,又想回到刚刚的目录. 这种情况下,就用到了我们这篇博客的主角 cd - . c ...

  10. G面经: Design Stock Price Display System

    Implement a simple stock price display systemwhich will show High, Low and Last price for a given st ...