topcoder srm 495 div1
problem1 link
从前向后确定一下,然后再从后向前确定一下。一样的话就是可以确定的。
problem2 link
首先将强连通分量缩点。理论上来说,只需要遍历所有入度为0的联通块中的一个即可。
但是有一种情况可能是某个入度为0的联通块只包含一个节点$u$,这时当遍历完其他入度为0的联通块时即可确定节点$u$。
problem3 link
当$N$不能整除$H$时无解。
设连续的电梯长度为$L$。那么两个相邻的连续电梯块之间的长度也一定是$L$的倍数。
设$f(H,N)$表示$L>1$的答案,$g(H,N)$表示$L=1$的答案,那么题目的答案为$f(H,N)+g(H,N)$
其中$f(H,N)=\sum_{L|N,L>1}g(\frac{H}{L},\frac{N}{L})$
那么剩下的问题是如何计算$g(H,N)$。首先当$N=1$时,$g(H,N)=1$。
否则,设$N$节电梯当=的位置分别为$a_{0},a_{1},...,a_{N-1}$,其中$a_{0}=0$。电梯一共要停$\frac{H}{N}$次。设第$i$次停的时候最下面一节电梯所停的楼层为$b_{i}$。显然$b_{0}=0$
第一次停的楼层为 $a_{0}+b_{0},a_{1}+b_{0},...,a_{N-1}+b_{0}$
第二次停的楼层为 $a_{0}+b_{1},a_{1}+b_{1},...,a_{N-1}+b_{1}$
那么很显然$a_{0}+b_{1}=1$,因为$a_{1}+b_{0}=a_{1}\ne 1$
所以$b_{1}=1$
对于某一层楼$x$,一定存在唯一的二元组$(i,j)$满足$x=a_{i}+b_{j}$
交换数组$a,b$,可以看作现在是$\frac{H}{N}$节电梯,停$N$次。因此
$g(H,N)=\left\{\begin{matrix}1 & N=1\\ f(H,\frac{H}{N}) & N > 1\end{matrix}\right.$
code for problem1
public class ColorfulCards {
public int[] theCards(int N, String colors) {
boolean[] p = new boolean[N + 1];
for (int i = 1; i <= N; ++ i) {
p[i] = isprime(i);
}
final int x = colors.length();
int[] f = new int[x];
for (int id = 1, i = 0; i < x; ++ i) {
while (id <= N && !match(colors.charAt(i), p[id])) {
++ id;
}
if (id > N) {
f[i] = -1;
}
else {
f[i] = id ++;
}
}
for (int id = N, i = x - 1; i >= 0; -- i) {
while (id > 1 && !match(colors.charAt(i), p[id])) {
-- id;
}
if (id < 1) {
f[i] = -1;
}
else {
if (f[i] != id) {
f[i] = -1;
}
-- id;
}
}
return f;
}
static boolean match(char c, boolean b) {
return c == 'R' && b || c == 'B' && !b;
}
static boolean isprime(int x) {
if (x == 1) {
return false;
}
for (int i = 2; i * i <= x; ++ i) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
code for problem2
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class CarrotBoxes { public double theProbability(String[] information) {
final int n = information.length;
boolean[][] g = new boolean[n][n];
for (int i = 0; i < n; ++ i) {
for (int j = 0; j < n; ++ j) {
g[i][j] = (information[i].charAt(j) == 'Y');
}
}
for (int i = 0; i < n; ++ i) {
for (int j = 0; j < n; ++ j) {
for (int k = 0; k < n; ++ k) {
if (g[j][i] && g[i][k]) {
g[j][k] = true;
}
}
}
}
List<Integer> top = new ArrayList<>();
boolean[] tag = new boolean[n];
for (int i = 0; i < n; ++ i) {
if (tag[i]) {
continue;
}
int ind = 0;
for (int j = 0; j < n; ++ j) {
if (g[j][i] && g[i][j]) {
tag[j] = true;
continue;
}
if (g[j][i]) {
++ ind;
break;
}
}
if (0 == ind) {
top.add(i);
}
}
final long all = (1l << n) - 1;
for (int i = 0; i < top.size(); ++ i) {
final int last = top.get(i);
long st = 0;
for (int j = 0; j < top.size(); ++ j) {
if (j == i) {
continue;
}
final int t = top.get(j);
for (int k = 0; k < n; ++ k) {
if (k != last && g[t][k]) {
st |= 1l << k;
}
}
}
if (st == (all ^ (1l << last))) {
return 1.0 * (n - (top.size() - 1)) / n;
}
}
return 1.0 * (n - top.size()) / n;
}
}
code for problem3
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class StrangeElevator {
final static long B = 1000000001;
final static int mod = 1000000007; Map<Long, Integer> Gmap = new HashMap<>();
Map<Long, Integer> Fmap = new HashMap<>(); public int theCount(int H, int N) {
if (H % N != 0) {
return 0;
}
return (F(H, N) + G(H, N)) % mod;
}
int G(int H, int N) {
if (N == 1) {
return 1;
}
if (Gmap.containsKey(H * B + N)) {
return Gmap.get(H * B + N);
}
int result = F(H, H / N);
Gmap.put(H * B + N, result);
return result;
} int F(int H, int N) {
if (Fmap.containsKey(H * B + N)) {
return Fmap.get(H * B + N);
}
int result = 0;
for (int i = 1; i * i <= N; ++ i) {
if (N % i == 0) {
if (i > 1) {
result += G(H / i, N / i);
result %= mod;
}
if (i *i != N) {
result += G(H / (N / i), i);
result %= mod;
}
}
}
Fmap.put(H * B + N, result);
return result;
}
}
topcoder srm 495 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)其 ...
随机推荐
- ProxySQL(读写分离)部署
proxySQL是MySQL的中间件产品,是灵活强大的代理层,实现读写分离,支持Query路由功能,支持动态指定某个SQL进行缓存,支持动态加载配置,故障切换和一些SQL 过滤功能 环境: 192.1 ...
- C# Dapper 简单实例
/// <summary> /// 分页信息 /// </summary> public class PageInfo<T> { /// & ...
- IntelliJ IDEA 2017.3/2018.1 激活
传统的License Server方式已经无法注册IntelliJ IDEA2017.3的版本了. http://idea.lanyus.com,这个网站有破解补丁和注册码两种方式,另外http:// ...
- mysql 5.6 每天凌晨12:00 重置sequence表中的某个值
#.创建evevt要调用的存储过程update_current_value_procedure delimiter // drop procedure if exists update_current ...
- Unity shader学习之屏幕后期处理效果之边缘检测
边缘检测的原理是利用一些边缘检测算子对图像进行卷积操作. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7232707.html 例如: 代码如下: usin ...
- SQLGetStmtAttr
SQLGetStmtAttr 函数定义: SQLRETURN SQLGetStmtAttr( SQLHSTMT StatementHandle, SQLINTEGER Attribut ...
- 16. 3Sum Closest(双指针)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- sql server得到某个数据库的所有表和所有字段
select b.name tablename,a.name as columnnamefrom sys.columns a,sys.objects b,sys.types cwhere a.obje ...
- SolidWorks242个使用技巧
1 您可以使用 CTRL+TAB 键循环进入在 SolidWorks 中打开的文件. 2 使用方向键可以旋转模型.按 CTRL 键加上方向键可以移动模型.按 ALT 键加上方向键可以将模型沿顺时针或逆 ...
- Lua 服务器Socket通信实例
local socket = require"socket" local host = "127.0.0.1"local port = "843&qu ...