ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1537. Ents

crash!! please help!!
Posted by Dmitriy Black 2 Jan 2012 04:05
my program works fine on my computer. it even gives right answers :) But when i submit it has crash on 20 test. I cannot understant why. Please help!!

import java.io.*;
import java.util.Arrays;
public class Task_1537 {
    public static void main(String[] args) throws Exception{
        StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
        st.nextToken();
        int k = (int) st.nval;
        st.nextToken();
        int p = (int) st.nval;

        int t[] = new int[k+1];
        t[2] = 1 % p;
        for (int i = 3; i <= k; i++){
            if ((i & 1) == 1)
                t[i] = t[i-1];
            else t[i] = t[i-1] + t[i/2];
            if (t[i] >= p)
                t[i] = t[i] - p;
        }
        //System.out.println(Arrays.toString(t));
        System.out.println(t[k]);
    }

}
accepted 0.078 s c++ 0.218 s JAVA
Posted by Sunnat 15 Apr 2012 15:08
// JAVA :
import java.io.*;
import java.util.Arrays;
public class Task_1537 {
    public static void main(String[] args) throws Exception{
        StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
        st.nextToken();
        int k = (int) st.nval;
        st.nextToken();
        int p = (int) st.nval;
        if(k<2)System.out.println(0);
        else {
        int t[] = new int[k+1];
        t[2] = 1 % p;
        for (int i = 3; i <= k; i++){
            if ((i & 1) == 1)
                t[i] = t[i-1];
            else t[i] = t[i-1] + t[i/2];
            if (t[i] >= p)
                t[i] = t[i] - p;
        }
        //System.out.println(Arrays.toString(t));
        System.out.println(t[k]);
       }
    }

}



// C++ :

#include<stdio.h>
int main(){
    int k,p,i;
    int *t;
    scanf("%i%i",&k,&p);
    if(k<2)printf("0");
    else {
    t = new int[k+3];
        t[2] = 1 % p;
        for (i = 3; i <= k; i++){
            if ((i & 1) == 1)
                t[i] = t[i-1];
            else t[i] = t[i-1] + t[i/2];
            if (t[i] >= p)
                t[i] = t[i] - p;
        }
        printf("%i",t[k]);
    }
}
Re: accepted 0.156 s paskall
Posted by Sunnat 15 Apr 2012 18:06
CONST
 MaxN = 10000000;

VAR
 K, P : Longint;
 A    : Array [0 .. MaxN] of Longint;

PROCEDURE Solve;
 Var
  i : Longint;
   Begin
    A[1] := 0;
    A[2] := 1 mod P;
     for i := 3 to K do
      if i mod 2 = 0 then
        A[i] := (A[i - 1] + A[i div 2]) mod P
          else
        A[i] := A[i - 1] mod P;
   End;

PROCEDURE Run;
   Begin
    Read(K, P);
    if K<2 then WriteLn(0)
    else begin
    Solve;
    WriteLn(A[K]);
    end;
   End;

BEGIN
  Run;
END.