MHB Writing a number as sum of squares

AI Thread Summary
The discussion revolves around solving a combinatorial problem from HackerRank that requires finding the number of n-tuples of positive integers whose squares sum to a given integer k, while maintaining a non-decreasing order. The problem is framed with constraints allowing up to 100,000 test cases, where n can be as large as 100 and k can go up to 5000. A dynamic programming (DP) approach is initially attempted, where a 2D array is used to store the number of ways to express integers as sums of squares. However, the implementation encounters an issue with overcounting solutions due to duplicate values in the tuples. Suggestions for addressing this overcounting include tracking solutions to eliminate duplicates or adjusting counts by dividing by the factorial of the counts of repeated elements. Despite these suggestions, concerns are raised about the feasibility of these methods within the time constraints, leading to a preference for the DP approach, although it requires further refinement to avoid overcounting.
Saitama
Messages
4,244
Reaction score
93
Here's the problem statement from HackerRank: https://www.hackerrank.com/contests/programaniacs-june-15/challenges/sum-of-squares-1

Problem Statement

Find the number of n-tuples $(a_1,a_2,a_3,\cdots, a_n)$ such that

$$a_1^2+a^2_{2}+a^2_{3}+\cdots+a^2_{n} = k\,\,\,\text{and}\,\,\, a_1≤a_2≤\cdots≤a_n\,\, \text{where} \,\,a_i>0$$

Input Format

The first line contains the number of test cases $T$.

The next $T$ lines contain two integers, $n$ and $k$.

Constraints

$1≤T≤10^5$
$1≤n≤100$
$1≤k≤5000$

Output Format

For each of the $T$ test cases, output one integer, the answer to the problem.

Sample Input

4
1 2
1 4
2 50
3 1000

Sample Output

0
1
2
2

Since the constraints are small, I tried a DP solution. Code I have written so far:

Code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

ll dp[5010][101]={};

int main() {
    ll T, i, j, n, k;
    vector<ll> sq;
    for(i=1; i*i<=5000; ++i)
        sq.push_back(i*i);
    for(i=0; i<sq.size(); ++i)
        dp[sq[i]][1]=1;
    for(i=2; i<=5000; ++i)
    {
        for(j=2; j<=min(i, 100LL); ++j)
        {
            for(k=0; k<sq.size(); ++k)
            {
                if(sq[k]>i)
                    break;
                dp[i][j]=dp[i][j]+dp[i-sq[k]][j-1];
            }
        }
    }
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        cout<<dp[k][n]<<endl;
    }
    return 0;
}

dp[j] denotes the number of ways to write $i$ as sum of $j$ squares.

The problem with the above code that it overcounts the number of ways. How do I fix this issue?

Thanks!
 
Technology news on Phys.org
Hi Pranav,

The typical reason to overcount in a situation like this, is that a solution with the same value more than once, will be counted more than once.

So for instance $1^2+1^2+2^2+2^2+3^2+3^2+3^2 = k$ will typically be counted $2! \cdot 2! \cdot 3!$ times.

Easiest way to fix it, is to track the solutions and eliminate duplicates.
Alternatively, we can compensate and count for instance my example only for the fraction $1 / (2! \cdot 2! \cdot 3!)$.

Unfortunately, I guess that doesn't work very well with your dynamic programming approach.
Alternatively, we could try a back tracking approach that will yield full solutions.
 
I like Serena said:
Alternatively, we could try a back tracking approach that will yield full solutions.

But that won't run under the given time constraints. (Sweating)

I can't think of a way besides dp. :(
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top