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

  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. vuex使用一

    至于为什么使用vuex在这里就不过多的解释了,直接进入正题 1.vuex的安装 cnpm install vuex -S 2.然后在main.js中引入 import Vue from 'vue' i ...

  2. Service Fabric下删除实例并注销应用

    Service Fabric下删除实例并注销应用: 以应用名称:Application1为例 1.打开PowerShell 2.连接集群: Connect-ServiceFabricCluster - ...

  3. MYSQLi数据访问查询数据

    单条件查询 <body> <div align="center" style="width:90%;"> <h1>数据查询& ...

  4. UVa-12563 劲歌金曲

    题目 https://vjudge.net/problem/Uva-12563 给出n首歌和KTV的剩余时间T,因为KTV不会在时间到的时候立刻把歌切掉,而是会等它放完.而<劲歌金曲>长达 ...

  5. sitecore系统教程之禁用xDB和Xdb跟踪

    Sitecore体验管理包含未启用体验数据库(xDB)且无需购买xDB许可证情况下使用Sitecore内容管理系统. 除了在未启用xDB的情况下运行Sitecore Experience Platfo ...

  6. [转][LoadRunner]LR性能测试结果样例分析

    LR性能测试结果样例分析 测试结果分析 LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源 ...

  7. jQuery选择器--selector1,selector2,selectorN和ancestor descendant

        selector1,selector2,selectorN 概述 将每一个选择器匹配到的元素合并后一起返回.你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内 参数 selector1 ...

  8. codeoforces 975B Mancala

    题意: 一个游戏,有14个洞,每个洞中开始有若干个球或者没有球. 每一步的操作,是将一个洞中的所有球取出,再逆时针放一个球到它的后一个洞,后两个洞,后三个洞....如果当前放的是最后一个,那么下一个又 ...

  9. url组成

  10. linux 虚拟机挂载光驱

    step 1 step 2 step 3 挂载 root@vm-xiluhua /dev # mount cdrom /mnt mount: /dev/sr0 写保护,将以只读方式挂载 step 4 ...