How to initialize structure to zero everytime in while loop? - c++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
}s[max];;
int main(){
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}
In this code, how I will initialize struct to zero in every while loop. I have tried memset() but It is showing error. Also I created struct object in main function. But the program crashes. Please give me hints to solve the problem.

Try this instead, its more structured, usually less error prone.
std::fill(std::begin(s), std::end(s), st({0,0,0}));
I might even be converted to memset by the compiler.

Surely there will be a better solution than this but you can add this snippet at the beginning of the while loop:
for(int j=0; j<max; j++){
s[j].val=0;
s[j].index=0;
s[j].freq=0;
}
You can also consider the idea of redeclaring the struct array every time in the loop, just like this:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
};
int main(){
ll t;
cin>>t;
while(t--){
st s[max];
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max 200005
struct st{
ll index,freq,val;
};
int main(){
ll t;
cin>>t;
while(t--){
st s[max]={0};
ll n;
cin>>n;
ll counter[max]={0};
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
s[x].val=x;
s[x].index=i;
s[x].freq++;
cout<<s[x].freq<<endl;
}
}
return 0;
}
I did this.
The problem is solved. I used dev c++ ide, that's why the code was doing problem. Thanks all

Related

I am trying to find the factors of factorial of a number

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.

codeforces problem named Strange List the "MEMORY LIMIT EXCEEDED"

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
}
}

I need help regarding weird time limit exceeded error in cpp

So there was this code which got accepted when i took its output in a vector
#include <iostream>
#include<vector>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
vector<int>v;
int n,k; cin >> n >> k;
for(int i=0;i<n;i++){
int x; cin >> x;
if(x%k==0) v.push_back(1);
else v.push_back(0);
}
for(auto x:v) cout <<x <<"";
cout << endl;
}
return 0;
}
but then there is this code gives Time Limit Exceeded error when i am direct printing it
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
cout<<"1";
else
cout<<"0";
}
cout<<"\n";
}
}
Can you tell why ?
(The contest is now over)Here is the question in case
Edit:1 I used int instead of long long as well as printf as well as cin.tie(NULL) stuff , but still to no avail
The implementation with the cout in the for loop body is going to bottle-beck on the cout output for sure, especially given a modulo operation is very cheap in contrast.
See below question as reference:
C++: Does cout statement makes code slower
Something like this would work better:
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
std::vector<bool> r(n);
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
r[i] = true;
}
for(auto i : r)
cout<<(i ? '1' : '0')<<endl;
cout<<"\n";
}
}

I got a RunTime error SIGCONT for the below code

#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,ctr=0;
cin>>n;
int a[n],ptr=750;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<ptr){
ctr++;
ptr=a[i];
}
}
cout<<ctr-(n/5)+1<<"\n";
}
return 0;
}
All other answers say I would have divided by zero or accessed out of bounds but here I do not find any such situation

SIGABRT error in my code in c++

I am getting SIGABRT error in my code. Can anyone tell me from where it might be coming?
Here's my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int d=stoi(s,nullptr,2);
int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
string s;
cin>>s;
long long int d=stoi(s,nullptr,2);
long long int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}`enter code here`
}
Now Try This