C# 使用同余代换简化多个大数相乘取模运算_密码学

•(A + B) mod M = ( A mod M + B mod M ) mod M
•(A * B) mod M = ((A mod M) *( B mod M)) mod M

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 大数相乘取模
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            string strmod = textBox1.Text, strn = textBox2.Text;
            int mod = Convert.ToInt32(strmod), n = Convert.ToInt32(strn);
            string text = textBox3.Text;
            //MessageBox.Show(text);
            int ans = 1;
            int t = 0, cnt = 0;
            for(int i = 0; i < text.Length; i++)
            {
                if (text[i]>='0' && text[i] <= '9')
                {
                    t = t * 10 + text[i] - '0';
                }
                else
                {
                    if(text[i-1]>='0' && text[i-1]<='9')
                    {
                        ans = ans * t % mod;
                        cnt++;
                        if (cnt == n) break;
                    }
                    //MessageBox.Show(Convert.ToString(t));
                    t = 0;
                }
            }
            //MessageBox.Show(Convert.ToString(ans));
            textBox4.Text = Convert.ToString(ans);
        }
    }
}