Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

factorial (easy)

$
0
0

this is my code

include<iostream>

include<math.h>

using namespace std; int main() { int n,a[n]; int i,j,c; cout <<pow(5,2);

cin >>n;
for(i=0;i<n;i++)
{
    c=0;
    cin>>a[i];
    for(j=1;j==13;j++)
    {

        if(a[i]/pow(5,j)!=0)

            c=c+(a[i]/pow(5,j));

    }
    cout <<c;
}

}

for the factorial(easy) problem when i run this program in codeblocks it crashes , but if i remove the initial pow(0 it runs but does not give the proper output(give 0 to everything).alt text


TLE in MUTLQ3

$
0
0

I have been trying to solve http://www.spoj.com/problems/MULTQ3/ I have implemented segment trees with lazy propagation. I am also keeping record of number of numbers leaving 0,1,2 as remainder when divided by 3.

Here is the code:

//#include GRAPH
//#define DEBUG       //comment when you have to disable all debug macros.
#define LOCAL     //uncomment for testing from local file
#define NDEBUG    //comment when all assert statements have to be disabled.
#include <iostream>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <climits>
#include <ctime>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <list>
#include <deque>
#include <sys/time.h>
#include <iomanip>
#include <cstdarg>
#include <utility> //std::pair
#include <cassert>
#define tr(c,i) for(typeof(c.begin()) i = (c).begin(); i != (c).end(); i++) 
#define present(c,x) ((c).find(x) != (c).end()) 
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define log2(x) (log(x)/log(2))
#define ARRAY_SIZE(arr) (1[&arr]-arr)      
#define INDEX(arr,elem)        (lower_bound(all(arr),elem)-arr.begin())
#define lld long long int
#define MOD 1000000007
#define gcd __gcd
#define equals(a,b) (a.compareTo(b)==0)    //for strings only
using namespace std;


#ifdef DEBUG
#define debug(args...)            {dbg,args; cerr<<endl;}
#else
#define debug(args...)              // Just strip off all debug tokens
#endif


#ifdef GRAPH
#include "drawGraph.cpp"
#endif

struct debugger
{
    template<typename T> debugger& operator , (const T& v)
    {    
        cerr<<v<<" ";    
        return *this;    
    }

}dbg;

template<class T>
inline void inputInt(T &n )
{
    n=0;
    int sign=1;
    T ch=getchar_unlocked();
    while(( ch < '0' || ch > '9') && ch!='-')
      ch=getchar_unlocked();
    if(ch=='-')
    {
        sign=-1;
        ch=getchar_unlocked();
    }
    while(  ch >= '0' && ch <= '9' )
       n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
    n*=sign;
}

typedef struct{
    lld mod0, mod1, mod2;
}node;

node tree[1<<18];
lld lazy[1<<18];

void build_tree(lld n, lld i, lld j){
    lazy[n] = 0;
    if(i==j){
        tree[n].mod0 = 1;
        tree[n].mod1 = tree[n].mod2 = 0;
        return;
    }
    build_tree(2*n, i, (i+j+1)/2-1);
    build_tree(2*n+1, (i+j+1)/2, j);
    tree[n].mod0 = tree[2*n].mod0+tree[2*n+1].mod0;
    tree[n].mod1 = tree[n].mod2 = 0;
}

inline void update_node(lld pos, lld val){
    if(val==0)return;
    lld a, b, c;
    a = tree[pos].mod0;
    b = tree[pos].mod1;
    c = tree[pos].mod2;
    tree[pos].mod0 = (val==1)?c:b;
    tree[pos].mod1 = (val==1)?a:c;
    tree[pos].mod2 = (val==1)?b:a;
    lazy[pos] = (lazy[pos]+val)%3;
}


void update_tree(lld n, lld i, lld j, lld x, lld y){
    if(j<x || y<i || i>j || x>y){
        return;
    }
    if(i==x && y==j){
        update_node(n, 1);
        return;
    }
    lld m = (i+j+1)/2-1;
    update_node(2*n, lazy[n]);
    update_node(2*n+1, lazy[n]);

    update_tree(2*n, i, m, x, min(y,m));
    update_tree(2*n+1, m+1, j, max(x, m+1), y);

    lazy[n] = 0;
    tree[n].mod0 = tree[2*n].mod0 + tree[2*n+1].mod0;
    tree[n].mod1 = tree[2*n].mod1 + tree[2*n+1].mod1;
    tree[n].mod2 = tree[2*n].mod2 + tree[2*n+1].mod2;
}

lld query(lld n, lld i, lld j, lld x , lld y, lld p){
    if(j<x || y<i || i>j || x>y){
        return 0;
    }
    if(i==x && y==j){
        if(p==0)
            return tree[n].mod0;
        if(p==1)return tree[n].mod2;
        return tree[n].mod1;
    }
    lld m=(i+j+1)/2-1, p1, p2;
    p1 = query(2*n,i,m,x,min(y,m), (p+lazy[n])%3);
    p2 = query(2*n+1,m+1,j,max(x,m+1),y, (p+lazy[n])%3);
    return p1+p2;
}



int main()
{
#ifdef LOCAL
    freopen("input.in","r",stdin);
#endif
     lld n,q,op,u,v;
     inputInt(n);
     inputInt(q);
     build_tree(1,1,n);
//   print_tree(1,1,n);
     while(q--){
         inputInt(op);
         inputInt(u);
         inputInt(v);
         u++;
         v++;
         if(op)printf("%lld\n",query(1,1,n,u,v,0));
         else {
             update_tree(1,1,n,u,v);
//            debug("update......................")print_tree(1,1,n);
         }
     }
}

But it is giving TLE. What is the problem? I am using segment trees with lazy propagation + fast I/O. Perhaps I've mis-implemented the lazy propagation part?

Printing in java

$
0
0

why in java printing with help of stringbuilder is faster than simple string printing

java program to convert integer to Roman numbers and vice versa

$
0
0

java program to convert integer to Roman numbers and vice versa

Fast string input

$
0
0

What is the fastest way to input strings in c and c++?

I know there are many ways to get integers fast but I was not able to find anything that helps with strings especially char strings.

Practice>easy>obstacle_course (run time error) plz help

$
0
0

include<stdio.h>

include<math.h>

float dis(float,float,float,float); int main() { int t; scanf("%d",&t);
if(t>=1&&t<=25) { int n,i,j; float r,R,k,d,f,d1,d2,ff;

for(i=1;i<=t;i++)                        
{

scanf("%f",&r);

scanf("%f",&R);                      
if(r>=1&&R>=1&&R<=25000&&r<R) 
    {

    scanf("%d",&n);
    if(n>0&&n<=500)
    {
    int arr1[n],arr2[n];
    float arr3[n];
    for(j=1;j<=n;j++)
    {

        scanf("%d%d",&arr1[j],&arr2[j]);
        if(arr1[j]!=arr2[j])
        {
        if(arr1[j]<0)
        {
            arr1[j]=arr1[j]*(-1);

        }
        if(arr2[j]<0)
        {
            arr2[j]=arr2[j]*(-1);
        }
        ff=sqrt(arr1[j]*arr1[j]+arr2[j]*arr2[j]);
        if(ff>=r&&ff<=R)            
      {
        f=dis(arr1[j],arr2[j],r,R);
        arr3[j]=f;
       } 
       else
       {
        printf("the distance is %f which is not in range\n",ff);
        j--;
       }
    }
    else 
    {
        j--;
        printf("coordinates are not alright\n");
    }
    }


    for(j=2;j<=n;j++)
    {
          k=arr3[1];
          if(arr3[j]<arr3[1])
          {
            arr3[1]=arr3[j];
            arr3[j]=k;

          }
    }

  printf("%0.3f\n",arr3[1]);

} else if(n==0) { printf("%0.3f\n",R-r); } } else printf("the radius not in range\n"); }

} else { printf("the number of cases should not exceed 25\n"); main(); } } float dis(float x,float y,float r,float R) { float z,d1,d2; z=sqrt(xx+yy); printf("the distance of the obstacle from the origin is%f\n",z); d1=z-r; d2=R-z; if(d1>=d2) return d1; else return d2;
}

spoj problem eko time limit exceeded

$
0
0

http://www.spoj.com/problems/EKO/ This is the spoj problem i am trying i am using binary search.I dont know what is wrong with this it is giving TLE on 9th test case.please help.

include<iostream>

using namespace std;

define scanint(a) scanf("%d",&a);

int main() {

int n,m,max=0;
scanf("%d%d",&n,&m);
int arr[n];
for(int i=0;i<n;i++)
{
scanint(arr[i]);
if(arr[i]>max)
    max=arr[i];
}
int lower=1,upper=max,temp,tempo,sum=0;
while(true)
{
    sum=0;
    temp=(lower+upper)/2;
    for(int i=0;i<n;i++)
    {
        tempo=arr[i]-temp;
        if(tempo>0)
        sum=sum+tempo;
    }
    if(sum>m)
    lower=temp;
    if(sum<m)
    upper=temp;
    if(sum==m||lower==upper||temp==1)
    {
    printf("%d\n",temp);
    break;
}


}
return 0;
}

cleaning tables cletab


how to solve questions on spoj

$
0
0

I recently started spoj. How to go for solving questions on it? I solved a few easy questions. But there is no way I can solve a question which is difficult because there are no editorials for questions on spoj. Moreover, people can't see each others codes. So how can I solve a question on which I'm stuck?

If we modify Travelling Salesman problem to allow visit to each node more than once, will it still be NP-Complete?

$
0
0

I have little or no knowledge about the NP-Completeness. But I have read that even small variations in problem statements can lead to change in the difficulty. So if we modify the travelling salesman problem to such that each node needed to visit can be visited more than once, will it still be NP-Complete?

Online Programming contest in Hackerrank

masm x8086

$
0
0

aim of this code is to simply print the string.

code is giving undesirable output i.e it does not print enter the number

.model small
.stack 100h

CR equ 13d LF equ 10d .data msg1 db 'Enter the num' , '0'
.code

start: mov ax, @data mov ds, ax

 mov ax, offset msg1
 call put_string
 mov ax,4c00h
 int 21h

put_string: ;mov ax, offset msg1

     push ax
 push bx
 push cx
 push dx

 mov bx, ax
 mov al, byte ptr [bx]
 put_loop:cmp al, 0
          je out_
          call put_char
          call nwln
          inc bx
          mov al, byte ptr [bx]
          jmp put_loop
out_:             
 pop ax
 pop bx
 pop cx
 pop dx
 ret
end start

please help thank you

Why this simple small python code gives TLE?

fact 2(easy)

$
0
0

whats wrong ? i get the answer in my compiler , here it says wrong answer

#include <iostream>
#include<stdio.h>
#include<math.h>

using namespace std;

double fact(int s)
{
    if(s==0)
    {
        double fact=1;
        return fact;
    }
    else
    {
        double y=fact(s-1);
        return (y*(s));
    }
}

int main()
{
    double b[100];
    int a[100],t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        scanf("%d",&a[i]);
        b[i]=fact(a[i]);    
    }
    for(int i=0;i<t;i++)
        printf("%f \n",b[i]);
    return 0;
}

Trie implementation

$
0
0

Can somebody please help with this implementation of trie. I am just inserting certain words and checking if any given word is present in trie or not. The output displayed on the link depicts the problem. There seems to be a problem with line 55 and so.. but I am unable to get it!!

Please help!


Problem regarding August Cook-Off

$
0
0

It took me 5 minutes to load the contest page. I haven't been able to load the first problem though. I'm not the only one, right ?

Algorithm vs Android development

$
0
0

Frnd m a student of b.tech 3rd year, i want to ask about that whether i have to go in Android development (as i have learnt earlier) or Algorithm development... I have only 2 year left in engineering , can i be a good competitive programmer in 2 years if i give my 1oo% ??, I can solve only one problem question everytime, you can see my codechef id here, of till now problem solved .. http://www.codechef.com/users/chaman_amit

How to do this , please suggest.

$
0
0

Consider a regular polygon with N vertices labelled 1..N. In how many ways can you draw K diagonals such that no two diagonals intersect at a point strictly inside the polygon? A diagonal is a line segment joining two non adjacent vertices of the polygon.

Input:

The first line contains the number of test cases T. Each of the next T lines contain two integers N and K.

Output:

Output T lines, one corresponding to each test case. Since the answer can be really huge, output it modulo 1000003.

Constraints:

1 <= T <= 10000

4 <= N <= 10^9

1 <= K <= N

Sample Input:

3

4 1

5 2

5 3

Sample Output:

2

5

0

Explanation:

For the first case, there are clearly 2 ways to draw 1 diagonal - 1 to 3, or 2 to 4. (Assuming the vertices are labelled 1..N in cyclic order).

For the third case, at most 2 non-intersecting diagonals can be drawn in a 5-gon, and so the answer is 0.

"shooting" question in august cook off-2014

$
0
0

*BELOW WRITTEN TEXT WAS MY CODE FOR THE SHOOTING QUESTION FOR AUGUST COOK OFF-2014 THE CODE WORKS ON MY COMPILER IF I TAKE TEST CASE=1 BUT DOES NOT LET ME INPUT ALL THE ARRAY ELEMENTS IF I TAKE REST CASES MORE THAN 1. I TRIED CHECKING IT FOR ALL 3 SAMPLE INPUT-OUTPUT VALUES EVERYTHING SEEMS OKAY. CAN SOMEONE EXPLAIN ME THE FAULT IN THE CODE, IF ANY*

include<stdio.h>

int main() { int i,j,n,m,t,x,z,flag;char arr[50][50]; scanf("%d",&t); for(z=1;z<=t;z++) { flag=0; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%ch",&arr[i][j]); }

}

for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(arr[i][j]=='L') { for(x=0;x<=j;x++) if(arr[i][x]=='E') arr[i][x]='.';

    for(x=j;x<m;x++)
    if(arr[i][x]=='E')
    arr[i][x]='.';

    for(x=0;x<=i;x++)
    if(arr[x][j]=='E')
    arr[x][j]='.';
   }
 }

} for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(arr[i][j]=='E') flag++; } } if(flag==0) printf("impossible\n"); else printf("possible\n"); } return 0; }

Problem in PERMSUFF ?

$
0
0

Can someone explain what's wrong with my code. I tried just everything and for whole contest I was with this problem. Here's my submission

Viewing all 39796 articles
Browse latest View live