CHEFSTEP - Editorial

PROBLEM LINK:

Contest

Setter: Aryan Agarwala
Tester: Yash Chandnani
Editorialist: Rajarshi Basu

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

In order to establish dominance amongst his friends, Chef has decided that he will only walk in large steps of length exactly K feet. However, this has presented many problems in Chef’s life because there are certain distances that he cannot traverse. Eg. If his step length is 5 feet, he cannot travel a distance of 12 feet. Chef has a strict travel plan that he follows on most days, but now he is worried that some of those distances may become impossible to travel. Given N distances, tell Chef which ones he cannot travel.

EXPLANATION:

We just simulate whatever is asked in the problem. We run a loop and take each number as input. If that number is divisible by K then we output a 1 else we output a 0. When we are done, we output a newline character.

SOLUTIONS:

tester’s Code
#include <bits/stdc++.h>
using namespace std;
 
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
void pre(){
 
 
}
long long readInt(long long l,long long r,char endd){
	long long x=0;
	int cnt=0;
	int fi=-1;
	bool is_neg=false;
	while(true){
		char g=getchar();
		if(g=='-'){
			assert(fi==-1);
			is_neg=true;
			continue;
		}
		if('0'<=g && g<='9'){
			x*=10;
			x+=g-'0';
			if(cnt==0){
				fi=g-'0';
			}
			cnt++;
			assert(fi!=0 || cnt==1);
			assert(fi!=0 || is_neg==false);
 
			assert(!(cnt>19 || ( cnt==19 && fi>1) ));
		} else if(g==endd){
			if(is_neg){
				x= -x;
			}
			assert(l<=x && x<=r);
			return x;
		} else {
			assert(false);
		}
	}
}
string readString(int l,int r,char endd){
	string ret="";
	int cnt=0;
	while(true){
		char g=getchar();
		assert(g!=-1);
		if(g==endd){
			break;
		}
		cnt++;
		ret+=g;
	}
	assert(l<=cnt && cnt<=r);
	return ret;
}
long long readIntSp(long long l,long long r){
	return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
	return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
	return readString(l,r,'\n');
}
string readStringSp(int l,int r){
	return readString(l,r,' ');
}
void solve(){
	int n=readIntSp(1,1000),k=readIntLn(1,1e9);
	rep(i,n-1){
		int d = readIntSp(1,1e9);
		cout<<(d%k==0);
	}
	{
		int d = readIntLn(1,1e9);
		cout<<(d%k==0)<<'\n';
	}
 
}
 
int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();
	int n=readIntLn(1,1000);
	rep(i,n) solve();
	assert(getchar()==EOF);
	return 0;
}

I did exact same using java but getting TLE. I even tried fast io but was still getting TLE.Can you please tell why .

my code-

import java.util.Scanner;

class Chef_and_Steps {

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t-- > 0)
	{
		int n = sc.nextInt();
		int k = sc.nextInt();
		
		while(n-- > 0)
		{
			int x = sc.nextInt();
			
			if(x % k == 0)
				System.out.print("1");
			else
				System.out.print("0");
		}
	}
	sc.close();
}

}

instead of printing ans for each value of n keep adding ‘1’ and ‘0’ to ans string for each test case and then print and for test case ,dont forget end line

1 Like

I just dont know, why this code didnt pass?

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl “\n”
#define REP(i,n) for(int i=0;i<n;i++)
#define REPJ(j,n) for(int j=0;j<n;j++)
#define PER(i,n) for(int i=n;i>0;i–)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPJ1(j,n) for(int j=1;j<=n;j++)
#define ull unsigned long long int
#define ld long double
#define prec(n) fixed<<setprecision(n)
#define test ll t; cin>>t; while(t–)
#define re return 0
int main()
{
IOS;
test{
ll n,k;
cin>>n>>k;
ll a[n],b[n];
REP(i,n){
cin>>a[i];
if(a[i]>=k){
if(a[i]%k==0){
b[i]=1;
}else{
b[i]=0;
}
}else if(a[i]<k){
b[i]=0;
}
}
REP(i,n){
cout<<b[i];
}
}
re;
}

i did the same using java and got tle
also i optimized io and added each no. to a string and then printed

adarsh i did the same but still i got tle
i also used fastreader for faster io
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
class Distance {

    public static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }

public static void main(String[] args) {
	FastReader sc=new FastReader();
	int t=sc.nextInt();
	String str=new String("");
	while(t-->0)
	{
		int s=sc.nextInt();
		int k=sc.nextInt();
		int ar[]=new int[s];
		for(int i=0;i<s;i++)
		{
			ar[i]=sc.nextInt();
			if(ar[i]%k==0)
				ar[i]=1;
			else
				ar[i]=0;
			str=str+ar[i];
		}
		System.out.println(str);
	}
}

}

I am not familiar with java , but here is my solution with same idea in c++ hope it helps.

CAN SOMEONE HELP ME WITH THIS…
TELL ME WHATS WRONG !!!
#include <stdio.h>

int main(void) {
// your code goes here
int t,n,i;
long long k;
scanf("%d",&t);
while(t–)
{
scanf("%d",&n);
scanf("%llu",&k);
long long arr[n];
int a[n];
for(i=0;i<n;i++)
{
scanf("%llu",&arr[i]);
}

    for(i=0;i<n;i++)
    {
        if(arr[i]%k==0)
         {a[i]=1;}
        else
          a[i]=0;
    }

    for(int i=0;i<n;i++)
    {printf("%d",a[i]);}
   
}
return 0;

}

After every testcase you have to use “\n”
since for every test case you have to print a single line

same bro even i did same just output the 0 or 1 while taking steps size simultaneously and didn’t use extra loop for and still got tle.

CAN SOMEONE PLZZ TELL WHAT’S THE ERROR IN THIS PYTHON CODE(WA)
try:
for i in range(int(input())):
s = str(input())
p = str(input())
l1 = s.split()
l2 = p.split()
n = int(l1[0])
m = int(l1[1])
res = ‘’
for j in range(n):
if (int(l2[j])%m==0):
res = res+‘1’
else:
res = res+‘0’
print(int(res))
except:
pass

#include<iostream>

using namespace std;

int main()
{
int t,i,j,n;
cin>>t;

long long a[n],k;
char arr[n];
for(i=0;i<t;i++)
{
cin>>n;
cin>>k;
for(j=0;j<n;j++)
{
cin>>a[j];
if(a[j]%k==0)
{
arr[j]=‘1’;
}
else{arr[j]=‘0’;}
}
cout<<arr<<endl;
}

}

this is my submission a simple one

i say that when i run any submission i got runtime error and when i submit same submission i got right submission . so what i doo. if any one face this problem and solve this. please told mee

please remove the explicit type conversion in the print statement. output should be a string.

1 Like

@aryanag_adm Hi :slight_smile: At the time of writing, the “Contest” link in the OP is a 404.

Also, could you change the “Output:” section of the Problem Statement? Unlike most Problems, it doesn’t say “For each test case, print a single line” etc, which has mislead at least one person.

Thanks!

Edit: Make that two :slight_smile:

2 Likes

n = int(input())
result = “”
for i in range(n):
dist, stepval = input().split()
stepval = int(stepval)
lis = input().split()
for i in lis:
if int(i) % stepval == 0:
result += “1”
else:
result += “0”
print(result)

I am new to coding, what did I do wrong,
my custom test cases work but after submitting i got a nzec error.

Instead of appending String to a String use StringBuilder. Strings in Java are immutable.

StringBuilder ans= new StringBuilder();
ans.append(Integer. toString (1));

I am not familiar with the language you are using but i think there is an error in taking input from the user

Here is my answer for java:

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
static class FastReader{
BufferedReader br;
StringTokenizer st;

    public FastReader(){
        br=new BufferedReader(new InputStreamReader(System.in));
    }
    String next(){
        while(st==null || !st.hasMoreTokens()){
            try{
                st=new StringTokenizer(br.readLine());
            }
            catch(IOException e){
                 e.printStackTrace();
            }
    }
        return st.nextToken();
    }
    int nextInt(){
        return Integer.parseInt(next());
    }
    long nextLong(){
        return Long.parseLong(next());
    }
    double nextDouble(){
        return Double.parseDouble(next());
    }
    String nextLine(){
        String str="";
        try{
            str=br.readLine().trim();
        }catch(IOException e){
            e.printStackTrace();
        }
        return str;
    }
}
public static void main (String[] args) throws java.lang.Exception
{
	FastReader in=new FastReader();
	int t=in.nextInt();
	while(t--!=0){
	    int n=in.nextInt();
	    int k=in.nextInt();
	    String ans="";
	    for(int i=0;i<n;i++){
	        int step=in.nextInt();
	        if(step%k==0){
	            ans=ans+1;
	        }else{
	            ans=ans+0;
	        }
	    }
	    System.out.println(ans);
	}
	
	
}

}