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;
	}
}

  

时间: 2024-08-26 01:15:26

topcoder srm 370 div1的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

topcoder srm 738 div1 FindThePerfectTriangle(枚举)

Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle with the following properties: The coordinates of each vertex are integers between 0 and 3000, inclusive. The perimeter of the triangle must be exactly

Topcoder SRM 603 div1题解

昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点,每个节点都有一个权值,两人A和B分别进行操作,由A先手,每人可以选择一条边,将它删掉得到两个联通块.游戏不断进行下去,最后只剩下一个节点.A希望最后的节点权值尽可能大,B希望尽可能小,求这个最后的值.数据保证n<=50. 这道题真的是博弈好题啊-(感觉放到ACM很合适啊) 我们考虑第一次A会如何选

topcoder srm 320 div1

problem1 link 两个数字后面都有阶乘符号,可以抵消. import java.util.*; import java.math.*; import static java.lang.Math.*; public class ExtraordinarilyLarge { public String compare(String x, String y) { boolean both=false; while(x.endsWith("!")&&y.endsWit

topcoder srm 712 div1 -23

1.给定两个长度为$n$的数组$A,B$.有两种操作可以作用于数组$A$.第一种,将每个元素左侧的相邻数字加到其上:第二种,将每个元素右侧的相邻数字加到其上.0的左侧是$n-1$,$n-1$的右侧是0.比如1,2,3,4执行第一种操作后是5,3,5,7.给出一个不超过100的操作序列使得$A$变成$B$.$n \leq 50$. 思路:将$a_{0},a_{1},...,a_{n-1}$看做$a_{0}x^{0}+a_{1}x^{1}+...+a_{n-1}x^{n-1}$.那么第一种操作相当于

Topcoder SRM 345 Div1 250

题意:起初在(0,0),要到(x,y)去,每次只能横向或纵向移动.横向移动时,若所在直线y为偶数,那么只能往x轴正方向移动,若为奇数,只能往x轴反方向移动:纵向移动时,若所在直线x为偶数,那么只能往y轴正方向移动,若为奇数,只能往y轴反方向移动.问从起点到终点的最短距离是多少? x,y 范围是[-1e6, 1e6] 解法:一开始想到bfs(想到很自然),将(0, 0), (x, y), (x, 0), (0, y)这4个点分别周围9个点(包括自己)作为可达点.bfs处理出(0, 0)到(x, y

Topcoder SRM 653 Div1 250

Problem N个人坐成一排,属于同一个公司的人都坐在一起(挨在一起),但不知道谁属于哪个公司.现在问其中的某些人 他属于哪个公司,他的回答是Ai,Ai=0表示此人未被询问,否则表示他的回答.题目保证一定存在一种分组方案使得属于同一公司的人都坐在一起.现问方案是否唯一? Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,Ai∈[1,100] Solution 设dp[i]表示从i位置开始往后分组的情况,dp[i]=0表示无法分组,dp[i]=1表示