[BZOJ2242]SDOI2011计算器|快速幂|扩展欧几里得|BSGS

乘法逆元(扩展欧几里得算法) 加密解密的基础,扩展欧几里得算法(辗转相除法) 立即下载

    第一问快速幂,第二问扩欧,第三问BSBG,注意特判。。

BSGS戳这里

#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
map<int,int> mp;
int T,i,k;
ll y,z,p,ans;
ll pow(ll a,ll t,ll p)
{
	if (t==0) return 1ll;
	ll b=pow(a,t/2,p);
	b=(b*b)%p;
	if (t%2) b=(b*a)%p;
	return b;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
	if (b==0) {x=1;y=0;return a;}
	ll x1,y1,d=exgcd(b,a%b,x1,y1);
	x=y1;y=x1-(a/b)*y1;
	return d;
}
void solve2(ll y,ll z,ll p)
{
	ll x,c,d=exgcd(y,p,x,c);
	if (z%d) {printf("Orz, I cannot find x!\n");return;}
	y/=d;z/=d;p/=d;
	x=(x*z)%p;
	if (x<0) x=(x+(-x/p+1)*p)%p;
	printf("%lld\n",x);
}
ll bsgs(ll a,ll b,ll p) //a^x≡b(mod p)
{
	a%=p;
	if (b==1) return 0;
	if (!a&&!b)return 1;
	if (!a) return -1;
	int m=ceil(sqrt(p*1.0)),i;
	ll w=1;
	mp.clear();
	mp[1]=2*m;
	for (i=1;i<m;i++) {w=(w*a)%p;if (!mp[w]) mp[w]=i;}
	ll t=pow(a,p-m-1,p);w=1;
	for (i=0;i<m;i++)	
	{
		int j=mp[(w*b)%p];
		if (j) return (ll)i*m+j%m;
		else w=(w*t)%p;
	}
	return -1;
}
int main()
{
	freopen("2242.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d%d",&T,&k);
	while (T--)
	{
		scanf("%lld%lld%lld",&y,&z,&p);
		switch(k)
		{
			case(1):printf("%lld\n",pow(y,z,p));break;
			case(2):solve2(y,z,p);break;
			case(3):
				ans=bsgs(y,z,p);
				if (ans==-1) printf("Orz, I cannot find x!\n");else printf("%lld\n",ans);
				break;
		}
	}
}


欧几里得计算器/辗转相除法计算器(带过程) 一个小小的exe文件,其中是我在学习网络安全数学期间编写的欧几里得除法/辗转相除法计算器。 也就是根据两个互素的整数a和b,通过辗转相除法算出两个整数s,t,使得sa+tb=1。 立即下载

相关推荐

全面分析总结解决Could not transfer artifact org.apache.maven.plugins等此类问题的方法

全面分析总结解决Could not transfer artifact org.apache.maven.plugins,Could not transfer artifact xxx from/to xxx等此类问题的方法

ccjjjjdff的博客 1万+

计时器 【扩展欧几里得+BSGS】

题目:   https://www.luogu.org/problemnew/show/P2485 题目简介:   你被要求设计一个计算器完成以下三项任务:   1、给定y、z、p,计算y^z mod p 的值;   2、给定y、z、p,计算满足xy ≡z(mod p)的最小非负整数x;   3、给定y、z、p,计算满足y^x ≡z(mod p)的最小非负整数x。   为了...

baicang2022的博客 254

保姆级教程:用TI IWR6843雷达板跑通3D人体追踪demo(附CCS/UniFlash避坑指南)

本文提供了一份详尽的TI IWR6843毫米波雷达开发板3D人体追踪demo实现指南,涵盖开发环境搭建、工程导入编译、固件烧录及可视化工具配置全流程。特别针对CCS和UniFlash使用中的常见问题给出解决方案,帮助开发者快速掌握3D People Tracking技术应用。

weixin_29159441的博客 503

牛客——[SDOI2011]计算器(快速幂+扩展欧几里得+BSGS)

链接:https://ac.nowcoder.com/acm/problem/20347 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满...

lgz0921的博客 988

INCA基础教程

以软件自带Demo为例记录INCA学习过程,主要内容包括测量与标定。

yunqianrui的博客 6万+

手把手教你用C语言实现GNSS卫星方位角计算(附RTKLIB风格代码解析)

本文详细介绍了如何使用C语言实现GNSS卫星方位角计算,包括从地固坐标系到站心坐标系的转换链条。通过RTKLIB风格的代码解析,展示了XYZ到ENU坐标转换的核心原理和C语言实现框架,帮助开发者掌握卫星方位角和高度角的计算技巧,适用于GNSS观测数据质量分析和精密定位算法开发。

weixin_30699915的博客 365

【ybt金牌导航8-6-10】计算器(exgcd)(扩展BSGS)

给你 y,z,p 三个数,要你实现三种操作中的其中一个。 1. 求 y^z mod p 2. 求最小非负数 x 使得 x*y≡z(mod p) 3. 求最小非负数 x 使得 y^x≡z(mod p)

欢迎您的光顾awa 587

如何在 Linux 上安装 RTX 5090 / 5080 /5070 Ti / 5070 驱动程序 — 详细指南

Linux 上的 NVIDIA 5090 是 AI、游戏和渲染的强大引擎,但需要耐心。如果稳定性是首要考虑因素,请坚持使用开源驱动程序,并关注 NVIDIA 论坛以获取未来更新。

iCloudEnd的博客 6912

[SDOI2011]计算器(逆元/BSGS)

题目链接 一个很简单的题,套一下就好了。操作2的解利用exgcd形式可以得到当z与gcd(y,p)不整除时无解。否则解就是y的逆元的倍数emmmm #include<bits/stdc++.h> #include <map> #include <cmath> #include <cstdio> #include <iostream> #include <unordered_map> #define ll long long usi

weixin_45948940的博客 1352

RT-DETR算法优化改进:Backbone改进 | HGBlock完美结合PPHGNetV2 GhostConv

GhostConv助力RT-DETR ,HGBlock与PPHGNetV2 GhostConv完美结合

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 2909

机器人学(三):手眼标定

主要讲了eye-to-hand的手眼标定求解算法,包括AX=XB和AX=YB两种情况,包含SVD法、Tsai-Lenz法、最小二乘法等,从原理到解法再到算法代码实现,包括matlab代码、C++代码、库的使用等,简单明了,容易在实际项目中进行应用

行者无疆的博客 2554

【bzoj2242】[SDOI2011]计算器 数论相关(快速幂+扩展欧几里得+BSGS)

2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2529 Solved: 1003 [Submit][Status][Discuss] Description 你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满足xy≡ Z (

ATOD 1710

Node-RED教程(二):Node-RED的面板的操作

Node-RED系列(二):Node-RED的面板的操作 要使用Node-RED去搭建一个物联网平台,链接设备,设备通信,就必须要先熟悉Node-RED的这种面板,对于一个新的项目,在动手做之前一点想对这个的技术的文档有足够的了解做起来才能游刃有余。本期我给大家介绍一下Node-RED的各种面板,熟悉这些面板后,在后续的搭建Iot服务中,会举重若轻,同时该系列的文档,难度会逐步加深,知道了本章解释的名称后对后续文章的理解有很大帮助。 面板1:默认节点选项列表 安装Node-RED后默认的节点分为这5大类

知无涯 1万+

bzoj 2242 [SDOI2011]计算器 快速幂+扩展欧几里得+BSGS

1:快速幂  2:exgcd  3:exbsgs,题里说是素数,但我打的普通bsgs就wa,exbsgs就A了...... (map就是慢)..... #include #include #include #include #include #include #define LL long long using namespace std; map pp; map bo; LL a,b,c;

Ren_Ivan的博客 601

深度学习工具包 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")

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

BZOJ 2242 SDOI2011 计算器 快速幂+扩展欧几里得+BSGS

题目大意:……简洁明了自己看 第一问快速幂 第二问扩展欧几里得 第三问BSGS 顺便一开始没看到p是质数0.0 去弄了EXBSGS的模板0.0 懒得改了 #include #include #include #include #include #define M 1001001 using namespace std; typedef long long ll; typedef pa

世界 1990

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS

【bzoj2242】: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS 1 /* http://www.cnblogs.com/karl07/ */ 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #includ...

weixin_30299539的博客 119

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

【SDOI2011】【BZOJ】【P2242】【计算器】【题解】【快速幂+扩展欧几里得+高次同余方程/BSGS】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2242 文献:http://blog.youkuaiyun.com/nike0good/article/details/9171173 http://blog.youkuaiyun.com/acm_cxlove/article/details/7831793 http://blog.youkuaiyun.com/nike0

退役狗的专栏 2457

【数论】【快速幂】【扩展欧几里得】【BSGS算法】bzoj2242 [SDOI2011]计算器

说是BSGS……但是跟前面那题的扩展BSGS其实是一样的……因为模数虽然是质数,但是其可能可以整除a!!所以这两者其实是一样的…… 第一二种操作不赘述。 #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std;...

weixin_34249678的博客 152

bzoj 2242: [SDOI2011]计算器【扩展欧几里得+快速幂+BSGS】

第一问快速幂板子 第二问把式子转化为\( xy\equiv Z(modP)\rightarrow xy+bP=z \),然后扩展欧几里得 第三问BSGS板子 #include<iostream> #include<cstdio> #include<map> #include<cmath> using namespace std; long long...

weixin_30951389的博客 144

BZOJ_P2242&Codevs_P1565[SDOI2011]计算器(快速幂+扩展欧几里得+BSGS)

BZOJ传送门 Codevs传送门 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2302 Solved: 910 [Submit][Status][Discuss] Description 你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满足xy≡ Z ( mo

BeiYu 572

[bzoj2242][SDOI2011]计算器

2242: [SDOI2011]计算器Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2032 Solved: 791 [Submit][Status][Discuss] Description你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满足xy≡ Z ( mod

Vampire 1868

【BZOJ2242】[SDOI2011]计算器 BSGS

【BZOJ2242】[SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数; 3、给定y,z,p,计算满足Y^x ≡Z ( mod P)的最小非负整数。 Input 输入包含多组数据。 第一行包...

aodanchui1057的博客 166

[bzoj2242] [SDOI2011]计算器

#570. [bzoj2242] [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务:1、给定y,z,p,计算Y^Z Mod P 的值;2、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数;3、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。 ...

weixin_30668887的博客 107

BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS

BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1、给定y,z,p,计算Y^Z Mod P 的值; 2、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数; 3、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。 分析: 各种板子题 代码...

164

bzoj2242 : [SDOI2011]计算器 [BSGS算法]

第一次写一个比较高大上的数论题吧~~~虽然要花好长时间才明白一点,但是有收获吧~~对扩展欧几里得加深了理解。还认识了高大上的BSGS算法,哈哈~虽然还是一知半解,脑袋太小理解太慢,不易记住 上别人的题解吧~~~ 相关连接: BSGS算法_Baby steps giant steps算法(无扩展)详解 bzoj2242: [SDOI2011]计算器 && BSGS 算法  

Lin_disguiser的博客 637
上一篇: [BZOJ1293]SCOI2009生日礼物|贪心
下一篇: [BZOJ1069]SCOI2007最大土地面积|凸包|旋转卡壳
Tag_king
博客等级 码龄12年 15粉丝 164原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值