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)其 ...
随机推荐
- 【网络设备】某防火墙基于IP地址的目的地址转换
由于来自Internet的对政府,企业的网络攻击日益频繁,因此需要对内网中向外网提供访问服务的关键设备进行有效保护.采用目的地址NAT可以有效地将内部网络地址对外隐藏. 图中:公网中Internet用 ...
- BCB6.0 清除TPanel面板上的所有控件
方法一: panel->ComponentCount属性获得panel所拥有的控件个数 panel->Components[i]属性获得某一个控件 delete panel->Com ...
- mongodb中直接根据某个字段更新另外一个字段值
表:tblCard 要更新的字段:tPAFlow 值字段: pFlow 过滤 条件:{"lCycle":2} db.tblCard.find({"lCycle" ...
- 合并dict、list的方法
dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}合并两个字典得到类似 {1:[1,11,111],2:[2,22,2 ...
- Sitecore CMS中配置模板部分
如何在Sitecore CMS中配置模板部分. 注意: 本教程将扩展于“Sitecore CMS中创建模板”的章节. 配置折叠状态 配置模板部分的折叠状态允许用户选择默认折叠或展开哪些模板部分.此设置 ...
- eclipse更换workspace需要重新设置的内容
.jdk Window-->java-->Installed JREs,新增或修改你所需要的jdk版本,点击需要的jdk-->edit 在Default VM arguments里面 ...
- 变量为空代表false
name = ''#名字为空即代表False while not name:#not name=False即 真,将执行循环体 print('Enter your name:') name = inp ...
- Linux服务器---邮件服务postfix安装
安装postfix postfix是一个快速.易于管理.安全性高的邮件发送服务,可以配合dovecot实现一个完美的邮箱服务器. 1.安装postfix [root@localhost ~]# rpm ...
- 创建一个简单的WCF程序
1.创建WCF服务库 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→WCF→WCF服务库,然后输入项目名称(Name),存放位置(Location ...
- Vue.js是什么,vue介绍
Vue.js是什么,vue介绍 Vue.js 是什么Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. ...