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)其 ...
随机推荐
- can not create symbolic link HDFS解压自动配置lib报错。
如题,使用FusionInsight解压生成样例代码的时候报错,找不到解释.只猜测是权限问题.然后并没有仔细静心思考,心里杂念很多,很浮躁. 解决方法是“以管理员身份运行“. 想想高中:面对问题,不能 ...
- SpringMVC.入门篇《二》form表单
SpringMVC.入门篇<二>form表单 项目工程结构: 在<springmvc入门篇一.HelloWorld>基础上继续添加代码,新增:FormController.ja ...
- java详解内部类
可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类是一个非常有用的特性但又比较难理解使用的特性(鄙人到现在都没有怎么使用过内部类,对内部类也只是略知一二). 第一次见面 内部类我们从外面 ...
- C#6.0中10大新特性的应用和总结
微软发布C#6.0.VS2015等系列产品也有一段时间了,但是网上的教程却不多,这里真对C#6.0给大家做了一些示例,分享给大家. 微软于2015年7月21日发布了Visual Studio 20 ...
- 参考termux中包管理命令的伪装修改的arch版包管理命令
#!/bin/bash set -e -u show_help() { echo "This help message is useless, please read the content ...
- 页面每隔n分钟轮换一个微信名和微信名
1.前端index.html <head> <meta charset="UTF-8"> <title>n号循环</title> & ...
- html5-盒子模型
/*div{background: green;width: 60%;padding-top: 10px;padding-right: 20px;padding-bottom: 30px;paddin ...
- keras tensorboard的使用
http://blog.csdn.net/xiaojiajia007/article/details/72865764 https://stackoverflow.com/questions/4211 ...
- Python - 4. Control Structures
From:http://interactivepython.org/courselib/static/pythonds/Introduction/ControlStructures.html Cont ...
- 20155228 2016-2017-2 《Java程序设计》第9周学习总结
20155228 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 整合数据库 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则 ...