problem1 link

满足$a^{b}\leq5000000,b>1$的数字不多,有2000多个,直接暴力计算能组成哪些数字即可。

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class SumsOfPerfectPowers { public int howMany(int lowerBound,int upperBound) {
boolean[] a=new boolean[upperBound+1];
a[0]=true;
if(upperBound>=1) {
a[1]=true;
} for(int i=2;i*i<=upperBound;++i) {
if(a[i]) {
continue;
}
int k=i*i;
while(k<=upperBound) {
a[k]=true;
if(k>upperBound/i) {
break;
}
k=k*i;
}
}
int num=0;
for(int i=1;i<=upperBound;++i) {
if(a[i]) {
++num;
}
}
int[] b=new int[num];
num=0;
for(int i=1;i<=upperBound;++i) {
if(a[i]) {
b[num++]=i;
}
}
for(int i=0;i<b.length;++i) {
for(int j=i;j<b.length;++j) {
if(b[i]+b[j]>upperBound) {
break;
}
a[b[i]+b[j]]=true;
}
}
num=0;
for(int i=lowerBound;i<=upperBound;++i) {
if(a[i]) {
++num;
}
}
return num;
}
}

  

problem2 link

计算出每个点的值,bfs即可。长度大于顶点$n$时说明有环。

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class StarsInGraphs { public int starryPaths(String[] adjacencyMatrix,int C) {
final int n=adjacencyMatrix.length;
List<List> g=new ArrayList<>();
for(int i=0;i<n;++i) {
g.add(new ArrayList<>());
}
long[] d=new long[n];
for(int i=0;i<n;++i) {
for(int j=0;j<n;++j) {
if(adjacencyMatrix[i].charAt(j)=='1') {
g.get(i).add(j);
++d[i];
}
}
}
for(int i=0;i<n;++i) {
d[i]=(1l<<d[i])-1-d[i]-d[i]*(d[i]-1)/2;
}
int[] f=new int[n];
boolean[] inq=new boolean[n];
Queue<Integer> queue=new LinkedList<>();
for(int i=0;i<n;++i) {
if(1<=d[i]&&d[i]<=C) {
f[i]=1;
inq[i]=true;
queue.offer(i);
}
}
while(!queue.isEmpty()) {
int u=queue.poll();
inq[u]=false;
for(int i=0;i<g.get(u).size();++i) {
int v=(int)g.get(u).get(i);
if(d[u]<=d[v]&&d[v]<=C&&f[u]+1>f[v]) {
f[v]=f[u]+1;
if(f[v]>n) {
return -1;
}
if(!inq[v]) {
inq[v]=true;
queue.offer(v);
}
}
}
}
int result=0;
for(int i=0;i<n;++i) {
result=Math.max(result,f[i]);
}
return result;
} }

  

problem3 link

求出所有交点,那么就是一个图。每条边看作两条有向边。对于每个封闭图形(必是凸包)逆时针找它的每条边。

import java.util.*;
import java.math.*;
import static java.lang.Math.*; public class PlaneDivision { static class Rational {
public BigInteger x;
public BigInteger y;
public int sign; public Rational copy() {
return new Rational(new BigInteger(x.toString()),new BigInteger(y.toString()),sign);
} public Rational() { }
public Rational(BigInteger _x,BigInteger _y,int _sign) {
x=_x;
y=_y;
sign=_sign;
simple();
} public Rational(long t) {
x=new BigInteger(Long.toString(t));
y=BigInteger.ONE;
sign=1;
simple();
} public boolean equals(Rational a) {
if(x.equals(BigInteger.ZERO)) {
return a.x.equals(BigInteger.ZERO);
}
if(a.x.equals(BigInteger.ZERO)) {
return false;
}
return sign==a.sign&&x.equals(a.x)&&y.equals(a.y);
} private Rational simple() {
if(x.equals(BigInteger.ZERO)) {
sign=1;
y=BigInteger.ONE;
}
else {
if(x.compareTo(BigInteger.ZERO)<0) {
x=x.negate();
sign=-sign;
}
BigInteger t=x.gcd(y); if(!t.equals(BigInteger.ONE)) {
x=x.divide(t);
y=y.divide(t);
} }
return this;
} public Rational add(Rational a) {
if(a.x.equals(BigInteger.ZERO)) {
return copy();
}
if(x.equals(BigInteger.ZERO)) {
return a.copy();
} BigInteger p=y.multiply(a.y);
BigInteger x1=x.multiply(a.y);
BigInteger x2=a.x.multiply(y); Rational result=new Rational();
result.y=p; if(sign==1) {
if(a.sign==1) {
result.x=x1.add(x2);
result.sign=1;
}
else {
result.x=x1.subtract(x2);
result.sign=1;
}
}
else {
if(a.sign==1) {
result.x=x2.subtract(x1);
result.sign=1;
}
else {
result.x=x1.add(x2);
result.sign=-1;
}
}
return result.simple();
} public Rational substract(Rational a) {
return add(new Rational(a.x,a.y,-a.sign));
} public Rational multiply(Rational a) {
if(x.equals(BigInteger.ZERO)) {
return new Rational(0);
}
if(a.x.equals(BigInteger.ZERO)) {
return new Rational(0);
} Rational result=new Rational();
result.x=x.multiply(a.x);
result.y=y.multiply(a.y);
result.sign=sign*a.sign;
return result.simple();
}
public Rational divide(Rational a) {
if(x.equals(BigInteger.ZERO)) {
return new Rational(0);
}
return multiply(new Rational(a.y,a.x,a.sign));
} public boolean lessThan(Rational a) { if(a.x.equals(BigInteger.ZERO)) {
if(x.equals(BigInteger.ZERO)) {
return false;
}
return sign==-1?true:false;
}
if(x.equals(BigInteger.ZERO)) {
return a.sign==1?true:false;
}
if(sign<a.sign){
return true;
}
if(a.sign<sign) {
return false;
} if(sign==1) {
return x.multiply(a.y).compareTo(a.x.multiply(y))<0;
}
else {
return x.multiply(a.y).compareTo(a.x.multiply(y))>0;
}
} public double toDouble() {
return 1.0*Double.valueOf(x.toString())/Double.valueOf(y.toString())*sign;
} public String toString() {
return Double.toString(toDouble());
}
} static int DB(Rational x)
{
if(x.x.equals(BigInteger.ZERO)) {
return 0;
}
return x.sign;
} static class point implements Comparable
{
public Rational x;
public Rational y; public point(){}
public point(Rational _x,Rational _y) {
this.x=_x.copy();
this.y=_y.copy();
} public point add(point a)
{
return new point(x.add(a.x),y.add(a.y));
} public point substract(point a)
{
return new point(x.substract(a.x),y.substract(a.y));
} public Rational crossMultiply(point a)
{
return x.multiply(a.y).substract(y.multiply(a.x));
} public point multiply(Rational t)
{
return new point(x.multiply(t),y.multiply(t));
} public Rational dotMultiply(point a)
{
return x.multiply(a.x).add(y.multiply(a.y));
} public double getAng(point a)
{
return Math.atan2(crossMultiply(a).toDouble(),dotMultiply(a).toDouble());
} public boolean equals(point a)
{
return x.equals(a.x)&&y.equals(a.y);
} public point divide(Rational a) {
return new point(x.divide(a),y.divide(a));
} @Override
public int compareTo(Object e) {
point t=(point)e;
if(DB(x.substract(t.x))!=0) {
return x.lessThan(t.x)?-1:1;
}
if(DB(y.substract(t.y))==0) {
return 0;
}
return y.lessThan(t.y)?-1:1;
} public String toString() {
return x.toString()+","+y.toString();
} } static class pair {
public int first;
public int second; public pair() {}
public pair(int first,int second) {
this.first=first;
this.second=second;
}
} static final int N=85; point[][] L=null;
List<List<point>> G=null;
List<point> p=null; int m;
point x,y; List<List<pair>> g=null; boolean[] visit=null;
int n; void init() {
L=new point[N][];
for(int i=0;i<N;++i) {
L[i]=new point[2];
}
G=new ArrayList<>();
for(int i=0;i<N;++i) {
G.add(new ArrayList<>());
}
p=new ArrayList<>();
g=new ArrayList<>();
for(int i=0;i<N*N*N;++i) {
g.add(new ArrayList<>());
}
visit=new boolean[N*N*N];
} boolean isParal(point a,point b,point p,point q)
{
Rational x=(b.x.substract(a.x)).multiply(q.y.substract(p.y));
Rational y=(b.y.substract(a.y)).multiply(q.x.substract(p.x));
return DB(x.substract(y))==0;
} point getCross(point a,point b,point p,point q)
{
Rational s1=(a.substract(p)).crossMultiply(b.substract(p));
Rational s2=(b.substract(q)).crossMultiply(a.substract(q));
return (p.multiply(s2).add(q.multiply(s1))).divide(s1.add(s2));
} Rational cross(point a,point b,point p)
{
return (b.substract(a)).crossMultiply(p.substract(a));
} class MyComparator implements Comparator {
public int compare(Object o1,Object o2) {
pair A=(pair)o1;
pair B=(pair)o2;
point a=p.get(A.first);
point b=p.get(B.first); Rational xx=cross(x,y,a);
Rational yy=cross(x,y,b);
if(DB(xx)!=DB(yy)) {
return DB(xx)>DB(yy)?-1:1;
} double dx=a.substract(y).getAng(x.substract(y));
double dy=b.substract(y).getAng(x.substract(y)); return dx<dy?-1:1;
}
} int getindex(point t) {
for(int i=0;i<p.size();++i) {
if(p.get(i).equals(t)) {
return i;
}
}
assert false;
return -1;
} void build()
{
int x=0;
for(int i=1;i<=n;++i)
{
for(int j=0;j+1<G.get(i).size();++j)
{
int k=getindex(G.get(i).get(j));
int t=getindex(G.get(i).get(j+1)); if(k==t) {
continue;
}
g.get(k).add(new pair(t,++x));
g.get(t).add(new pair(k,++x));
}
}
} int result; void DFS(int cur,int pre,int t)
{
if(cur==t) {
++result;
return;
}
x=p.get(pre);
y=p.get(cur);
Collections.sort(g.get(cur),new MyComparator()); for(int i=0;i<g.get(cur).size();++i)
{
int j=g.get(cur).get(i).first;
int k=g.get(cur).get(i).second;
if(j==pre||visit[k]) {
continue;
}
if(DB(cross(p.get(pre),p.get(cur),p.get(j)))<=0) {
return;
}
visit[k]=true;
DFS(j,cur,t);
return;
}
} int cal()
{
result=0;
for(int i=0;i<m;++i)
{
for(int j=0;j<g.get(i).size();++j)
{
int k=g.get(i).get(j).first;
int t=g.get(i).get(j).second;
if(visit[t]||j!=0&&g.get(i).get(j-1).first==k) {
continue;
}
visit[t]=true;
DFS(k,i,i);
}
}
return result;
} public int howManyFiniteParts(int[] x1, int[] y1, int[] x2, int[] y2) { init(); n=x1.length; for(int i=1;i<=n;++i)
{
L[i][0] = new point(new Rational(x1[i-1]),new Rational(y1[i-1]));
L[i][1] = new point(new Rational(x2[i-1]),new Rational(y2[i-1])); for(int j=1;j<i;++j)
{
if(isParal(L[j][0],L[j][1],L[i][0],L[i][1])) {
continue;
}
point temp=getCross(L[j][0],L[j][1],L[i][0],L[i][1]);
int k=-1;
for(k=0;k<G.get(i).size();k++) {
point t=G.get(i).get(k);
if(t.equals(temp)) {
break;
}
}
if(k>=G.get(i).size()) {
G.get(i).add(temp);
}
for(k=0;k<G.get(j).size();k++) {
point t=G.get(j).get(k);
if(t.equals(temp)) {
break;
}
}
if(k>=G.get(j).size()) {
G.get(j).add(temp);
}
p.add(temp);
}
} Collections.sort(p); if(p.size()==0) {
m=0;
}
else {
m=1;
for(int i=1;i<p.size();++i) {
if(!p.get(m-1).equals(p.get(i))) {
p.set(m,p.get(i));
++m;
}
}
} for(int i=1;i<=n;++i) {
Collections.sort(G.get(i));
}
build();
return cal();
}
}

  

topcoder srm 350 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. php 下载完成后删除文件

    最近遇到一个需求:下载用户上传的图片,但是图片不断更新. 1.需要将图片从图片服务器下载到网站后台服务器 2.压缩文件夹生成zip压缩包 3.下载压缩包 4.删除压缩包和临时文件夹 其中遇到了一个问题 ...

  2. vue中使用ckeditor

    1.第一步首先去ckeditor官网去下载editor文件,这里以ckeditor4为例 首先在index.html里引入js代码 <script type="text/javascr ...

  3. 1.安装Python3和PyCharm

    一.安装Python3 1.进入官网:www.python.org 2.下载(可以选择你自己的电脑系统版本,我这里是win7 64位) 3.然后点击XXX.exe傻瓜式安装 4.配置环境变量 [右键计 ...

  4. web api HttpConfiguration

    //设置web api configuration public static void Register(HttpConfiguration config){ config.Services.Rep ...

  5. 使用函数接口和枚举实现配置式编程(Java与Scala实现)

    概述 做报表时,有时需要根据不同的业务生成不同的报表.这样,需要能够动态地配置列字段,并根据列字段来输出对应的报表.使用函数接口结合枚举可以比较优雅地实现配置式编程. 问题描述如下: 假设有对象 St ...

  6. Spring源码阅读(七)

    这一讲主要分析bean注册过程中各种初始化方法回调的执行逻辑(initializeBean) /** * Initialize the given bean instance, applying fa ...

  7. jsp无法访问

    一直无法访问jsp: 由于Spring boot使用的内嵌的tomcat,而内嵌的tamcat是不支持jsp页面的,所有需要导入额外的包才能解决. <!-- 解决jsp无法访问 --> & ...

  8. 【转】ETL讲解(很详细!!!)

    ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...

  9. Linux基础命令---chsh

    chsh 改变用户登录时使用的shell,默认使用bash.如果命令行上没有给出shell,chsh将提示输入一个shell.chsh将接受系统上任何可执行文件的完整路径名.但是,如果shell未在“ ...

  10. activemq消息队列的使用及应用docker部署常见问题及注意事项

    activemq消息队列的使用及应用docker部署常见问题及注意事项 docker用https://hub.docker.com/r/rmohr/activemq/配置在/data/docker/a ...