topcoder srm 335 div1
problem1 link
直接模拟即可。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class Multifactorial { public String calcMultiFact(int n, int k) {
long result=1;
final long nlimit=1000000000000000000l;
while(true) { if(result>nlimit/n) {
return "overflow";
}
result*=n;
if(n<=k) {
break;
}
n-=k;
}
return Long.toString(result);
}
}
problem2 link
记录到达$(x,y)$的步数以及当前新一步的和,dp即可。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ExpensiveTravel { static class Fraction {
public int a,b;
public Fraction() {
a=1;
b=1;
}
public Fraction(int a,int b) {
this.a=a;
this.b=b;
} public static int gcd(int x,int y) {
if(y==0) {
return x;
}
return gcd(y,x%y);
} private Fraction simple() {
int t=gcd(a,b);
a/=t;
b/=t;
return this;
} public Fraction add(Fraction p) {
int bb=p.b*b;
int aa=a*p.b+p.a*b;
return new Fraction(aa,bb).simple();
}
public boolean ok() {
return a>b;
} public boolean less(Fraction p) {
return a*p.b<p.a*b;
}
} static class Node {
public Fraction pref;
public Fraction f;
public int cost;
public boolean inq; public Node() {
f=new Fraction(0,1);
cost=0;
inq=false;
} Node add(int t) {
Node p=new Node();
p.f=new Fraction(f.a,f.b).add(new Fraction(1,t));
p.cost=cost;
p.inq=inq; if(p.f.ok()) {
++p.cost;
p.f=pref.add(new Fraction(1,t));
}
p.pref=new Fraction(1,t);
return p;
} public boolean less(Node p) {
return cost<p.cost||cost==p.cost&&f.less(p.f);
} public int result() {
if(cost==-1) {
return -1;
}
return cost+1;
} } public int minTime(String[] m, int startRow, int startCol, int endRow, int endCol) {
final int N=m.length;
final int M=m[0].length();
int[][] g=new int[N][M];
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
char c=m[i].charAt(j);
g[i][j]=c-'0';
}
}
--startRow;
--startCol;
--endRow;
--endCol;
if(g[startRow][startCol]==1||g[endRow][endCol]==1) {
return -1;
} Node[][] f=new Node[N][];
for(int i=0;i<N;++i) {
f[i]=new Node[M];
for(int j=0;j<M;++j) {
f[i][j]=new Node();
f[i][j].cost=-1;
}
} Queue<Integer> queue=new LinkedList<>();
f[startRow][startCol].f=new Fraction(1,g[startRow][startCol]);
f[startRow][startCol].pref=new Fraction(1,g[startRow][startCol]);
f[startRow][startCol].inq=true;
f[startRow][startCol].cost=0;
queue.offer(startRow*100+startCol); final int[] dx={0,0,1,-1};
final int[] dy={1,-1,0,0}; while(!queue.isEmpty()) {
final int x=queue.peek()/100;
final int y=queue.peek()%100;
queue.poll();
f[x][y].inq=false;
if(x==endRow&&y==endCol) {
continue;
}
for(int i=0;i<4;++i) {
final int xx=x+dx[i];
final int yy=y+dy[i];
if(xx<0||xx>=N||yy<0||yy>=M) {
continue;
}
if(g[xx][yy]==1) {
continue;
}
Node t=f[x][y].add(g[xx][yy]);
if(f[xx][yy].cost==-1||t.less(f[xx][yy])) {
f[xx][yy]=t;
if(!f[xx][yy].inq) {
f[xx][yy].inq=true;
queue.offer(xx*100+yy);
}
}
}
}
return f[endRow][endCol].result();
}
}
problem3 link
根据期望的可加性,A组中每个数$x$比B组中每个小于$x$的值$y$的贡献值$\frac{(x-y)^{2}}{n}$为正,对于每个大于$x$的值$z$的贡献值$\frac{(x-z)^{2}}{n}$为负。
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class RandomFights { int[] get(int[] X,int n) {
final int m=X.length;
int j=0;
int[] R=new int[n];
for(int i=0;i<n;++i) {
R[i]=X[j];
int s=(j+1)%m;
X[j]=((X[j]^X[s])+13)%49999;
j=s;
}
return R;
} BigInteger int2big(long x) {
return new BigInteger(Long.toString(x));
} public double expectedNrOfPoints(int[] A,int[] B,int n) {
int[] a=get(A,n);
int[] b=get(B,n); Arrays.sort(a);
Arrays.sort(b); BigInteger nxt=BigInteger.ZERO,nxt2=BigInteger.ZERO;
for(int i=0;i<n;++i) {
nxt=nxt.add(int2big(b[i]));
nxt2=nxt2.add(int2big((long)b[i]*b[i]));
} BigInteger result=BigInteger.ZERO;
BigInteger pre=BigInteger.ZERO,pre2=BigInteger.ZERO;
int k=0;
for(int i=0;i<n;++i) {
while(k<n&&b[k]<=a[i]) {
pre=pre.add(int2big(b[k]));
pre2=pre2.add(int2big((long)b[k]*b[k]));
nxt=nxt.subtract(int2big(b[k]));
nxt2=nxt2.subtract(int2big((long)b[k]*b[k]));
++k;
} BigInteger tmp=int2big((long)k*a[i]*a[i]).subtract(pre.multiply(int2big(a[i]*2))).add(pre2);
result=result.add(tmp);
tmp=int2big((long)(n-k)*a[i]*a[i]).subtract(nxt.multiply(int2big(a[i]*2))).add(nxt2);
result=result.subtract(tmp); }
BigInteger[] last=result.divideAndRemainder(int2big(n));
return Double.valueOf(last[0].toString())+Double.valueOf(last[1].toString())/n;
}
}
topcoder srm 335 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)其 ...
随机推荐
- mysql基础常用命令
数据库 1查询 Select * From table select host,user,password from mysql.user where user='ybb' and host='%'; ...
- Ecust DIV3 k进制 【暴力不断优化】
K进制 Description 给定一个正整数n,请你判断在哪些进制下n的表示恰好有2位是1,其余位都是0. Input 输入第一行为整数TT,表示有TT组数据(1 \le T \le 50)(1≤T ...
- 用Hexo在GitHub上搭建个人博客
我用Hexo在GitHub上搭建好了自己的博客,我的这第一篇博客就来说说搭建的过程. 1 环境配置 本文使用环境如下: Windows 10 node.js v8.1.3 git v2.13.2 np ...
- HTML5特性&&canvas
1.HTML5是由W3C(万维网联盟,专注于XHTML 2.0)和WHATWG(专注于web表单和应用程序)共同合作的结果,2014年10月完成标准制定! 主要设计目的:为了在移动设备上支持多媒体. ...
- 元素位置pageX,pageY,clientX,clientY,scrollX,scrollY,screenX,screenY,offsetX,offsetY
总结: event.clientX 设置或获取鼠标指针位置相对于当前窗口的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. (可见区域)event.clientY 设置或获取鼠标指针位置相对于当 ...
- Qt5 信号重载
下面以最常用的QComboBox为例说明. [1]Qt4风格的connect 示例代码: connect(ui->comboBox, SIGNAL(activated(int index)), ...
- Kylin, Mondrian, Saiku系统的整合
本文主要介绍有赞数据团队为了满足在不同维度查看.分析重点指标的需求而搭建的OLAP分析工具.这个工具对Kylin.Mondrian以及Saiku做了一个整合,主要工作包括一些定制化的修改以及环境的配置 ...
- 【Alpha版本】冲刺阶段——Day6
[Alpha版本]冲刺阶段--Day6 阅读目录 今日进展 问题困难 明日任务 今日贡献量 TODOlist [今日进展] 为注册模块增加界面代码 public static void Windows ...
- Linux基础命令---netstat显示网络状态
netstat netstat指令可以显示当前的网络连接.路由表.接口统计信息.伪装连接和多播成员资格等信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.open ...
- java.lang.RuntimeException: can not run elasticsearch as root
忘写了一个错误: [o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main] org.el ...