problem1 link

枚举指数,然后判断是不是素数即可。

problem2 link

令$f[len][a][b][r]$(r=0或者1)表示子串$init[a,a+len-1]$匹配$goal[b,b+len-1]$,翻转了$r$次的最小代价。

problem3 link

答案的公式很容易推导,为$n*\sum_{i=n-k+1}^{n}\frac{1}{i}$.

调和级数为$H(n)=\sum_{i=1}^{n}\frac{1}{i}$

所以答案为$n*(H(n)-H(n-k))$

由于$n$较大,不能枚举,这里有它的近似公式:

$\frac{1}{24(n+1)^{2}}<H(n)-ln(n+\frac{1}{2})-\gamma<\frac{1}{24n^{2}}$

所以$H(n)-H(n-k)\approx ln(\frac{n+\frac{1}{2}}{n-k+\frac{1}{2}})=ln(\frac{2n+1}{2n-2k+1})$

所以对于$H(n)$的较小部分暴力,然后用近似公式。

code for problem1

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class StrongPrimePower { public int[] baseAndExponent(String n) {
final long m=Long.valueOf(n);
for(int i=2;i<=60;++i) {
final int k=cal(i,m);
if(k==-1) {
continue;
}
if(isprime(k)) {
return new int[]{k,i};
}
}
return new int[0];
} int cal(int k,long m) {
int low=1,high=(int)Math.sqrt(m)+1;
int result=1;
while(low<=high) {
int mid=(low+high)>>1;
if(pow(mid,k,m)>m) {
high=mid-1;
}
else {
result=Math.max(result,mid);
low=mid+1;
}
}
if(pow(result,k,m)==m) {
return result;
}
return -1;
} long pow(long a,long b,long n) {
long result=1;
while(b>0) {
if(1==(b&1)) {
if(result>n/a) {
return n+1;
}
result*=a;
if(b==1) {
return result;
}
}
if(a>n/a) {
return n+1;
}
a=a*a;
b>>=1;
}
return result;
} boolean isprime(int n) {
if(n==1) {
return false;
}
for(long i=2;i*i<=n;++i) {
if(n%i==0) {
return false;
}
}
return true;
}
}

  

code for problem2

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ReversalChain { static final int INF=10000000; String init=null;
String goal=null;
int n;
int[][][][] f=null; public int minReversal(String init,String goal) {
this.init=init;
this.goal=goal;
n=init.length();
f=new int[n+1][n][n][2];
for(int i=0;i<n+1;++i) {
for(int j=0;j<n;++j) {
for(int k=0;k<n;++k) {
for(int t=0;t<2;++t) {
f[i][j][k][t]=-1;
}
}
}
}
int result=dfs(n,0,0,0);
if(result>=INF) {
return -1;
}
return result;
} int dfs(int len,int a,int b,int t) {
if(len==0) {
return 0;
}
if(len==1) {
return init.charAt(a)==goal.charAt(b)?0:INF;
}
if(f[len][a][b][t]!=-1) {
return f[len][a][b][t];
}
f[len][a][b][t]=INF; if(t==0) {
if(init.charAt(a)==goal.charAt(b)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a+1,b+1,0));
}
if(init.charAt(a+len-1)==goal.charAt(b+len-1)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a,b,0));
}
if(init.charAt(a)==goal.charAt(b+len-1)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a+1,b,1)+1);
}
if(init.charAt(a+len-1)==goal.charAt(b)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a,b+1,1)+1);
}
}
else {
if(init.charAt(a)==goal.charAt(b)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a+1,b+1,0)+1);
}
if(init.charAt(a+len-1)==goal.charAt(b+len-1)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a,b,0)+1);
}
if(init.charAt(a)==goal.charAt(b+len-1)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a+1,b,1));
}
if(init.charAt(a+len-1)==goal.charAt(b)) {
f[len][a][b][t]=Math.min(f[len][a][b][t],dfs(len-1,a,b+1,1));
}
} return f[len][a][b][t];
}
}

  

code for problem3

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class CollectingBonuses { final static int LIMIT=10000000; public double expectedBuy(String n, String k) {
long nn=Long.valueOf(n);
long mm=Long.valueOf(k);
long m=nn-mm+1;
double result=0;
while(m<=LIMIT) {
result+=1.0/m;
if(m==nn) {
return nn*result;
}
++m;
}
result+=Math.log1p((2*nn-2*m+2.0)/(2.0*m-1));
return nn*result;
}
}

  

topcoder srm 400 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 635 div1

    problem1 link 首先枚举长度$L$.然后计算每一段长度$L$的差值最大公约数,然后差值除以最大公约数的结果可以作为当前段的关键字.然后不同段就可以比较他们的关键字,一样就是可以转化的. p ...

  4. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  5. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  6. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  7. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  8. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  9. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

随机推荐

  1. C#6.0中10大新特性的应用和总结

    微软发布C#6.0.VS2015等系列产品也有一段时间了,但是网上的教程却不多,这里真对C#6.0给大家做了一些示例,分享给大家.   微软于2015年7月21日发布了Visual Studio 20 ...

  2. Web API 跨域问题

    解决办法: 1.web.config <system.webServer> <handlers> <remove name="ExtensionlessUrlH ...

  3. 06 str() bytes() 编码转换

    x = str() #创建字符串#转换成字符串,字节,编码 m = bytes()#创建字节#转换成字节,字符串,要编程什么编码类型的字节 a = "李露" b1 = bytes( ...

  4. [转]Hive开发经验问答式总结

    本文转载自:http://www.crazyant.net/1625.html 本文是自己开发Hive经验的总结,希望对大家有所帮助,有问题请留言交流. Hive开发经验思维导图 Hive开发经验总结 ...

  5. Hive和sparksql中的dayofweek

    dayofweek在hive2.2.0开始支持 ,低版本的hive没有提供原生的dayofweek函数,有时需要用到的时候不甚方便.其实低版本的sparksql和hive中可用以下方式实现dayofw ...

  6. C语言学习感受

    C语言,是我学习的第一种计算机语言,是他作为我编写程序的开始,在学习的时候,先学习了最基础的知识,在语言的理论学习语法上,我逐渐的了解了C语言并且对他有了基础的认识与理解,随着学习内容的不断深入,我逐 ...

  7. HashMap集合存储自定义类

    第一种情况,key为String,value为自定义类person类: 输出结果,key重复的被去掉了,key重复的那个value值之前的被最后一个覆盖了: 第二种情况,key为自定义类person类 ...

  8. Linux基础命令---apwatch

    arpwatch      arpwatch指令可以监听网络设备和ip地址的对应关系,将发现的信息发送到系统日志“/var/log/message”. 此命令的适用范围:RedHat.RHEL.Ubu ...

  9. 如何使用Wisdom RESTClient定制满足您个性化需求的API文档?

    Wisdom RESTClient 支持自动化测试RESTful API,输出精美的测试报告,生成精美的RESTful API文档. 这里介绍一下如何定制个性化的RESTful API文档. 定制个性 ...

  10. 对gulp的理解和使用(一)

    说的gulp,到底是什么?用来做什么的? 以前并没有想过这个问题,拿到公司的项目脚手架就开始做事情了.现在专门来总结一下. gulp干什么的呢? gulp是node中的一种代码构建工具,还有就是项目自 ...