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

  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. CentOS中无法使用setup命令 -bash:setup: command not found

    出现这个问题是因为 Minimal 安装模式 所以并没有安装 setuptool 软件. 解决办法为: 使用yum 源直接下载安装 或者 去下载 setuptool 软件包安装 #安装setuptoo ...

  2. javascript(二):数据类型&数值

    第一部分:数据类型 javascript数据类型通常来说是6种(ES6新增第七种Symbol类型) number:数值 string:字符串 boolean:布尔类型,true或false undef ...

  3. 【转】Loadrunner 性能指标定位系统瓶颈

    转至:http://www.51testing.com/html/63/n-1224463.html Loadrunner 性能指标定位系统瓶颈 判断CPU瓶颈 1, %processor time ...

  4. 20155228 2016-2017-2 《Java程序设计》第5周学习总结

    20155228 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 异常处理 try-catch语法:JVM执行try区块中的代码,如果发生错误就会跳到catc ...

  5. Hive中变量的使用

    1.Hive配置属性 (1)命令行方式 Hive配置属性存储于 hiveconf 命名空间中,该命名空间中的属性是可读写的.在查询语句中插入 '${hiveconf:变量名}',就可以通过 hive ...

  6. UVA 11100 The Trip, 2007 (贪心)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. Codeforce 294A - Shaass and Oskols (模拟)

    Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to ...

  8. C# Http文件上传下载

    C# Http文件下载公共类(支持断点续传) http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html C# Http方式下载文件到本 ...

  9. [转载] 关于出现“使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式”错误的可能原因

    1. 对于该问题确实存在UNION前后SELECT语句中的列数不同导致:2. 以下为个人遇到的一种可能:在项目开发中由于有张表是动态的,即有个基础表,其他的表按年月根据基础表来生成动态表,动态表结构和 ...

  10. GoldenGate 12.3 MA架构介绍系列(2) - 数据同步测试

    安装配置可参考上一篇:http://www.cnblogs.com/margiex/p/8071957.html 安装完成之后,会自动启动ServiceManager服务,此时,可以通过浏览器访问. ...