2009-09-11SRM 448

SRM 448 Medium

| 00:03




  • 各手順において手札からは2枚ずつ減っていく
  • n-2枚目をシャッフルし終わったときに一番上にあるカードがk番目のカードであれば、n番目をシャッフルし終わったときに一番上にあるカードは1回のシャッフル手順を繰り返した時の状態でのk番目のカードである



public class TheCardShufflingDivOne {
  int rotate(int idx , int len , int move){
    return (idx + len - (move % len)) % len;
  int shuffle_once(int n , int idx , int left , int right){
    int lnum = (n + 1) / 2;
    int rnum = n / 2;
    if(idx < rnum - 1){
      int i = rotate(idx, rnum, right);
      return i * 2 + 1;
      idx -= rnum - 1;
      int i = rotate(idx, lnum, left);
      return i * 2;
  public int shuffle(int n, int left, int right) {
    boolean iseven = n % 2 == 0;
    int mdeck = iseven ? 2 : 1;
    int k = iseven ? 1 : 0;
    while(mdeck < n){
      mdeck += 2;
      k = shuffle_once(mdeck , k, left, right);
    return k + 1  ;

WilliamTagWilliamTag2019/02/02 11:26cialis without a rx
kjop cialis online
<a href=https://kellyannehulme.com>buy Cialis 20 mg</a>
acheter cialis france
cialis tyskland
low cost cialis 2.5mg daily
expired cialis side effects
<a href=https://kellyannehulme.com>20 mg cialis buy</a>
generic cialis in germany drug store
buy cialis us pharmacy

ArnoldNizArnoldNiz2019/02/05 20:50order female cialis side effects
buying cialis in malaysia
<a href="http://xcialisxx.com">Cheap Cialis buy</a>
cialis back pain
cialis review recreational
<a href="http://xcialisxx.com">Buy Cheap Cialis</a>
cialis strips online
about cialis
<a href="http://xcialisxx.com">Buy Cheap Cialis Online</a>

LukeEvifeLukeEvife2019/06/05 08:55Luke Bryan is my favourite US contry singer. His strong voice takes me away from all issues of this planet so I can enjoy my life and listen songs created by his voice. Now the singer is going on a tour in 2019. The concerts scheduled for this year, up to the 12th of October. Ticket prices are moderate and available for all men and women with different income. If you love contry music, then you must visit at least one of his concert. All tour dates are available at the <a href=https://lukebryantourdates.com>Luke Bryan concert list</a>. Visit the website and make yourself familiar with all Luke Bryan concerts in 2020!

LukeEvifeLukeEvife2019/06/08 00:46Luke Bryan is my favourite country singer. His strong voice takes me away from all problems of this planet so I start enjoy my life and listen songs created by his. Now the singer is going on a tour in 2020. The concerts scheduled for the whole 2019, up to the mid-October. Tickets are available for everyone. If you are a country music lover as me, then you must visit at least one Luke's concert. All tour dates are available at the <a href=https://lukebryantourdates.com>Luke Bryan concert list</a>. Open the website and make yourself familiar with all powerful Luke Bryan concerts in 2020!

FlorluptFlorlupt2019/06/10 12:34Florida Georgia Line is my favourite country music band. Headliners Brian Kelley and Tyler Hubbard are those people that could make anyone sing along with them. That's why I like to visit their shows. And - that's surprisingly wonderful - in 2019 they have CAN'T SAY IT AIN'T COUNTRY TOUR which covers all the US towns and cities. For concert dates list visit <a href=https://fgltour.com>Florida Georgia Line Concert Tour</a>.


SRM 442 Div 1 Medium

| 06:44


基本的に真ん中の合計と四隅の合計をO(1)で求めて、残りの端の4箇所をループでまわして合計を求めればいい。ただし縦長と横長のときに場合分けを行ったり、mod 5が0のときに開始点を変えなければならないとか非常にめんどう。


public class BedroomFloor {
  long num(long count[]) {
    long total = 0;
    total = count[5];
    total += count[4];
    count[1] -= count[4];
    total += count[3];
    if(count[2] >= count[3]){
      count[2] -= count[3];
      count[1] -= 2 * (count[3] - count[2]);
      count[2] = 0;
    if(count[2] > 0){
      long n = (count[2] + 1) / 2;
      count[1] -= 5 * n - 2 * count[2];
      total += n;
    if(count[1] > 0){
      total += (count[1] + 4) / 5;
    return total;

  public long numberOfSticks(int x1, int y1, int x2, int y2) {
    long count[] = new long[6];
    long x1g = x1 / 5;
    long y1g = y1 / 5;
    long x2g = (x2 - 1) / 5;
    long y2g = (y2 - 1) / 5;
    if (x1g == x2g && y1g == y2g) {
      plus(x1, y1, x2, y2, count);
    } else if (x1g == x2g) {
      long ys = y1 % 5 == 0 ? y1 : (y1g + 1) * 5;
      long ye = y2 % 5 == 0 ? y2 : y2g * 5;
      for (long y = ys; y < ye; y += 5) {
        plus(x1, y, x2, y + 5, count);
      plus(x1, y1, x2, ys, count);
      plus(x1, ye, x2, y2, count);
    } else if (y1g == y2g) {
      long xs = x1 % 5 == 0 ? x1 : (x1g + 1) * 5;
      long xe = x2 % 5 == 0 ? x2 : x2g * 5;
      for (long x = xs; x < xe; x += 5) {
        plus(x, y1, x + 5, y2, count);
      plus(x1, y1, xs, y2, count);
      plus(xe, y1, x2, y2, count);
    } else {
      long xs = x1 % 5 == 0 ? x1 + 5 : (x1g + 1) * 5;
      long xe = x2 % 5 == 0 ? x2 - 5 : x2g * 5;
      long ys = y1 % 5 == 0 ? y1 + 5 : (y1g + 1) * 5;
      long ye = y2 % 5 == 0 ? y2 - 5 : y2g * 5;
      count[5] += (ye - ys) * (xe - xs) / 5;
      for (long x = xs; x < xe; x += 5) {
        plus(x, y1, x + 5, ys, count);
        plus(x, ye, x + 5, y2, count);
      for (long y = ys; y < ye; y += 5) {
        plus(x1, y, xs, y + 5, count);
        plus(xe, y, x2, y + 5, count);
      plus(x1, y1, xs, ys, count);
      plus(x1, ye, xs, y2, count);
      plus(xe, y1, x2, ys, count);
      plus(xe, ye, x2, y2, count);
    return num(count);

  void plus(long x1, long y1, long x2, long y2, long count[]) {
    if (x1 >= x2)
    if (y1 >= y2)
    long xs = x1 / 5;
    long ys = y1 / 5;
    int xd = (int) (x2 - x1);
    int yd = (int) (y2 - y1);
    if ((xs + ys) % 2 == 0) {
      count[xd] += yd;
    } else {
      count[yd] += xd;


SRM440 Div1 Medium

| 05:59

一個ルートノードを決めて、探索していったときに(あるノードの期待値) = a * (親ノードの期待値) + bと書け(ノードが葉のときはa=1.0,b=1.0、チーズのときはa=b=0.0となる)再帰的に計算するとルートノードでの期待値となる。一回の計算にノード数分に比例する時間がかかり、それをすべてのノードに対して実行するのでO(N^2)となる。

また、cafelierさんによると期待値 = 親の期待値 + 子孫の数*2 + 1となるのでこれだとチーズから始めればO(N)で計算できるけどこの式を思いつくのは難しいと思う(証明は天下り的に子供の期待値がこの形で書けているとして代入していけば帰納法で示せる。)

import java.util.*;

class Vertex{
  List<Integer> adjoint;
    adjoint = new ArrayList<Integer>();
  int size(){
    return adjoint.size();
  void add(int e){

public class MazeWandering {  
  int cheeseId;
  public double expectedTime(String[] maze) {
    Vertex[] vs = createGraph(maze);
    double res = 0.0;
    for(int i = 0 ; i < vs.length ; i++){
      res += rec(vs , -1 , i)[1];
    return res / vs.length;
  double[] rec(Vertex vs[] , int parent , int cp){
    if(cp == cheeseId)
      return new double[]{0.0 , 0.0};
    Vertex v = vs[cp];
    double res[] = new double[2];
    res[0] = 0.0;
    res[1] = 1.0;    
    for(int adj : v.adjoint){
      if(adj == parent)continue;
      double a[] = rec(vs , cp , adj);
      res[0] += a[0] / v.size();
      res[1] += a[1] / v.size();
    res[1] /= (1 - res[0]);
    res[0] = 1.0 / (v.size() * (1.0 - res[0]));
    return res;
  private Vertex[] createGraph(String[] maze) {
    int W = maze[0].length();
    int H = maze.length;
    int ids[][] = new int[H][W];
    int id = 0;
    for (int i = 0; i < H; i++) {
      Arrays.fill(ids[i], -1);
      for (int j = 0; j < W; j++) {
        char c = maze[i].charAt(j);
        if(c == 'X')continue;
        if(c == '*')
          cheeseId = id;
        ids[i][j] = id++;
    int n = id;
    Vertex vs[] = new Vertex[n];
    int dx[] = { 1, -1, 0, 0 };
    int dy[] = { 0, 0, 1, -1 };
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c = maze[i].charAt(j);
        if (c == 'X') 
        int cid = ids[i][j];
        Vertex v = new Vertex();
        for (int k = 0; k < dx.length; k++) {
          int x = j + dx[k];
          int y = i + dy[k];
          if (x < 0 || y < 0 || x >= W || y >= H)
          if (ids[y][x] < 0)
        vs[cid] = v;
    return vs;

WilliamTagWilliamTag2019/02/02 11:41i don't get cialis commercials
cialis online pagamento postepay
<a href=https://kellyannehulme.com>cheap price</a>
mail order discount cialis
does generic cialis work
buy cialis in israel
canada brand cialis sex tablets
<a href=https://kellyannehulme.com>Cialis 20 mg cheap price</a>
generic cialis online best price
cialis official site

ArnoldNizArnoldNiz2019/02/05 20:50buy cialis paypal payment
product team cialis getting ready to market case study
<a href="http://cialistlm.com">buy cialis</a>
cialis prescription limits
buy cialis black 800mg
<a href="http://xcialisxx.com">buy cialis</a>
buy cialis spain
cialis para mujeres
<a href="http://cialistlm.com">Cheap Cialis buy</a>

ArnoldNizArnoldNiz2019/02/05 21:19farmacia online cialis espana
cena cialis c20 w aptece
<a href="http://xcialisxx.com">Cheap Cialis buy</a>
cialis sale in malaysia
cialis once a day review
<a href="http://cialistlm.com">Buy Cialis Online</a>
cialis store new york
cialis daily use price
<a href="http://xcialisxx.com">buy cialis</a>


SRM 439 Div1 Medium

| 20:08


まず、$の数が1つのときは A B B B ... という周期的な列になるため容易に求まる。また$の数が2つ以上のときは変数の長さが常に2倍以上になるため、s <= 32のときのみを考えればよい。

import java.util.*;
public class EndlessStringMachine {
  int cnt(String s) {
    int cnt = 0;
    for (char c : s.toCharArray())
      if (c == '$')cnt++;
    return cnt;
  char rec(String in , String p , int s , int i , List<Long> ls){
    if(s == 0)return in.charAt(i);
    long plen = ls.get(s - 1);
    for(char c : p.toCharArray())
      if(c == '$')
        if(i < plen)return rec(in, p, s - 1, i, ls);
        else i -= plen;
      else if(i-- == 0)return c;
    return '-';
  public String getFragment(String in, String p, int s, int L, int R) {
    L--; R--;
    long dn = cnt(p);
    StringBuilder sb = new StringBuilder();
    if (dn == 1) {
      int plen = p.length() - 1;
      long tlen = in.length() + plen * (long)s;
      for (int i = L; i <= R; i++)
        if (i < in.length())sb.append(in.charAt(i));
        else if(i >= tlen)sb.append('-');
        else sb.append( p.charAt(1 + (i - in.length()) % plen));      
    } else {
      List<Long> ls = new ArrayList<Long>();
      for (int i = 1; i <= s; i++) {
        long li = (ls.get(i - 1) * dn) + (p.length() - dn);
        if (li >= R)break;
      for(int index = L ; index <= R ; index++)
        sb.append(rec(in, p, ls.size() - 1 , index, ls));      
    return sb.toString();


TCO 09 Round 1 Medium

| 04:05


public class KthProbableElement {
  double fact(int n){
    double res = 1.0;
    for(int i = 1 ; i <= n ; i++)
      res *= i;
    return res;
  public double probability(int M, int lowerBound, int upperBound, int N, int K) {
    double res = 0.0;
    double pi = 1.0 * (N - lowerBound + 1) / (upperBound - lowerBound + 1);
    double pii = 1.0 * (N - lowerBound ) / (upperBound - lowerBound + 1);
    for(int k = K ; k <= M ; k++){
      double cmb = fact(M) / (fact(k) * fact(M - k)); 
      double r = Math.pow(pi , k) * Math.pow(1.0 - pi ,M - k);
      r -= Math.pow(pii , k) * Math.pow(1.0 - pii ,M - k);
      res += r * cmb;
    return res;

JospehmoilaJospehmoila2019/01/30 07:27buy cialis in mexico
cialis without a rx
buy cialis with dapoxetine online
cialis sin receta medica

best cialis online order
cialis original brand name sale
buy cialis online scams
cialis from canada with a prescription

JospehmoilaJospehmoila2019/01/30 16:30cialis thailand buy
typical cialis prescription
order cheap cialis
canadian pharmacy cialis 20mg

cialis super active 20mg pills
swiss medical cialis
cialis for women free sample
z 14 order cialis super active online

JospehmoilaJospehmoila2019/01/31 01:32canadian brand label cialis
bay cialis fast shipping
cialis 20 mg effects
cialis for sale without prescription

buy cialis in nigeria
cialis purchace on line fast no rx
genuine brand name cialis
buy cialis soft tabs information

JospehmoilaJospehmoila2019/01/31 11:59what is correct dosage for cialis
do broken cialis work
current price of cialis
cialis discount paris

cialis hearing loss
bootleg cialis
acquisto online cialis originale
rui products cialis review

JospehmoilaJospehmoila2019/01/31 23:26real cialis canadian pharmacy
cialis tablets for sale
dove posso acquistare cialis online
best buy cialis online

cialis kanada
5mg daliy cialis reviews
high price of cialis
cialis experience

JospehmoilaJospehmoila2019/02/01 13:50cialis online.com
once a day cialis cheap
generic cialis 99cents
best place to buy cialis online reviews

get a prescription to buy brand cialis
cialis drug information
lloyds pharmacy cialis price
usa overnight pharmacy cialis

DavidtogDavidtog2019/02/02 23:43cialis online apotheke
cheap generic female cialis
<a href="http://kaivanrosendaal.com/">cialis vs viagra</a>
cialis 20 mg coupon
buy cialis chennai
<a href=http://kaivanrosendaal.com>cialis purchasing</a>
rite aid pharmacy cialis
online apotheke cialis kaufen

DavidtogDavidtog2019/02/03 08:52cialis tienda online
brand name cialis online
<a href="http://kaivanrosendaal.com">cialis from canada</a>
selling cialis on craigslist
<a href=http://kaivanrosendaal.com/>cialis tadalafil</a>
cheap cialis extra strength
best place to buy cialis online yahoo

DavidtogDavidtog2019/02/03 23:29purchase cialis in south africa
men's health online cialis
<a href="http://kaivanrosendaal.com">cialis coupons printable</a>
cialis prescription dosage
cialis forums
<a href=http://kaivanrosendaal.com/>cialis generic availability</a>
is generic cialis legal
prescription cost for cialis

DavidtogDavidtog2019/02/04 07:44cialis professional vs regular cialis
<a href="http://kaivanrosendaal.com/">Buy Cheap Cialis</a>
comprare il cialis online
cialis soft online kaufen
<a href=http://kaivanrosendaal.com/>what is cialis</a>
cialis effectiveness review
rayh health care cialis

DavidtogDavidtog2019/02/04 18:06import cialis
cialis no prescription montreal
<a href="http://kaivanrosendaal.com">cialis for daily use</a>
cialis commercial actor
buy cialis forum
<a href=http://kaivanrosendaal.com>cialis reviews</a>
purchase cialis for daily use online

DavidtogDavidtog2019/02/05 18:04costco price for cialis
cialis e-shops europe
<a href="http://kaivanrosendaal.com">Buy Cheap Cialis Online</a>
cialis von lilly
buy cialis with no perscription
<a href=http://kaivanrosendaal.com/>cialis manufacturer coupon</a>
cialis online singapore
canada law on cialis prescription

DavidtogDavidtog2019/02/06 03:19cialis black no prescription
buy cialis 10mg australia
<a href="http://kaivanrosendaal.com">discount cialis</a>
buy cialis online with discover card
cialis generico farmacia italiana
<a href=http://kaivanrosendaal.com>online cialis</a>
cialis no prescription needed canada
buy cialis in florida

JasonPoGJasonPoG2019/03/01 03:44buy cialis united statescialis online express Cialis 10mg <a href="http://xcialis20mg.com">Buy Cialis 10 mg</a>

JasonPoGJasonPoG2019/03/01 11:04cialis 60mgcialis profesional Buy Cialis 60mg <a href="http://xcialis20mg.com">http://xcialis20mg.com</a>

JasonPoGJasonPoG2019/03/01 19:44cialis without prescription usacialis 5 mg terapia Buy Cialis 10 mg <a href="http://xcialis20mg.com">xcialis20mg.com</a>

JasonPoGJasonPoG2019/03/02 04:30cialis shop deutschlandcialis sold on the street Buy Cialis 40 mg <a href="http://xcialis20mg.com">http://xcialis20mg.com</a>

JasonPoGJasonPoG2019/03/03 01:24cialis heartburnis cialis in mexico good Cialis 10mg <a href="http://xcialis20mg.com">xcialis20mg.com</a>

GeorgegedGeorgeged2019/03/29 00:26evasione slot machine italia avalon slot machine gratis cleopatra slot machine wins <a href="http://hlaastmu.com/#slot">giochi on line free</a> slot machine mechanism bar bar bar slot machine compensi slot machine
slot machine expected value slot machine jammer app slot machine da bar video <a href=http://hlaastmu.com>giochi macchine gratis</a>


GeorgegedGeorgeged2019/03/29 04:56scandalo slot machine siti di slot machine slot machine donne <a href="http://hlaastmu.com/#slot">giochi gratis nuovi</a> slot machine anni 80 online re delle slot machine slot machine gratis deluxe
contabilitГ  gestore slot machine slot machine clan russo haunted house slot machine free <a href=http://www.hlaastmu.com>scaricare giochi gratis</a>


GeorgegedGeorgeged2019/03/29 09:13migliori slot machine frutta king of the wild slot machine slot machine da bar fowl play <a href="http://hlaastmu.com">slot bar</a> trucchi scaricare slot machine slot machine gratis senza download circus slot machine free
slot machine legge stabilitГ  slot machine gratis athens novostar slot machine trucchi <a href=http://hlaastmu.com/#slot>giochi slot machine gratis</a>


GeorgegedGeorgeged2019/03/29 13:32casino lawsuit slot machine logo slot machine slot machine ercole gratis <a href="http://hlaastmu.com/#slot">slot gratis senza registrazione</a> slot machine nuove normative slot machine l'isola del tesoro 2 evasione slot machine
truccare le slot machine simulazione slot machine gratis trucchi delle slot machine <a href=http://hlaastmu.com/>slot gratis</a>


GeorgegedGeorgeged2019/03/29 17:55happy birthday slot machine come si gioca alle slot machine percentuali di guadagno slot machine <a href="http://hlaastmu.com/#slot">giochi gratis 2016</a> giochi di slot machine net slot machine acquistare slot machine
slot machine in svizzera sala slot machine come aprire video slot machine bar <a href=http://hlaastmu.com/>giocare gratis slot</a>


GeorgegedGeorgeged2019/03/29 22:27gioco della slot machine da scaricare gratis chi gioca alle slot machine tasse vincite slot machine <a href="http://hlaastmu.com/#slot">http://hlaastmu.com/</a> percentuale slot machine bar slot machine farwest gratis come funziona una slot machine online
sala slot machine slot machine guadagno slot machine gratis sizzling hot <a href=http://hlaastmu.com/>slot gratis senza registrazione</a>


GeorgegedGeorgeged2019/03/30 03:06machine slot per bar 3 diamonds slot machine apparecchio per slot machine <a href="http://hlaastmu.com/">slot machine gratis da bar</a> fornitore slot machine free online slot machine games ordinanza slot machine
emp slot machine slot machine arezzo slot machine gratis da bar il mago <a href=http://www.hlaastmu.com>gioco gratis online</a>


GeorgegedGeorgeged2019/03/30 11:58how to cheat a slot machine with a cell phone slot machine meccaniche giocare gratis slot machine da bar <a href="http://hlaastmu.com">giochi gratis flash</a> sale slot machine portogruaro slot machine jackpot malaysia slot machine haunted house gioca gratis
slot machine legge stabilitГ  slot machine per casa slot machine tokens <a href=http://www.hlaastmu.com>giochi online gratis</a>


GeorgegedGeorgeged2019/03/31 16:07slot machine le iene electrifying riches slot machine online country girl slot machine <a href="http://hlaastmu.com/">slot machine gratis da giocare</a> golden goddess slot machine giocare con slot machine gratis slot-machine gratis
come vincere alle slot machine pokemon rosso fuoco slot machine quick fire slot machine cake topper <a href=http://hlaastmu.com>giochi on line free</a>


GeorgegedGeorgeged2019/03/31 20:22slot machine 3d gratis slot machine da bar 2017 free spin slot machine games <a href="http://hlaastmu.com">slot machine gratis da giocare</a> mandare in tilt slot machine si puo vincere alle slot machine slot machine gratis toto
riforma slot machine best slot machine in wynn come fare refill slot machine <a href=http://www.hlaastmu.com>giochi on line free</a>


GeorgegedGeorgeged2019/04/01 00:26slot machine gratis anni 90 slot machine nuove normative borderlands pre sequel slot machine hack <a href="http://www.hlaastmu.com">slot gratis</a> la grande fuga slot machine zeus slot machine las vegas best slot machine on bovada
casino lawsuit slot machine crack slot machine relazione tecnica installazione slot machine <a href=http://www.hlaastmu.com>slot machine gratis da scaricare</a>


GeorgegedGeorgeged2019/04/01 04:33consumo slot machine trucchi macchine slot machine slot machine no limits mod apk <a href="http://hlaastmu.com/#slot">giochi di macchina</a> aprire una sala slot machine in franchising come uscire dalla dipendenza dal gioco slot machine slot machine produzione
javascript slot machine code licenza per noleggio slot machine noleggio slot machine umbria <a href=http://hlaastmu.com/>gioco gratis</a>


GeorgegedGeorgeged2019/04/01 08:35disegni slot machine slot machine gratis da bar con bonus video slot machine gallina <a href="http://hlaastmu.com/#slot">slot machine gratis online</a> download slot machine gallina uova d'oro slot machine da bar gallina dalle uova d'oro giocare gratis slot machine da bar
crisi slot machine giochi nuovi gratis online slot machine slot machine novomatic gratis <a href=http://www.hlaastmu.com>gioco gratis online</a>


GeorgegedGeorgeged2019/04/01 12:51roman empire slot machine disegni di slot machine roman dynasty slot machine free <a href="http://www.hlaastmu.com">giocare gratis slot</a> associazione noleggiatori slot machine trucchi slot machine pokemon rosso tassazione slot machine 2015
slot machine ndrangheta mini slot machine scarica slot machine gratis per pc <a href=http://hlaastmu.com/#slot>gioco slot gratis</a>


GeorgegedGeorgeged2019/04/01 17:09golden girls slot machine game of thrones slot machine cavernicoli slot machine <a href="http://hlaastmu.com">nuovi giochi gratis</a> software per slot machine bluetooth giochi slot machine gratuiti slot machine con bancomat
free online slot machine games wheel of fortune gioco slot machine online king of the wild slot machine <a href=http://hlaastmu.com/>giochi di macchinette</a>


GeorgegedGeorgeged2019/04/01 21:36slot machine gratis gallina 666 slot machine casino online slot machine gallina uova d oro <a href="http://www.hlaastmu.com">giochi da scaricare gratis</a> free cleopatra ii slot machine le iene nadia toffa slot machine franchising slot machine
slot machine tattoo old school cash spin slot machine jackpot slot machine il faraone <a href=http://hlaastmu.com/#slot>www slot machine gratis</a>