整数排列函数的算法

发布于 2024-09-30 01:11:54 字数 489 浏览 7 评论 0原文

我想写一些函数如下

y = f(x) 和另一个函数,
x = g(y) 充当可逆函数,其中
y = f(g(y)) 并且其中 x 和 y 是置换整数。

对于 0 到 10 之间的整数范围内的非常简单的示例,它看起来像这样:

0->1  
1->2  
2->3  
...  
9->10  
10->0 

但这是最简单的方法,即加 1 并通过减 1 进行反转。

我想要一个更复杂的算法,可以执行以下操作,

234927773->4299  
34->33928830  
850033->23234243423  

但是可以通过转换获得相反的结果

可以通过存储唯一整数对的巨大表来获得解决方案,但这将是不正确的。这必须是一个函数。

I want to write some functions as follows

y = f(x) and another function,
x = g(y) that acts as a reversible, where
y = f(g(y)) and where x and y are permutated integers.

For very simple example in the range of integers in 0 to 10 it would look like this:

0->1  
1->2  
2->3  
...  
9->10  
10->0 

but this is the simplest method by adding 1 and reversing by subtracting 1.

I want to have a more sofisticated algorithm that can do the following,

234927773->4299  
34->33928830  
850033->23234243423  

but the reverse can be obtained by conversion

The solution could be obtained with a huge table storing pairs of unique integers but this will not be correct. This must be a function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑白记忆 2024-10-07 01:11:54

你可以只进行异或运算。

y = x XOR p
x = y XOR p

虽然不是我的专业领域,但我认为密码学应该为您的问题提供一些有价值的答案。

You could just XOR.

y = x XOR p
x = y XOR p

Though not my area of expertise, I think that cryptography should provide some valuable answers to your question.

浅笑依然 2024-10-07 01:11:54

如果您的排列域是 2 的幂,则可以使用任何分组密码:“f”是使用特定密钥进行加密,“g”是使用相同密钥进行解密。如果您的域不是 2 的幂,您可能仍然可以使用分组密码:请参阅 本文

If the domain of your permutation is a power of 2, you can use any block cipher: 'f' is encryption with a specific key, and 'g' is decryption with the same key. If your domain is not a power of 2, you can probably still use a block cipher: see this article.

ぃ双果 2024-10-07 01:11:54

您可以使用多项式插值方法以一种方式对函数进行插值,然后进行反向插值以找到反函数。

以下是 MATLAB 中的一些示例代码:

function [a] = Coef(x, y)
    n = length(x);

    a = y;
    for j = 2:n
        for i = n:-1:j
            a(i) = (a(i) - a(i-1)) / (x(i) - x(i-j+1));
        end
    end
end

function [val] = Eval(x, a, t)
    n = length(x);

    val = a(n);
    for i = n-1:-1:1
       val = a(i) + val*(t-x(i)); 
    end
end

它构建一个除差表并根据牛顿插值计算函数。

然后,如果您的点集是 x 和 y(作为相同长度的向量,其中 x(i) 与 y(i) 匹配,则值 n 处的前向插值函数将是 Eval(x, Coef(x , y), n) ,反向插值函数将是 Eval(y, Coef(y, x), n)

具体取决于您的语言,可能有更简洁的方法。这样做,但这与数学有关。

这是我的数值方法课程中使用的教科书的摘录:Google 图书链接

You could use polynomial interpolation methods to interpolate a function one way, then do reverse interpolation to find the inverse function.

Here is some example code in MATLAB:

function [a] = Coef(x, y)
    n = length(x);

    a = y;
    for j = 2:n
        for i = n:-1:j
            a(i) = (a(i) - a(i-1)) / (x(i) - x(i-j+1));
        end
    end
end

function [val] = Eval(x, a, t)
    n = length(x);

    val = a(n);
    for i = n-1:-1:1
       val = a(i) + val*(t-x(i)); 
    end
end

It builds a Divided Difference table and evaluates a function based on Newtons Interpolation.

Then if your sets of points are x, and y (as vectors of the same length, where x(i) matches to y(i), your forward interpolation function at value n would be Eval(x, Coef(x, y), n) and the reverse interpolation function would be Eval(y, Coef(y, x), n).

Depending on your language, there are probably much cleaner ways to do this, but this gets down and dirty with the maths.

Here is an excerpt from the Text Book which is used in my Numerical Methods class: Google Book Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文