With reference to the problem named "Strange List" in codeforces following is my code
But the problem is All test case is passing but the last test case it is showing"MEMORY EXCEEDED LIMIT" and the last test case value is also huge like for n it is 100000.
Please guide me the way to tackle it
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll ele,sum=0,i,j,h,n,x;
cin>>n>>x;
vector <ll> vec;
for(i=0;i<n;i++)
{
cin>>ele;
vec.push_back(ele);
}
for(i=0;i>=0;i++)
{
if(vec.at(i)%x==1)
{
break;
}
else
{
h=vec.at(i)/x;
for(j=0;j<x;j++)
{
vec.push_back(h);
}
}
}
for(i=0;i<vec.size();i++)
{
sum+=vec.at(i);
}
cout<<sum<<"\n";
}
}
You can use Dynamic Programming to solve this problem
Implementation of storing results can be done by using Vector of pairs
Below is the AC code demonstrates how these kinds of problems can be done efficiently in terms of memory consumption......Happy Coding :)
CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
vector<pair<ll,ll>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first;
a[i].second=a[i].first; // Storing initial results in second part of pair
}
ll s=0;
ll d=0;
ll r=0;
ll k=0;
while(++r){
for(int i=0;i<n;i++){
if(a[i].first%x==0){
s+=a[i].second;
a[i].first=a[i].first/x;
a[i].second=(a[i].first*pow(x,r)); // updating results
}
else{
s+=a[i].second;
if(d==0)
k=i;
d=1;
}
}
if(d)
break;
}
for(int i=0;i<k;i++)
s+=a[i].second; // getting desired sum
cout<<s<<"\n"; // printing answer
}
}
Related
I am trying to solve the SPOJ problem DIVFACT where we need to find the factorial of a number. Though I used the correct formulae and at the same time I also checked the special case of 0 and 1,still I am getting wrong answer and it's really difficult for me to figure out what's wrong with my code. Can someone please help me figure out the problem? For reference I am providing the link to the problem:- Link of the problem
#include<iostream>
#include<vector>
#define MOD 100000007
#define MAX 50001
#define pb push_back
using namespace std;
typedef long long ll;
ll no_of_factors(int num,vector<int> primes)
{
ll result=1;
for(int i=0;i<primes.size();i++)
{
if(primes[i]>num)
break;
ll k=primes[i];
ll count=0;
while(num/k!=0){
count=(count+(num/k))%MOD;
k=k*primes[i];
}
result=(result*((count+1)%MOD))%MOD;
}
return result;
}
vector<int> seive(){
bool isPrime[MAX];
vector<int> v;
for(int i=2;i<MAX;i++)
isPrime[i]=true;
for(int i=2;i*i<MAX;i++)
{
if(isPrime[i])
{
for(int j=i*i;j< MAX;j+=i)
isPrime[j]=false;
}
}
for(int i=2;i<MAX;i++)
{
if(isPrime[i])
v.pb(i);
}
return v;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int num;
ll divisors;
cin>>num;
vector<int> v1;
v1=seive();
divisors=no_of_factors(num,v1);
cout<<divisors<<endl;
}
return 0;
}
The problem states that the answer should be in MOD 10^9+7 but you have mistakenly defined MOD as 10^8+7.
I have given an array and I need to find is there any duplicate element present in array or not? (I have to take input from the user.)
This is my first code which is giving the wrong answer for some test cases:
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
unordered_map<int,int> mp;
int temp=1;
for(int i=0,i<n;i++)
{
int x; cin>>x;
if(mp.find(x)!=mp.end())
{
temp=-1; break;
}
mp[x]++;
}
if(temp==-1) cout<<"YES"<<endl; //if duplicate present
else cout<<"NO"<<endl;
mp.clear();
}
return 0;
}
And this code is running perfectly on all the test cases:
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
unordered_map<int,int> mp;
int temp=1;
for(int i=0,i<n;i++)
{
int x;
cin>>x;
mp[x]++;
}
if(mp.size()<n) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
mp.clear();
}
return 0;
}
what is the reason behind it?
You used break; in the first code, but didn't use that in the second code.
Using break; will prevent the code from reading part of test case and treat that as next text case, and it will lead to Wrong Answer.
For example, using this input
2
5 5 5 1 2 3
2 2 2
Your first code will print
YES
NO
while the correct output is
YES
YES
To prevent this, the break; should be removed.
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
My code is showing TLE when there when number of entries in array(n) is 1 *10^5. What should i do? I saw the submission status and in all cases it ran fine except in the last case when n is 100 000, it shows time limit error.
Problem:: https://codeforces.com/contest/474/problem/B
My solution: https://pastebin.com/5RLBirpF
Code:
#include <iostream>
#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;
int arr[t];
int arrfreq[t];
int sum=1;
for(int i=0;i<t;i++ )
{
cin>>arr[t];
sum+=arr[t];
arrfreq[i]=sum;
}
int m;
cin>>m;
int qsn[m];
int k;
for(int i =0;i<m;i++)
{
cin>>qsn[i];
}
for(int j =0;j<t;j++)
{
if(k<arrfreq[j])
{
cout<<j+1<<"\n";
break;
}
if(k==arrfreq[j])
{
cout<<j+2<<"\n";
break;
}
}
}
You are using one loop for calculating the number of pile. What you can do instead is find the answer in the same loop in which you are taking the input for qsn. Just take the input of qsn and find the pile number in that loop itself. That will reduce your code's time complexity and remove the TLE error.
I saw other solutions on the internet and found out that std::lower_bound function is what has to be used to avoid T.L.E.
But one thing, as mentioned on internet, this function returns a pointer to the lower bound, then , why are we subtracting c from it and then +1 also(in the 2nd last cout line)??(refer to the code.)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j,cnt=0,ans,x,sum=0;
cin>>n;
int a[n],c[n];
for(i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
c[i]=sum;
}
cin>>m;
int b[m];
for(i=0; i<m; i++) cin>>b[i];
for(j=0; j<m; j++)
{
cout<<lower_bound(c,c+n,b[j])-c+1<<endl;
}
return 0;
}
Hi I started doing code chef beginner problems and got stuck at this one.I tried reducing the stack memory by declaring the arrays globally but that doesn't work too.Here is my code
#include<iostream>
using namespace std;
#define max 101
int main()
{
int t,n;
cin>>t;
while(t--)
{
int a[max];
int c[max]={0};
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
c[a[j]]++;
}
int temp=0;
int x=0;
for(int k=0;k<n;k++)
{
if(c[k]>temp)
{
temp=c[k];
x=k;
}
}
cout<<x<<" "<<temp<<endl;
}
}
You may need a data structure that does not limit the input value to be bounded, or just change your algorithm.
Either use std::map<int, int> in place of c to count occurence of each number, or just sort a to aggregate same values and count.