Hatena::Grouptopcoder

tsubosakaの日記

2009-01-17

SRM 310 Div1 Easy

| 18:08

上面と底面の面積の和は一番下のlevelだけで決まるのであとは側面の面積をレベルごとに足していけばよい.

public class PyramidOfCubes {
  public double surface(int K) {
    int level = 0;
    long size = 0;
    for(int i = 1 ; ; i++){
      level = i;
      size += i * i;
      if(size >= K)break;
    }
    double area = 2.0 * (Math.min(level * level, K));
    while(K >= level * level){
      area += 4.0 * level;        
      K -= level * level;
      level--;
    }
    if(K > 0){
      int w = ((K + level - 1) / level);
      if(w == 1)area += 2.0 * (1 + K);
      else area += 2.0 * (level + w);      
    }
    return area;
  }
}