0

i was writing the code for no of numbers less than a number C of length b that could be made using elements from a set "a" (same no can be chosen more than once) then while debugging i encountered this wierd problem every time i try to cout<< something in the function it shows a runtime error otherwise executes fine ca someone help me with this thanks for your help

here is the code

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &a, int b, int C) {
    int n=a.size();
    int copy=C;

    if(C==0||n==0)return 0;

    vector <int> c;

    while(C>0){
        c.push_back((C%10));
        C=C/10;
    }
     //cout<<C;               //shows a runtime error if this included otherwise not
    int l=c.size();
    reverse(c.begin(),c.end());

    int flag=0;
    if(find(a.begin(),a.end(),0)!=a.end())
    flag=1;

    if(l>b){
    if(flag==1 && b!=1)
    return pow(n,b-1)*(n-1);
    else
    return pow(n,b);
    }

    if(l<b)
    return 0;

    if(l==1){
        int cth=0;
        for(int i=0;i<n;i++){
            if(a[i]<c[0])
            cth++;
        }
        return cth;
    }

    int dp[n][2];
    int ctl=0,cte=0;

    for(int i=0;i<n;i++){
        if(a[i]<c[0]&&a[i]!=0)
        ctl++;

        if(a[i]==c[0])
        cte++;
    }

    dp[0][0]=ctl;
    dp[0][1]=cte;


    for(int i=1;i<b;i++){
        int ctL=0,ctE=0;
        for(int j=0;j<n;j++){if(a[j]<c[i])ctL++; else{if(a[j]==c[i])ctE++;}}
        dp[i][0]=dp[i-1][0]*n+dp[i-1][1]*ctL;
        dp[i][1]=dp[i-1][1]*ctE;
    }

    return dp[b-1][0];

} 

int main() {
    int n;
    cin>>n;

    vector<int> a(n);
    for(int i=0;i<n;i++)
    cin>>a[i];
    int b,c;
    cin>>b>>c;
    cout<<solve(a,b,c);

}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
noobie
  • 13
  • 1
  • 4
  • 2
    I'm guessing your doing something for an online judge/competition site? I can tell because the code show-cases all the bad habits perpetrated by beginners going to such sites. Don't use such sites as a learning resource, because they're *not* learning resources. Take classes, read [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), but don't go to such sites until you already have plenty of knowledge of C++ and programming and algorithms and data-structures. – Some programmer dude Apr 13 '20 at 11:49
  • [Can't reproduce](https://rextester.com/OYLP78365). No runtime error here. – Igor Tandetnik Apr 13 '20 at 11:59

0 Answers0