有没有办法生成一个包含平方的序列,这些平方加起来就是一个整数平方?

Is there a way to generate a sequence that includes squares which sum up to an integer square?

本文关键字:一个 方加 整数 起来 包含平 有没有      更新时间:2023-10-16

decompose(11)必须返回[1,2,4,10]。 请注意,实际上有两种分解 11² 的方法,11² = 121 = 1 + 4 + 16 + 100 = 1² + 2² + 4² + 10²。

对于decompose(50),不要返回[1, 1, 4, 9, 49]而是[1, 3, 5, 8, 49],因为[1, 1, 4, 9, 49]不会形成严格递增的序列。

我创建了一个函数,但只有在某些情况下提供了一个严格递增的序列,我的所有解决方案加起来都是正确的数字,我必须进行哪些更改才能返回严格递增的序列?

vector<ll> Decomp::decompose(ll n){
ll square = n * n, j = 1, nextterm = n - 1, remainder, sum = 0;
float root;
vector<ll> sequence;
do
{
sequence.push_back(nextterm);
sum = sum + (nextterm * nextterm);
remainder = square - sum;
root = sqrt(remainder - 1);
if (root - (int)root > 0)
{
root = (int)root;
}
j = 1;
nextterm = (int)root;
if (remainder == 1)
{
sequence.push_back(1);
}
} while (root > 0);
reverse(sequence.begin(),sequence.end());
for (int i=0; i < sequence.size(); i++)
{
cout << sequence[i] << endl;
}
}

这是一个简单的递归方法,基本上探索了所有的可能性.
一旦找到解决方案,它就会停止。

输出:

11 : 1 2 4 10
50 : 1 3 5 8 49

和代码:

#include    <iostream>
#include    <vector>
#include    <algorithm>
#include    <cmath>
bool decompose_dp (long long int sum, long long int k, std::vector<long long int> &seq) {

while (k > 0) {
long long int sump = sum - k*k;
if (sump == 0) {
seq.push_back(k);
return true;
}
if (sump < 0) {
k--;
continue;
}
long long int kp = k-1;

while (kp > 0) {
if (decompose_dp(sump, kp, seq)) {
seq.push_back(k);
return true;
}
kp --;
}
k--;
}
return false;
}

std::vector<long long int> decompose(long long int n){
long long int square = n * n, j = 1, nextterm = n - 1, remainder, sum = 0;
float root;
std::vector<long long int> sequence;

auto check = decompose_dp (n*n, n-1, sequence);
return sequence;
}
void pr (long long int n, const std::vector<long long int> &vec) {
std::cout << n << " : ";
for (auto k: vec) {
std::cout << k << " ";
}
std::cout << "n";
}
int main() {
long long int n = 11;
auto sequence = decompose (n);
pr (n, sequence);
n = 50;
sequence = decompose (n);
pr (n, sequence);
}

这是 Python 中的 BFS、DFS 和蛮力。BFS 对于输入 50 似乎很慢。蛮力为输入 50 产生 91020 种不同的组合。

from collections import deque
def bfs(n):
target = n * n
queue = deque([(target, [], 1)])
while queue:
t, seq, i = queue.popleft()
if t == 0:
return seq
if (t == target and i*i < t) or (t != target and i*i <= t):
queue.append((t - i*i, seq[:] + [i], i + 1))
queue.append((t, seq, i + 1))
def dfs(n):
target = n * n
stack = [(target, [], 1)]
while stack:
t, seq, i = stack.pop()
if t == 0:
return seq
if (t == target and i*i < t) or (t != target and i*i <= t):
stack.append((t - i*i, seq[:] + [i], i + 1))
stack.append((t, seq, i + 1))
def brute(n):
target = n * n
stack = [(target, [], 1)]
result = []
while stack:
t, seq, i = stack.pop()
if t == 0:
result.append(seq)
if (t == target and i*i < t) or (t != target and i*i <= t):
stack.append((t - i*i, seq[:] + [i], i + 1))
stack.append((t, seq, i + 1))
return result
print bfs(50) # [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20]
print dfs(50) # [30, 40]
#print brute(50)