Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Numerics;
- try
- {
- Console.WriteLine("Выберите режим работы программы:");
- Console.WriteLine("1. Ввести данные вручную");
- Console.WriteLine("2. Использовать тестовые данные");
- Console.Write("Ваш выбор: ");
- string mode = Console.ReadLine();
- if (mode == "1")
- {
- Console.Write("a = ");
- int a = int.Parse(Console.ReadLine());
- Console.Write("b = ");
- int b = int.Parse(Console.ReadLine());
- Console.Write("n = ");
- int n = int.Parse(Console.ReadLine());
- var T = new BigInteger(a) * new BigInteger(b);
- int invN = ModInverse(n, b);
- if (invN == -1)
- {
- Console.WriteLine("Обратного значения не существует");
- return;
- }
- var m = T * invN % b;
- var u = (T + m * n) / b;
- Console.WriteLine($"Результат u = {u}");
- Console.ReadLine();
- }
- else if (mode == "2")
- {
- // Заданные значения для проверки
- int[] aValues = { 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925 };
- int[] bValues = { 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925, 27056, 59259, 25925 };
- int[] nValues = { 21249, 22225, 31877, 65060, 40417, 24461, 39501, 8339, 9560, 7209, 38407, 64775, 17018, 51130, 4717, 58183, 33683, 27851, 17354, 19168, 16732, 30270, 23737, 59423, 36199, 9 };
- int[] uExpected = { 37629, 20959, 33281, 62342, 19752, 32000, 56446, 58723, 19095, 64964, 13501, 28287, 36720, 383, 3482, 28050, 50444, 16293, 5802, 61867, 33550, 21881, 55090, 26725, 33952, 1 };
- for (int i = 0; i < aValues.Length; i++)
- {
- int a = aValues[i];
- int b = bValues[i];
- int n = nValues[i];
- int expectedU = uExpected[i];
- var T = new BigInteger(a) * new BigInteger(b);
- int invN = ModInverse(n, b);
- if (invN == -1)
- {
- Console.WriteLine("Обратного значения не существует\n");
- continue;
- }
- var m = T * invN % b;
- var u = (T + m * n) / b;
- Console.WriteLine($"Проверка значений a={a}, b={b}, n={n}:");
- Console.WriteLine($"Рассчитанное u = {u}, ожидаемое u = {expectedU}");
- Console.WriteLine(u == expectedU ? "Результат верный.\n" : "Результат неверный.\n");
- }
- Console.ReadLine();
- }
- else
- {
- Console.WriteLine("Неверный ввод. Выберите 1 или 2.");
- Console.ReadLine();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine($"Error: {e.Message}");
- Console.ReadLine();
- }
- // Функция для нахождения обратного значения по модулю с использованием BigInteger.ModPow
- static int ModInverse(int a, int m)
- {
- a %= m;
- for (int x = 1; x < m; x++)
- {
- if ((a * x) % m == 1)
- {
- return x;
- }
- }
- return -1;
- }
Advertisement
Comments
-
- An updated mod, named Blue WhatsApp Plus APK Download with more emojis, themes and fonts,
- Complete customizing includes everything APK with online free version WhatsApp Plus Blue.
- <a href="https://bluewhatsapp.org/">Blue WhatsApp</a>
Add Comment
Please, Sign In to add comment
Advertisement