1

I have an exercise which looks like that:

Problem statement is simple and straight forward . You will be given a non-negative integer P of length N and you need to check whether it's divisible by Q ?

Integer P will be given in its decimal representation with P0 as leftmost digit and P1 as second digit from left !

Rest of the digit can be generated from the formula :

Pi = ( 4*Pi-1 + Pi-2 ) modulo Q for 2 <= i <= N-1

Input The first line contains one integer T - denoting the number of test cases.

T lines follow each containing four integers P0 , P1 , Q and N !

Output For each testcase output YES if the corresponding integer is divisible by Q and NO otherwise.

Constraints T <= 100000 0 < P0 , P1 , Q < 10 0 < N <= 1018 Example Input:

4

1 4 2 2

1 4 2 1

4 2 3 2

3 4 7 3

Output: YES NO YES NO Explanation Value of P is 14, 1, 42, 345 in respective cases !

and that's what I came up with

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;

    int main()
    {
        int t, q, n, p_0, p_1, p_temp, p;
        vector<int> digits;
        vector<string> answers;
        string number = "";
    
        cin >> t;
    
        for (int i = 0; i < t; i++)
        {
            cin >> p_0 >> p_1 >> q >> n;
            if (n == 1)
            {
                digits.push_back(p_0);
            }
            else
            {
                digits.push_back(p_0);
                digits.push_back(p_1);
                for (int i = 2; i <= (n - 1); i++)
                {
                    p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
                    digits.push_back(p_temp);
                }
            }
            for (int i = 0; i < digits.size(); i++)
            {
                number += to_string(digits[i]);
            }
            p = stoi(number);
            cout << number << endl;
            if (p % q == 0)
            {
                answers.push_back("YES");
            }
            else
            {
                answers.push_back("NO");
            }
            number = "";
        }
        for (int i = 0; i < answers.size(); i++)
        {
            cout << answers[i] << endl;
        }
    
    }

Everything I have done works fine, except for one thing, this part does not clear my number variable

    number = "";

And honestly I don't know why, could someone correct my mistakes and explain me what did I do wrong. Thanks.

6
  • 2
    Since number is std::string, you can try calling number.clear() Commented Nov 17, 2021 at 15:34
  • Doesn't work :/ Commented Nov 17, 2021 at 15:35
  • 1
    It assigns empty string to your number. How do you check that "it does not clear it"? You always std::cout the content after the digits conversion and before the clear. Commented Nov 17, 2021 at 15:38
  • 1
    You could have moved vector<int> digits; and string number = ""; to right inside the first for loop to have these cleared on each iteration. Commented Nov 17, 2021 at 15:49
  • @pptaszni raises a good point. If you don't have access to a debugger where you can step through the code 1 line at a time and look at the variables at each step, printing the values before and after something happens can be very helpful. Commented Nov 17, 2021 at 15:51

2 Answers 2

2
+500

Your problem is with the digits vector.

Each loop the number string just gets repopulated with the digits vector which is never cleared.

Use digits.clear() to empty the vector like so:

#include <iostream>
#include <vector>
#include <string>

using namespace std;



int main()
{
    int t, q, n, p_0, p_1, p_temp, p;
    vector<int> digits;
    vector<string> answers;
    string number = "";

    cin >> t;

    for (int i = 0; i < t; i++)
    {
        cin >> p_0 >> p_1 >> q >> n;

        

        if (n == 1)
        {
            digits.push_back(p_0);
        }
        else
        {
            digits.push_back(p_0);
            digits.push_back(p_1);
            for (int i = 2; i <= (n - 1); i++)
            {
                p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
                digits.push_back(p_temp);
            }
        }

        for (int i = 0; i < digits.size(); i++)
        {
            number += to_string(digits[i]);
        }

        p = stoi(number);
        cout << number << endl;

        if (p % q == 0)
        {
            answers.push_back("YES");
        }
        else
        {
            answers.push_back("NO");
        }

        digits.clear();
        number = "";
    }

    for (int i = 0; i < answers.size(); i++)
    {
        cout << answers[i] << endl;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

To clear a string you can/should use std::string::clear() as:

number.clear();

There may be other logical errors in your program which may be the reason for not getting the output you expect.

Also instead of creating/initializing the string number using string number = "";, you should use

string number;//no need to write = ""

4 Comments

I tried this and Unfortunately it still doesn't work.
It does clear the string otherwise the implementation of the standard library that you are using is broken. More likely your code has a bug and you need to use a debugger to step through the code line by line to see where its behavior deviates from your expectation.
Honestly I don't see how this answer is supposed to help, as empty string assignment has almost the same effect as calling clear (let's forget memory alloc/dealloc impl details). OP obviously has a logical error in his algorithm.
@pptaszni As i said there may be other logical errors (which is completely OP's responsibility to solve) in OP's program. I just mentioned standard ways to initialized the string and clearing the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.