取模运算的性质

你不可错过的快速幂算法模板超详细整理 快速幂算法1.介绍2.模板3.优化4.终极优化5.例题 1.介绍 快速幂算法能帮我们算出指数非常大的幂,传统的求幂算法之所以时间复杂度非常高(为O(指数n)),就是因为当指数n非常大的时候,需要执行的循环操作次数也非常大。所以我们快速幂算法的核心思想就是每一步都把指数分成两半,而相应的底数做平方运算。这样不仅能把非常大的指数给不断变小,所需要执行的循环次数也变小,而最后表示的结果却一直不会变。 2.模板 ////快速幂算模板 long long fastPower(long base, long long 阅读详情
AI摘要

本文详细介绍了模运算的基本概念,包括取模、模加法、模减法及模乘法,并阐述了同余式的定义及其性质。同时,文中还列举了模运算的基本规则和重要定理。

定义

给定一个正整数p,任意一个整数n,一定存在等式 :
n = kp + r ;
其中 k、r 是整数,且 0 ≤ r < p,则称 k 为 n 除以 p 的商,r 为 n 除以 p 的余数。
对于正整数 p 和整数 a,b,定义如下运算:
取模运算:a % p(或a mod p),表示a除以p的余数。
模p加法: ,其结果是a+b算术和除以p的余数。
模p减法: ,其结果是a-b算术差除以p的余数。
模p乘法: ,其结果是 a * b算术乘法除以p的余数。
说明:
1. 同余式:正整数a,b对p取模,它们的余数相同,记做 或者a ≡ b (mod p)。
2. n % p 得到结果的正负由被除数n决定,与p无关。例如:7%4 = 3, -7%4 = -3, 7%-4 = 3, -7%-4 = -3

基本性质

  1. 若p|(a-b),则a≡b (% p)。例如 11 ≡ 4 (% 7), 18 ≡ 4(% 7)
  2. (a % p)=(b % p)意味a≡b (% p)
  3. 对称性:a≡b (% p)等价于b≡a (% p)
  4. 传递性:若a≡b (% p)且b≡c (% p) ,则a≡c (% p)

运算规则

模运算与基本四则运算有些相似,但是除法例外。其规则如下:
  1. (a + b) % p = (a % p + b % p) % p (1)
  2. (a - b) % p = (a % p - b % p) % p (2)
  3. (a * b) % p = (a % p * b % p) % p (3)
  4. a ^ b % p = ((a % p)^b) % p (4)
  • 结合律:
    ((a+b) % p + c) % p = (a + (b+c) % p) % p (5)
((a*b) % p * c)% p = (a * (b*c) % p) % p (6)
  • 交换律:
    (a + b) % p = (b+a) % p (7)
(a * b) % p = (b * a) % p (8)
  • 分配律:
    ((a +b)% p * c) % p = ((a * c) % p + (b * c) % p) % p (9)

重要定理

  • 若a≡b (% p),则对于任意的c,都有(a + c) ≡ (b + c) (%p);(10)
  • 若a≡b (% p),则对于任意的c,都有(a * c) ≡ (b * c) (%p);(11)
  • 若a≡b (% p),c≡d (% p),则 (a + c) ≡ (b + d) (%p),(a - c) ≡ (b - d) (%p),
    (a * c) ≡ (b * d) (%p),(a / c) ≡ (b / d) (%p); (12)
快速幂第一章(C++递归版本) C++快速幂第一章(递归版本) 阅读详情

相关推荐

模运算(密码学/数论/算法)

模运算的概念模运算是一种算术运算,常写作a mod n,表示整数a除以正整数n后的余数。模数是模运算中的除数n,它决定了结果的范围。公式表达:对于任意整数a和正整数n,可以将a表示为:a = qn + r,其中0 ≤ r < n,q是整数商,即q = ⌊a/n⌋。a除以n的余数是a mod n。示例:11 mod 7 = 4(11除以7的余数是4)** -11 mod 7 = 3(-11除以7的余数是3)**

weixin_45556264的博客 1232

快速幂模板

快速幂 #include<bits/stdc++.h> using namespace std; int Quick_pow(int a,int b) { int ans=1,base=a;// ans:结果;base:底数 while(b) { if(b & 1)//等于b%2==1 { ans=ans*base; } base=base*base; b =

算法小猪的博客 2502

ArcGis布局视图中Excel表格无边框插入与图例符号边框定制技巧

本文详细解析了在ArcGis布局视图中实现Excel表格无边框插入与图例符号边框定制的专业技巧。通过源头净化Excel表格、精准粘贴以及分解图例并单独设置边框属性,有效解决了地图报告中常见的视觉粗糙问题,帮助用户提升专题图与报告的精致度与专业感。

weixin_29292535的博客 308

(数论专题)【 快速幂 模板详解 】

(数论专题)【 快速幂 模板详解 】 快速幂 %mod怎么快速算出来? 举个例子: 怎么快速算出来呢? 162的二进制为10100010,所以162 = ++ = 128+32+2, 这样 = * * 这样就可以得到下面的代码了; a维护的是 , 上面1,2,4,8,16,就是2的多少次方。。n&1==1的时候表示答案需要乘以这个ai 代码: ll qpow( l...

石旭先森的博客 999

RK3568—基于GM8775C的MIPI转双通道LVDS屏幕调试

Rockchip RK3568 原生显示接口不支持双通道LVDS屏幕的数据输出,因此需要借助显示转换芯片才能实现双通道LVDS屏幕的驱动。本文介绍使用GM8775C芯片方案,在 RK3568 平台实现单路 MIPI DSI 信号输出来驱动双通道LVDS显示屏。 关于GM8775C GM8775C 型 DSI 转双通道 LVDS 发送器产品主要实现将 MIPI DSI 转单/双通道 LVDS功能,MIPI 支持 1/2/3/4 通道可选,每通道最高支持 1Gbps 速率,最大支持 4Gbps 速率。LV.

Industio_CSDN的博客 1万+

2024从零开始使用VSCode编程(静态页面制作篇)HTML+CSS+JS阶段

VSCode第一个前端静态页面的编写

ssrsrrn的博客 9604

取模运算性质_取模运算的性质

对于整型数a,b来说,取模运算或者求余运算的方法都是:1.求 整数商: c = a/b;2.计算模或者余数: r = a - c*b.求模运算和求余运算在第一步不同: 取余运算在取c的值时,向0 方向舍入(fix()函数);而取模运算在计算c的值时,向负无穷方向舍入(floor()函数)。例如:计算-7 Mod 4那么:a = -7;b = 4;第一步:求整数商c,如进行求模运算c = -2(向负...

weixin_29955355的博客 2619

openclaw + ollama本地模型 + 飞书平台 windows平台部署教程

本教程将指导您在 Windows 10/11 环境下,通过 Ollama 运行本地模型,并结合 OpenClaw 接入飞书,搭建一个完全本地化的 AI 助手。

专注于技术研发行业技术主要有AI人工智能视觉识别、物联网技术、电商平台、app原生技术、微信小程序、鸿蒙语言开发、办公企业oa、ERP等,致力于做东半球最优秀的程序员 2188

取模运算性质_模运算的基本性质

基本理论基本概念给定一个正整数p,任意一个整数n,一定存在等式n=kp+r;其中k、r是整数,且0≤r对于正整数p和整数a,b,定义如下运算:取模运算:a%p(或amodp),表示a除以p的余数。模p加法:(a+b)%p,其结果是a+b算术和除以p的余数,也就是说,(a+b)=kp+r,则(a+b)%p=r。模p减法:(a-b)%p,其...

weixin_39886238的博客 350

从一个过气考渣的角度看LLM智能化测试用例生成

根据业务需求生成测试用例,会涉及到相关上下文的召回、用例生成等场景。除了用例生成之外,也可以考虑应用LLM进行用例评审。因此,本文将涉及以下内容。

software_test010的博客 833

2025爬虫实战:某音直播间实时弹幕获取原理及源码分析

某音直播弹幕虽然没有直接挂在 DOM 上,但由于用的是西瓜播放器,弹幕是通过 JS 接口动态渲染的,这就给了我们 Hook 的机会。关键点是找到调用的位置,然后拦截并输出对象里的message字段。这个思路其实也适用于其他前端渲染的播放器,比如 B 站的 DPlayer、HLS.js、甚至很多自研播放器都有类似结构。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1831

取模运算性质_取模运算的性质、扩展欧几里德定理

取模运算对于整型数a,b来说,取模运算或者求余运算的方法都是:1.求 整数商: c = a/b;2.计算模或者余数: r = a - c*b.求模运算和求余运算在第一步不同: 取余运算在取c的值时,向0 方向舍入(fix()函数);而取模运算在计算c的值时,向负无穷方向舍入(floor()函数)。例如:计算-7 Mod 4那么:a = -7;b = 4;第一步:求整数商c,如进行求模运算c = -...

weixin_30794381的博客 902

手把手教你学PCIE(2.1.23)--PCIe基本概念:链路速率协商

链路速率协商是一个多步骤的过程,确保发送端和接收端之间的链路速率设置一致。通过初始化链路速率寄存器、发送和接收 TS2 序列、检测链路对端的存在、交换链路速率信息、确定最大链路速率和配置链路速率,可以确保 PCIe 设备之间的数据传输速度最大化,从而提高数据传输效率。初始化链路速率寄存器:设置支持的最大链路速率。发送和接收 TS2 序列:检测链路对端的存在。交换链路速率信息:发送和支持的最大链路速率。确定最大链路速率:选择最小值作为最终链路速率。配置链路速率:更新链路速率寄存器。

MHD0815的博客 1820

取模运算性质_【转】数学与编程——求余、取模运算及其性质

一、求余运算(Remainder)Euclidean division:Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|, where |b| denotes the absolute value of b.(术语:...

weixin_35051143的博客 571

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

取模运算性质_数学与编程——求余、取模运算及其性质 | 学步园

一、求余运算(Remainder)http://zh.wikipedia.org/wiki/同余)Euclidean division:Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|, where |b| denot...

weixin_39828956的博客 200

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

数学与编程——求余、取模运算及其性质

一、求余运算(Remainder) (参考维基百科: http://zh.wikipedia.org/wiki/余数  http://en.wikipedia.org/wiki/Remainder http://en.wikipedia.org/wiki/Euclidean_division http://zh.wikipedia.org/wiki/同余) Euclidean

chensidney‘s blog 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

取模运算的几条性质

取模运算的几条性质

qq_40659901的博客 584

取模运算性质

定义 给定一个正整数p,任意一个整数n,一定存在等式 : n = kp + r ; 其中 k、r 是整数,且 0 ≤ r &lt; p,则称 k 为 n 除以 p 的商,r 为 n 除以 p 的余数。 对于正整数 p 和整数 a,b,定义如下运算: 取模运算:a % p(或a mod p),表示a除以p的余数。 模p加法: ,其结果是a+b算术和除以p的余数。 模p减法: ,其结果是...

SPIDER的博客 616

取模运算的性质、扩展欧几里德定理

取模运算 对于整型数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商: c = a/b; 2.计算模或者余数: r = a - c*b. 求模运算和求余运算在第一步不同: 取余运算在取c的值时,向0 方向舍入(fix()函数);而取模运算在计算c的值时,向负无穷方向舍入(floor()函数)。 例如:计算-7 Mod 4 那么:a = -7;b = 4; ...

afvdxhq267883的博客 500

取模运算性质_求余、取模运算在RTOS中计算优先级的理解

uCOS3中的部分源码:/* 置位优先级表中相应的位 */void OS_PrioInsert (OS_PRIO prio){CPU_DATA bit;CPU_DATA bit_nbr;OS_PRIO ix;/* 求模操作,获取优先级表数组的下标索引 */ix = prio / DEF_INT_CPU_NBR_BITS;//32bits//由于数据均为无符号数,...

weixin_39562579的博客 713

取模运算性质_关于取模运算 和 求逆元

先分享2个式子当模式左边有除法:今天了解了2个,感觉这2个很棒~,尤其第一个:1、$\dfrac {a} {b}\% m=\dfrac {a \%\left( b\cdot m\right) } {b}$ 要求:a能整除b。(不知道用了什么奇技淫巧。。。)2、$\dfrac {a} {b}\% m=(a\cdot b^{m-2} )\%m$ 要求:gcd(b , m)== 1 且 m...

weixin_39821605的博客 585
下一篇: 求一个整数的所有因子
varinic
博客等级 码龄11年 23粉丝 78原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值