Dynamic programming approach for finding perfect square subsequence












0












$begingroup$


Given an array of numbers $a_i$ with length $n$, such that $0<a_i<30$, how can we find the number of subsequences that the product of numbers is perfect square.



I have tried to find a dynamic programming approach but had no success so far.










share|cite|improve this question







New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Use linear algebra instead.
    $endgroup$
    – Yuval Filmus
    16 hours ago
















0












$begingroup$


Given an array of numbers $a_i$ with length $n$, such that $0<a_i<30$, how can we find the number of subsequences that the product of numbers is perfect square.



I have tried to find a dynamic programming approach but had no success so far.










share|cite|improve this question







New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Use linear algebra instead.
    $endgroup$
    – Yuval Filmus
    16 hours ago














0












0








0





$begingroup$


Given an array of numbers $a_i$ with length $n$, such that $0<a_i<30$, how can we find the number of subsequences that the product of numbers is perfect square.



I have tried to find a dynamic programming approach but had no success so far.










share|cite|improve this question







New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




Given an array of numbers $a_i$ with length $n$, such that $0<a_i<30$, how can we find the number of subsequences that the product of numbers is perfect square.



I have tried to find a dynamic programming approach but had no success so far.







algorithms






share|cite|improve this question







New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|cite|improve this question







New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this question




share|cite|improve this question






New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 17 hours ago









besmelbesmel

285




285




New contributor




besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






besmel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Use linear algebra instead.
    $endgroup$
    – Yuval Filmus
    16 hours ago


















  • $begingroup$
    Use linear algebra instead.
    $endgroup$
    – Yuval Filmus
    16 hours ago
















$begingroup$
Use linear algebra instead.
$endgroup$
– Yuval Filmus
16 hours ago




$begingroup$
Use linear algebra instead.
$endgroup$
– Yuval Filmus
16 hours ago










1 Answer
1






active

oldest

votes


















2












$begingroup$

Let us assume first that by subsequence you mean non-contiguous subsequence.



There are 10 primes in the range $1,ldots,29$:
$$ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. $$
You can represent each number in the range $1,ldots,29$ as a vector of length 10 of exponents. A set of such vectors correspond to numbers whose product is a perfect square iff they sum to a vector whose entries are all even. Alternatively, if we think of the exponents modulo 2, then a set of such vectors correspond to numbers whose product is a perfect square iff they sum to zero.



This suggests the following algorithm. Construct a matrix whose rows correspond to the vectors outlined above (modulo 2), and determine its rank. From the rank you can compute the size of the kernel, which is the quantity you’re after.





Suppose now that you’re after contiguous subsequences.



Let $b_i = a_1 cdot a_2 cdots a_i$. The subsequence $a_i,ldots,a_j$ multiplies to a perfect square iff $b_j/b_{i-1}$ is a perfect square, which happens if the vectors corresponding to $b_{i-1}$ and $b_j$ are equal.



This suggests the following algorithm. Compute the vectors corresponding to each $b_i$ (do this by computing the vectors for $a_i$ and summing them as you go), sort them (you can represent them as 10-bit integers in an arbitrary way), and divide them into runs of length $c_1,ldots,c_ell$. The answer is then $sum_i binom{c_i}{2}$.






share|cite|improve this answer









$endgroup$














    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "419"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    besmel is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f106546%2fdynamic-programming-approach-for-finding-perfect-square-subsequence%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2












    $begingroup$

    Let us assume first that by subsequence you mean non-contiguous subsequence.



    There are 10 primes in the range $1,ldots,29$:
    $$ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. $$
    You can represent each number in the range $1,ldots,29$ as a vector of length 10 of exponents. A set of such vectors correspond to numbers whose product is a perfect square iff they sum to a vector whose entries are all even. Alternatively, if we think of the exponents modulo 2, then a set of such vectors correspond to numbers whose product is a perfect square iff they sum to zero.



    This suggests the following algorithm. Construct a matrix whose rows correspond to the vectors outlined above (modulo 2), and determine its rank. From the rank you can compute the size of the kernel, which is the quantity you’re after.





    Suppose now that you’re after contiguous subsequences.



    Let $b_i = a_1 cdot a_2 cdots a_i$. The subsequence $a_i,ldots,a_j$ multiplies to a perfect square iff $b_j/b_{i-1}$ is a perfect square, which happens if the vectors corresponding to $b_{i-1}$ and $b_j$ are equal.



    This suggests the following algorithm. Compute the vectors corresponding to each $b_i$ (do this by computing the vectors for $a_i$ and summing them as you go), sort them (you can represent them as 10-bit integers in an arbitrary way), and divide them into runs of length $c_1,ldots,c_ell$. The answer is then $sum_i binom{c_i}{2}$.






    share|cite|improve this answer









    $endgroup$


















      2












      $begingroup$

      Let us assume first that by subsequence you mean non-contiguous subsequence.



      There are 10 primes in the range $1,ldots,29$:
      $$ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. $$
      You can represent each number in the range $1,ldots,29$ as a vector of length 10 of exponents. A set of such vectors correspond to numbers whose product is a perfect square iff they sum to a vector whose entries are all even. Alternatively, if we think of the exponents modulo 2, then a set of such vectors correspond to numbers whose product is a perfect square iff they sum to zero.



      This suggests the following algorithm. Construct a matrix whose rows correspond to the vectors outlined above (modulo 2), and determine its rank. From the rank you can compute the size of the kernel, which is the quantity you’re after.





      Suppose now that you’re after contiguous subsequences.



      Let $b_i = a_1 cdot a_2 cdots a_i$. The subsequence $a_i,ldots,a_j$ multiplies to a perfect square iff $b_j/b_{i-1}$ is a perfect square, which happens if the vectors corresponding to $b_{i-1}$ and $b_j$ are equal.



      This suggests the following algorithm. Compute the vectors corresponding to each $b_i$ (do this by computing the vectors for $a_i$ and summing them as you go), sort them (you can represent them as 10-bit integers in an arbitrary way), and divide them into runs of length $c_1,ldots,c_ell$. The answer is then $sum_i binom{c_i}{2}$.






      share|cite|improve this answer









      $endgroup$
















        2












        2








        2





        $begingroup$

        Let us assume first that by subsequence you mean non-contiguous subsequence.



        There are 10 primes in the range $1,ldots,29$:
        $$ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. $$
        You can represent each number in the range $1,ldots,29$ as a vector of length 10 of exponents. A set of such vectors correspond to numbers whose product is a perfect square iff they sum to a vector whose entries are all even. Alternatively, if we think of the exponents modulo 2, then a set of such vectors correspond to numbers whose product is a perfect square iff they sum to zero.



        This suggests the following algorithm. Construct a matrix whose rows correspond to the vectors outlined above (modulo 2), and determine its rank. From the rank you can compute the size of the kernel, which is the quantity you’re after.





        Suppose now that you’re after contiguous subsequences.



        Let $b_i = a_1 cdot a_2 cdots a_i$. The subsequence $a_i,ldots,a_j$ multiplies to a perfect square iff $b_j/b_{i-1}$ is a perfect square, which happens if the vectors corresponding to $b_{i-1}$ and $b_j$ are equal.



        This suggests the following algorithm. Compute the vectors corresponding to each $b_i$ (do this by computing the vectors for $a_i$ and summing them as you go), sort them (you can represent them as 10-bit integers in an arbitrary way), and divide them into runs of length $c_1,ldots,c_ell$. The answer is then $sum_i binom{c_i}{2}$.






        share|cite|improve this answer









        $endgroup$



        Let us assume first that by subsequence you mean non-contiguous subsequence.



        There are 10 primes in the range $1,ldots,29$:
        $$ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. $$
        You can represent each number in the range $1,ldots,29$ as a vector of length 10 of exponents. A set of such vectors correspond to numbers whose product is a perfect square iff they sum to a vector whose entries are all even. Alternatively, if we think of the exponents modulo 2, then a set of such vectors correspond to numbers whose product is a perfect square iff they sum to zero.



        This suggests the following algorithm. Construct a matrix whose rows correspond to the vectors outlined above (modulo 2), and determine its rank. From the rank you can compute the size of the kernel, which is the quantity you’re after.





        Suppose now that you’re after contiguous subsequences.



        Let $b_i = a_1 cdot a_2 cdots a_i$. The subsequence $a_i,ldots,a_j$ multiplies to a perfect square iff $b_j/b_{i-1}$ is a perfect square, which happens if the vectors corresponding to $b_{i-1}$ and $b_j$ are equal.



        This suggests the following algorithm. Compute the vectors corresponding to each $b_i$ (do this by computing the vectors for $a_i$ and summing them as you go), sort them (you can represent them as 10-bit integers in an arbitrary way), and divide them into runs of length $c_1,ldots,c_ell$. The answer is then $sum_i binom{c_i}{2}$.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered 15 hours ago









        Yuval FilmusYuval Filmus

        196k15184349




        196k15184349






















            besmel is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            besmel is a new contributor. Be nice, and check out our Code of Conduct.













            besmel is a new contributor. Be nice, and check out our Code of Conduct.












            besmel is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Computer Science Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f106546%2fdynamic-programming-approach-for-finding-perfect-square-subsequence%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Callistus I

            Tabula Rosettana

            How to label and detect the document text images