Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- namespace Отнять_и_поделить__по_модулю_
- {
- class Program
- {
- static void Main(string[] args)
- {
- var timer = System.Diagnostics.Stopwatch.StartNew();
- Console.WriteLine("Результат ModExp: {0}", ModExp(7, 9999,1000));//1 задание
- timer.Stop();
- Console.WriteLine("Время выполнения алгоритма: {0}", timer.Elapsed);
- timer.Reset();
- timer.Start();
- Console.WriteLine("Результат MinNum: {0}", MinNum(60)); ;//2 задание
- timer.Stop();
- Console.WriteLine("Время выполнения алгоритма: {0}", timer.Elapsed);
- Console.ReadKey();
- }
- static ulong ModExp(ulong x, ulong y, ulong N)
- {
- if (y == 0)
- return 1 ;
- ulong z = ModExp(x, y / 2, N);
- if (y % 2 == 0)
- return z * z % N;
- else
- return x * z * z % N;
- }
- static BigInteger ModExp(BigInteger x, ulong y, BigInteger N)
- {
- if (y == 0)
- return 1;
- BigInteger z = ModExp(x, y / 2, N);
- if (y % 2 == 0)
- return z * z % N;
- else
- return x * z * z % N;
- }
- static BigInteger MinNum(ulong n)
- {
- const int step = 3 * 4 * 5;
- BigInteger number = new BigInteger(Math.Pow(10,n-1) + (step - ModExp(10, n - 1, step)));
- BigInteger thirty = new BigInteger(Math.Pow(10, 30));
- return ModExp(number, 1, thirty);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement