topcoder srm 370 div1
problem1 link
枚举每一种大于等于$n$的计算其概率即可。
problem2 link
首先二分答案,然后计算。令$f[i][j]$表示移动完前$i$最后一个在位置$j$的最小代价。
problem3 link
假如一个数质因子分解为$n=p_{1}^{x_{1}}p_{2}^{x_{2}}..p_{t}^{x_{t}}$,那么其约数的个数为$(x_{1}+1)(x_{2}+1)..(x_{t}+1)$
所以只需要将$k$分解成若干数字之积,然后分配每个约数最小的一些质数即可。
令$f[i][j]$表示到第$i$个质数,还剩下的数字之积为$j$的最小值。
code for problem1
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class DrawingMarbles { public double sameColor(int[] colors,int n) {
int m=0;
for(int i=0;i<colors.length;++i) {
m+=colors[i];
}
double result=0;
for(int i=0;i<colors.length;++i) {
if(colors[i]<n) {
continue;
}
double t=1;
for(int k=0;k<n;++k) {
t*=1.0*(colors[i]-k)/(m-k);
}
result+=t;
}
return result;
}
}
code for problem2
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class ConnectTheCities { public int minimalRange(int distance, int funds, int[] position) {
Arrays.sort(position);
int low=1,high=distance;
int result=high;
while(low<=high) {
int mid=(low+high)>>1;
if(check(mid,distance,funds,position)) {
result=Math.min(result,mid);
high=mid-1;
}
else {
low=mid+1;
}
}
return result;
}
boolean check(int mid,int distance,int funds,int[] positions) {
final int n=positions.length;
int[][] f=new int[n+1][distance+1];
for(int i=0;i<n+1;++i) {
for(int j=0;j<distance+1;++j) {
f[i][j]=-1;
}
}
f[0][0]=0;
for(int i=1;i<=n;++i) {
for(int j=0;j<distance+1;++j) {
if(-1==f[i-1][j]) {
continue;
}
for(int k=j;k<=j+mid&&k<=distance;++k) {
final int cost=f[i-1][j]+Math.abs(positions[i-1]-k);
if(cost>funds) {
continue;
}
if(f[i][k]==-1||f[i][k]>cost) {
f[i][k]=cost;
}
}
}
}
for(int i=distance-mid;i<=distance;++i) {
if(i>=0&&f[n][i]!=-1) {
return true;
}
}
return false;
}
}
code for problem3
import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class NumberOfDivisors { final static int N=100;
final static int MAX=50000;
final static long INF=1000000000000000000L; long[][] f=new long[N][MAX+1];
int[] p=new int[N]; public long smallestNumber(int n) {
if(n==1) {
return 1;
}
for(int i=0,k=2;i<N;++i) {
while(!isPrime(k)) {
++k;
}
p[i]=k++;
}
for(int i=0;i<N;++i) {
for(int j=0;j<n+1;++j) {
f[i][j]=-1;
}
}
return dfs(0,n)<=INF?dfs(0,n):-1;
} long dfs(int id,int n) {
if(n==1) {
return 1;
}
if(f[id][n]!=-1) {
return f[id][n];
}
f[id][n]=pow(p[id],n-1);
for(int i=2;i*i<=n;++i) {
if(n%i!=0) {
continue;
}
for(int j=0;j<2;++j) {
final int cur=j==0?i:n/i;
final int nxt=n/cur;
long t=pow(p[id],cur-1);
if(t>INF/dfs(id+1,nxt)) {
t=INF+1;
}
else {
t*=dfs(id+1,nxt);
}
if(f[id][n]>t&&t!=INF+1) {
f[id][n]=t;
}
}
}
return f[id][n];
} long pow(long n,long m) {
long result=1;
while(m>0) {
if(1==(m&1)) {
if(result>INF/n) {
return INF+1;
}
result*=n;
if(m==1) {
break;
}
}
if(n>INF/n) {
return INF+1;
}
n=n*n;
m>>=1;
}
return result;
} boolean isPrime(int x) {
for(int i=2;i*i<=x;++i) {
if(x%i==0) {
return false;
}
}
return true;
}
}
topcoder srm 370 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)其 ...
随机推荐
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- Oracle 23的用户管理
创建用户的语法格式 Create user <user_name> Identified by<password> Default tabespace<default t ...
- Bukkit编程之动态向yml文件中添加属性
yaml = new Yaml(); String goods = args[0]; String goodsNum = args[1]; YamlConfiguration yc = new Yam ...
- linux 监控脚本运行时间
虽然可以使用time命令,但是有时候会有写日志之类的需求. 使用如下脚本可以计算时间: #!/bin/bash sdate=`date +%s.%N` edate=`date +%s.%N` echo ...
- Spring MVC / Boot
https://stackoverflow.com/questions/5690228/spring-mvc-how-to-return-image-in-responsebody http://hw ...
- QT5中QByteArray转QString中文乱码
1.添加头文件 #include <QTextCodec> 2.用QTextCodec 设置格式转换 QByteArray barr; barr.insert(0,(char*)(pMsg ...
- HTTP方法之GET与POST对比
超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信.最常用的是GET与POST 1.GET方法: 查询字符串(键/值对)是在GET请求的URL中发送的. /test.php?a=val ...
- Yii ActiveRecord生命周期
- GCD(莫比乌斯+去重)
题目链接 莫比乌斯反演模板题, 去重即可: 我们可以发现只有在区间重叠部分才会有重复且为cal(e, e, k)/2;(e表示b, d中较小的一个): #include<cstdio> # ...
- C++笔试题2(基础题)
温馨提醒:此文续<C++笔试题(基础题)> (112)请写出下列程序的输出内容 代码如下: #include <iostream> using namespace std; c ...