 # CHFMOT18 - Editorial

Please tell me what is wrong in my code… It is giving the correct answer to all the sample test cases and the cases mentioned in the comments here…

``````#include<iostream>

using namespace std;

int main()
{
long long int t;
cin>>t;

while(t--)
{
long long int s,n;
cin>>s>>n;
long long int count=0;
if(s%2==0)
{
long long int leftover=0;
count+=s/n;
leftover=s%n;
if(leftover==0)
{
cout<<count<<endl;
}
else{
long long int i=1;
while(leftover!=0)
{
if(leftover%(n-2*i)==0)
{
count+=leftover/(n-2*i);
break;
}
i++;
}
cout<<count<<endl;
}
}
else{
long long int leftover=0;
count+=s/n;
leftover=s%n;
if(leftover==1)
{
cout<<count+1<<endl;
}
else{
long long int i=1;
while(leftover!=1)
{
if(leftover%(n-2*i)==1)
{
count+=leftover/(n-2*i);
break;
}
i++;
}
cout<<count+1<<endl;
}
}
}
}

``````

Wow , what a elegant approach , i was trying to solve this question using recursion :

``````
int find_coin_odd(long cost , long coin , int counter){
if(cost == 1){
return counter + 1;
}

counter += cost/coin ;

find_coin_odd(cost%coin,coin-2,counter);
}

int find_coin_even(long cost , long coin , int counter){
if(cost == 0){
return counter ;
}

counter += cost/coin ;

find_coin_even(cost%coin,coin-2,counter);
}
``````

Incidentally, I also tried the same approach at first You don’t need to check all the values from n to 2. First keep track of s/i. Then, the remainder has to be less than n. If s is 0, s/i is the final answer, if s is even, add 1 coin, if s is odd add 2 coins. My submissions are here:

1 Like

Inside `else if(s!=0)`, there need to be two options. If s is even, only one more coin is needed, if s is odd, two more coins are needed.

if(s%2==0)
{count += 1
} else
{count += 2

1 Like

@ butterflyeffec
When the S>N and S is an odd value
for example
S= 33, N=30
than rem= S % N = 3 and S / N = 1
So rem = 3 Now the else part will be executed
So we need a total of 3 coins (one 30 rs and one 2 rs and one 1 rs)
Thank you for given sample input my code give the correct sample output but still its showing wrong answer while submitting. anyone pls help.

#include
using namespace std;
int main() {
long long int t;
cin >> t;
while (t–){
long long int s,n,c=0,d=0,e=1 ;
cin >> s >> n;
if (n%2==0){
if (s <= n){
if (s%2 == 0)
cout << e << endl;

``````		else if (s==1)
cout << e << endl;

else
cout << e+1 << endl;
}
else{
c=s/n;
d=s%n;

if (d%2 == 0)
cout << c << endl;

else if (d==1)
cout << c+1 << endl;

else
cout << c+2 << endl;
}
}
``````

}
}

```````
``````

ll solve(){
ll s,n,ans=0;cin>>s>>n;
if(s<=n && s%2==0 || s==1) return 1;
else if(s<n && s%2!=0) return 2;
else{
ll x=s/n,r=s%n;
if(r==0) return x;
else if(r&1 && r==1) return x+1;
else if(r&1 && r!=1) return x+2;
else if(!(r&1)) return x+1;
}
}`

this was my approach.

what is wrong in my code i am getting nzec error after submitting
a=int(input())
while a>0:
ans=0
value,maxv=map(int,input().strip().split())
if value<maxv:
if value%2==0 or value==1:
ans=ans+1
else:
ans=ans+2
elif value==maxv:
ans=ans+1
elif value%2==0:
if value%maxv==0:
ans=ans+(value//maxv)
else:
ans=ans+(value//max)
if (value%maxv)%2==0:
ans=ans+1
else:
ans=ans+2
elif value%2!=0:
value=value-1
if value%maxv==0:
ans=ans+(value//maxv)+1
else:
if (value%maxv)%2==0:
ans=ans+2+(value//maxv)
else:
ans=ans+3+(value//maxv)
print(ans)
a=a-1

My code runs fine in the editor and it gives the correct answer but when i submit it gives SIGFPE Runtime Error
here is my code

#include
#include <bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
for( int i=0;i<t;i++)
{
long long int s,n,v=0,x=0,a=0,p=0,b=0;
cin>>s>>n;
p=s;
while(s-v>=2)
{
x=n;
a=p/x;
b=b+a;
v=v+x*a;
p=s-v;
n-=2;
}
if(v==s)
cout<<b<<"\n";
else
cout<<b+1<<"\n";

}
return 0;
}

``````#include <iostream>
``````

#include <bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define mp make_pair
#define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<=n;i++)
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int t;
cin>>t;
int s,n;
while(t–)
{
cin>>s>>n;
if(s==1)
cout<<“1\n”;
else
{
int ans=0;
if(s<n)
{
if(s%2==0)
ans=1;
else
ans=2;
}
else
{
ans+=s/n;
s=s%n;
if(s%2==0&&s!=0)
ans+=1;
else if(s%2==1)
ans+=2;
}
cout<<ans<<"\n";
}
}
return 0;
}
I also did with almost same login but I am getting WA. Whats going wrong?

can anybody check what is wrong in my code

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
while(t–>0)
{
long s=Long.parseLong(st);
long n=Long.parseLong(st);
long coin=0;
if(s>=n)
{
if(n%2!=0)
{

``````	           n=n-1;
coin+=s/n;
long rem=s%n;
if(s%n!=0 && rem%2==0)
{
coin+=1;
}
if(s%n!=0 && rem%2!=0)
{
coin+=2;
}
}
else
{
coin+=s/n;
long rem=s%n;
if(s%n!=0 && rem%2==0)
{
coin+=1;
}
if(s%n!=0 && rem%2!=0)
{
coin+=2;
}
}

}
else
{
if(s==1)
{
coin =1;
}
else if(s%2==0)
{
coin+=1;
}
else
{
coin+=2;
}
}
System.out.println(coin);
}
}
``````

}

https://www.codechef.com/viewsolution/37196814

Time Complexity = O(1) for each test case
Here is my code: Solution: 42979003 | CodeChef

PYTHON CODERS :

HAVE A LOOK AT THIS CODE-

times = int(input())
final = []
for i in range(0,times):
list1 = []
count = 0
coin = input()
list1 = coin.split(’ ')
first = int(list1)
divider= int(list1)
while(first!=0):
if(first % 2 !=0):
first-=1
count+=1
if(first<divider and first!=0):
count+=1
first=0
elif(first>=divider):
count+= first/divider
first = first%divider
final.append(count)

for z in final:
print(int(z))

EXECUTION TIME - 0.3s