Имею код:
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>
#include "methods.h"
#include "functions.h"
#include <boost/multiprecision/cpp_bin_float.hpp>
using namespace std;
namespace mp = boost::multiprecision;
using int128_t = boost::multiprecision::int128_t;
typedef int128_t matrix[2][2];
void mul(matrix a, matrix b, matrix c, int128_t mod)
{
int n = 2;
matrix d;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
int128_t s = 0;
for (int k = 0; k < n; ++k)
s = (s + a[i][k] * b[k][j]) % mod;
((c == a || c == b) ? d[i][j] : c[i][j]) = s;
}
if (c == a || c == b)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
c[i][j] = d[i][j];
}
void power(matrix i, int128_t p, matrix&r, int128_t mod)
{
int n = 2;
if (p % 2)
{
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
r[j][k] = i[j][k];
}
else
{
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
r[j][k] = (j == k) ? 1 : 0;
}
while (p /= 2)
{
mul(i, i, i, mod);
if (p % 2) mul(i, r, r, mod);
}
}
vector<int128_t> Schirokauer_Maps(int128_t a, int128_t b, int128_t h, int128_t q)
{
int128_t q1 = pow(q, 2);
vector<int128_t> s(2);
matrix ax, r;
ax[0][0] = a;
ax[0][1] = h;
ax[1][0] = b;
ax[1][1] = a - b;
power(ax, q - 1, r, q1);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
if (r[i][j] < 0)
r[i][j] += q1;
s[0] = (r[0][0] - 1) / q;
s[1] = r[1][0] / q;
return s;
}
int64_t number(uint64_t h, uint64_t g, uint64_t p, uint64_t m1) //p-модуль, g-основание, h-ответ, d-степень для взятия корня
{
int d = 2;
vector<int> prime{ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019 };
int128_t q = p, ts = p;
int128_t qs = (p - 1) / 2;
mp::cpp_bin_float_quad N = 0;
int i = 0;
int128_t quantity_prime = 0;
int128_t t = 0;
int128_t w = 0; // счетчик коэффицентов
//int128_t m = mp::pow(p, 1.0 / d);
int128_t m = mp::sqrt(p);
vector<int128_t> equation(d + 1);
//построение уравнения
for (i = d; i > 0; i--)
{
do {
q = q - mp::pow(m, int(i));
w++;
} while (q - pow(m, int(i)) > 0);
equation[int(i)] = w;
w = 0;
}
equation[0] = q;
//выделение памяти под базисы
int128_t b = 8;
do
{
i++;
t = prime[int(i)];
} while (t < b);
vector <pair<int128_t, int128_t> > rational_factor;
vector <pair<int128_t, int128_t> > algebraic_factor;
i = 0;
t = 0;
//расчет рационального базиса:
q = prime[0];
ts = prime[0];
do {
do
{
rational_factor.push_back(make_pair(m%q, q));
i++;
q = prime[int(i)];
} while (b > q);
//расчет алгебраического базиса:
w = 0; //для расчета уравнения
int128_t y = 0;
do
{
for (int128_t iw = 0; iw < ts; iw++)
{
for (int j = 0; j < equation.size(); j++)
w += equation[j] * pow(iw, j);
if (w%ts == 0)
{
algebraic_factor.push_back(make_pair(iw, ts));
y++;
}
w = 0;
}
t++;
ts = prime[int(t)];
} while (b > ts);
if ((i == prime.size() - 1) || t == prime.size() - 1)
return -1;
} while (algebraic_factor.size() != rational_factor.size());
b = 2 * algebraic_factor.size() + 3;
//построение матрицы
int128_t ij = 1;
vector<vector<int128_t>> mas(b.convert_to<size_t>(), vector<int128_t>(b.convert_to<size_t>(), 0));
pair<int128_t, int128_t> ab;
int o = 0;
vector <pair<int128_t, int128_t> > pairs(m.convert_to<size_t>());
bool a = true;
srand(time(0)); // автоматическая рандомизация
int128_t d0 = 0, d1 = 0, d2 = 0, d3 = 0;
do {
//генерация a и b проверка на нод и вхождения
do {
ab.first = rand() % (2 * m1) + 1; // диапазон равен от 1 до 3 включительно
ab.second = rand() % (2 * m1 + 1) + (-m1);
int128_t i = 0;
if (NOD(ab.first, ab.second) != 1)
a = false;
else if (find(pairs.begin(), pairs.end(), ab) == pairs.end())
{
a = true;
pairs[o] = ab;
o++;
}
else a = false;
if (a == true)
{
for (int i = 0; i < rational_factor.size(); i++)
{
d0 = -rational_factor[i].first*ab.second%rational_factor[i].second;
d3 = ab.first%rational_factor[i].second;
if (d0 < 0)
d0 += rational_factor[i].second;
if (d3 < 0)
d3 += rational_factor[i].second;
if (d3 == d0)
d1++;
}
for (int i = 0; i < algebraic_factor.size(); i++)
{
d0 = -algebraic_factor[i].first*ab.second%algebraic_factor[i].second;
d3 = ab.first%algebraic_factor[i].second;
if (d0 < 0)
d0 += algebraic_factor[i].second;
if (d3 < 0)
d3 += algebraic_factor[i].second;
if (d3 == d0)
d2++;
}
if ((d1 == 0) || (d2 == 0))
{
pairs[o].first = 0;
pairs[o].second = 0;
o--;
}
}
} while ((a == false) || (d1 == 0) || (d2 == 0));
//процесс заполнения матрицы
int128_t summa = 0;
int128_t yup = 0;
summa = ab.first + ab.second*m;
if (summa == 0)
yup = rational_factor.size();
if (summa < 0)
{
mas[0][ij.convert_to<size_t>()] = 1;
summa *= -1;
}
while (yup != rational_factor.size())
{
if (summa % rational_factor[yup.convert_to<size_t>()].second == 0)
{
summa /= rational_factor[yup.convert_to<size_t>()].second;
mas[(yup + 1).convert_to<size_t>()][ij.convert_to<size_t>()]++;
}
else
yup++;
}
int128_t i = 0;
//yup++;
mp::cpp_bin_float_quad as = (mp::cpp_bin_float_quad) - ab.first / ab.second.convert_to<mp::cpp_bin_float_quad>();
for (int j = 0; j <= 2; j++)
N += equation[j].convert_to<mp::cpp_bin_float_quad>() * pow(as, j);
N = N * mp::pow(-ab.second, d).convert_to<mp::cpp_bin_float_quad>();
if (N == 0 || summa == 0)
i = algebraic_factor.size();
while (i < algebraic_factor.size())
{
d0 = -algebraic_factor[int(i)].first*ab.second%algebraic_factor[int(i)].second;
d3 = ab.first%algebraic_factor[int(i)].second;
if (d0 < 0)
d0 += algebraic_factor[int(i)].second;
if (d3 < 0)
d3 += algebraic_factor[int(i)].second;
if (d3 == d0)
do {
if ((int128_t)N % algebraic_factor[int(i)].second == 0)
{
N /= algebraic_factor[int(i)].second.convert_to<mp::cpp_bin_float_quad>();
mas[(yup + 1).convert_to<size_t>()][ij.convert_to<size_t>()]++;
}
} while ((int128_t)N % algebraic_factor[int(i)].second == 0);
i++;
yup++;
}
if (N == 1 && summa == 1)
{
vector<int128_t> mapk = Schirokauer_Maps(ab.first, ab.second, -equation[0] * ab.second, qs);
int128_t y = 0;
for (int u = algebraic_factor.size() * 2 + 1; u < b; u++, y++)
mas[u][ij.convert_to<size_t>()] = mapk[y.convert_to<size_t>()];
ij++;
}
else
{
pairs[o].first = 0;
pairs[o].second = 0;
o--;
for (int k = 0; k<b; k++)
mas[k][ij.convert_to<size_t>()] = 0;
}
N = 0;
} while (ij != b);
//добавляем 1 строку в начало и ответ системы
vector<int128_t> rezult(b.convert_to<size_t>());
int128_t G = 0;
int128_t H = 0;
int128_t S = 0;
int128_t R = 0;
int128_t yup = 0;
do {
S = rand() % qs + 10;
G = int128_t(powmod(g, S, p));
while (yup != rational_factor.size())
{
if (G % rational_factor[yup.convert_to<size_t>()].second == 0)
{
G /= rational_factor[yup.convert_to<size_t>()].second;
mas[(yup + 1).convert_to<size_t>()][0]++;
}
else
yup++;
}
if (G != 1)
{
for (int i = 0; i<b; i++)
mas[i][0] = 0;
}
yup = 0;
} while (G != 1);
yup = 0;
do {
R = rand() % qs + 10;
H = int128_t(powmod(g, R, p)*h) % p;
while (yup != rational_factor.size())
{
if (H % rational_factor[yup.convert_to<size_t>()].second == 0)
{
H /= rational_factor[yup.convert_to<size_t>()].second;
rezult[(yup + 1).convert_to<size_t>()]++;
}
else
yup++;
}
yup = 0;
if (H != 1)
{
for (int i = 0; i<b; i++)
rezult[i] = 0;
}
} while (H != 1);
for (int i = 0; i < b; i++)
{
if (rezult[i] != 0)
rezult[i] = -rezult[i] + qs;
}
auto resh = gauss(mas, rezult, b, qs);
int128_t k = 0;
int128_t k1 = 0;
int64_t x = 0;
/*do {
if (k == resh.size())
return -1;
x = (-resh[k][0] * S - R) % qs;
if (x < 0)
x += qs;
k++;
} while (powmod(g, x, p) != h);*/
k1 = (-resh[k.convert_to<size_t>()][0] * S - R) % qs;
x = k1.convert_to<int64_t>();
if (x < 0)
x += uint64_t(qs);
return x;
}
Когда собираю ошибок нет, но как только начинаеться запуск выдает ошибку:
Необработанное исключение по адресу 0x00007FFDB2D4879B (ntdll.dll) в all_metods.exe: 0xC0000374: Куча была повреждена (параметры: 0x00007FFDB2DAC6E0).
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Когда мое приложение стартует, RegisterClassEx иногда возвращает 0, и приложение приходится аварийно завершатьЭто происходит не по тому ли что иногда...
Нужно динамически с помощью переменных и регулярного выражения поменять последнее значение в строкеВ первом случае работает во втором нет