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;
}
}
}
}
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:
@ 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;
}
}
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
#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?
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t–>0)
{
String st[]=br.readLine().trim().split(" ");
long s=Long.parseLong(st[0]);
long n=Long.parseLong(st[1]);
long coin=0;
if(s>=n)
{
if(n%2!=0)
{
//sort(arr, arr + n, greater());
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(NULL);
cin.tie(NULL);cout.tie(NULL);
ll t,ans,sum1; cin >> t;
while (t–) {
ll s,n;cin>>s>>n;
ll sum = 0;
if(s%2==0 ){
while(s>0){
sum1 = s/n;
sum = sum+sum1;
s = s % n;
n=n-2;
}
cout<<sum<<"\n";
}
else{
ll a = s-1;
while(a>0 && n>0){
sum1 = a/n;
sum = sum+sum1;
a = a % n;
n=n-2;
}
cout<<sum+1<<"\n";
}
}
} Why I am getting TLE in this solution…the given test is running correctly on other compilers