poj3641Pseudoprime numbers(快速幂+同余定理)

uniapp+coze制作app智能体 使用 UniApp 配合 Coze 平台来开发一个具备 AI 智能体的 App,是一个高效且实用的选择。其核心思路主要有两种:一种是通过,另一种则是通过实现更自主的集成。下面的流程图可以帮你快速了解这两种方案的全貌。下面,我们来详细看看这两种方案分别如何操作。 阅读详情

poj3641Pseudoprime numbers

Description

Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)

Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.

Input

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.

Output

For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Sample Input

3 2
10 3
341 2
341 3
1105 2
1105 3
0 0

Sample Output

no
no
yes
no
yes
yes
题意:给你两个数p和a,如果a的p次方对p取余等于a并且p不是素数,输出yes,否则输出no。

解题思路:因为P比较大暴力的话肯定不行,这里用到了快速幂的算法和同余定理。

快速幂算法:可以把b按二进制展开为:b = p(n)*2^n  +  p(n-1)*2^(n-1)  +…+   p(1)*2  +  p(0)
其中p(i) (0<=i<=n)为 0 或 1

这样 a^b =  a^ (p(n)*2^n  +  p(n-1)*2^(n-1)  +...+  p(1)*2  +  p(0))
               =  a^(p(n)*2^n)  *  a^(p(n-1)*2^(n-1))  *...*  a^(p(1)*2)  *  a^p(0)
对于p(i)=0的情况, a^(p(i) * 2^(i-1) ) =  a^0  =  1,不用处理
我们要考虑的仅仅是p(i)=1的情况
化简:a^(2^i)  = a^(2^(i-1)  * 2) = (  a^(  p(i)  *  2^(i-1)  )  )^2。

同余定理:a*b%c=((a%c)*b)%c。

#include
  
   
#include
   
    
using namespace std;
int isprime(long n){
	int i;
	for(i=2;i*i
    
     =n){
		return 1;
	}
	else{
		return 0;
	}
}
int quickpow(long long n,long long m,long long mod)
{  
    long long ans=1,base=n;  
    while(m)  
    {  
        if(m&1) 
  
        {  
            ans=(base*ans)%mod; 
        }  
        base=(base*base)%mod;  
        m/=2;
    }  
    return ans;  
}  
int main(){
	long long p,a,mod;
	while(scanf("%lld %lld",&p,&a)&&p||a){
		
		if(isprime(p)==0&&quickpow(a,p,p)==a){
			printf("yes\n");
		}
		else{
			printf("no\n");
		}
	}
} 
    
   
  

【从零开始vnpy量化投资】十一. 实盘接入测试与正式实盘 本篇主要讲解如何使用vnpy进行实盘交易,由于上一节我们已经学习了模拟盘运行,如果一切顺利的话,只需要通过穿透测试,就可以正式部署实盘了。 阅读详情

相关推荐

RDMA技术实战解析:从零搭建高性能网络通信架构

本文是一份详尽的RDMA技术实战指南,旨在帮助开发者从零搭建高性能网络通信架构。文章深入剖析了RDMA(远程直接内存访问)如何颠覆传统通信模式,实现CPU零参与、数据零拷贝的极速传输。内容涵盖硬件选型、RoCEv2无损网络配置、ibverbs API编程示例以及关键性能调优,并通过实测数据展示了其在降低延迟、释放CPU资源方面的巨大优势,是解决AI训练与高性能计算网络瓶颈的利器。

weixin_29207439的博客 194

同余定理 + 快速幂

同余定理:(a*b)%c == ((a%c)*b)%c == ((a%c * b%c)%c   证明(前一种): (a*b - a%c*b )为c的 倍数即可 提取b得到 b(a-a%c) 易知其为 c的  倍数 ,得证           一、一般的幂次取余 (主要利用(a*b)%c == ((a%c)*b)%c)   ll normal_mod(ll a, ll b, ll c){...

Unyielding will 924

OpenWrt虚拟机磁盘扩容实战:从SquashFS到ext4的完整避坑指南

本文详细介绍了OpenWrt虚拟机磁盘扩容的完整流程,重点对比了SquashFS和ext4文件系统在扩容操作上的关键差异。通过五步安全迁移法和直接分区调整方案,帮助用户有效解决磁盘空间不足问题,同时提供高级技巧和故障排除指南,确保扩容过程顺利无坑。

weixin_29251131的博客 241

数数 (数学)(快速幂+同余定理)

数数 【问题描述】 小 Star 还不会数数。有一天他看到了一张奇怪的数表,上面的每一个数各自都由相同数字构成,比如“11111111”“66666” 。于是他想自己从 1 慢慢数到这个数字。多少 Star 有个很不好的习惯,每数到一定个数就会从头开始数起。 现在请你帮忙求出,他最后数出来的数是多少。【输入】 第一行一个整数 Q,表示数表上总共有多少个数; 以下 Q 行每行三个整数 x、n、

CodeOnce的博客 899

Mac 不小心断开移动硬盘导致磁盘无法读取和加载(顺利解决!)

Mac移动硬盘不正确断开连接导致无法读取加载的解决方法,亲测有效

Robin_Pi的博客 1万+

T-Box功能详解及应用

T - BOX,其全称为 Telematics Box,作为远程 / 车载通信模块,是一款集车身网络与无线通讯功能于一体的智能终端设备。它充当了多媒体车机与 TSP(汽车远程服务提供商)以及互联网之间的无线路由器角色,为用户创造了远程访问的可能。用户借助T - BOX能够对车辆配备的各个 ECU(电子控制单元)进行远程操控,包括远程启动或停止发动机开启或关闭空调天窗后备箱门车门,以及实现闪灯鸣笛等功能。

wgl110110的博客 5695

【算法】同余定理及快速幂求模

文章目录定义及其性质大数的高精度对单精度取模快速幂取模(次方求模) 定义及其性质 定以 数论中的重要概念。给定一个正整数m,如果两个整数a和b满足a-b能够被m整除,即m/(a-b)得到一个整数,那么就称整数a与b对模m同余,记作a≡b(mod m)。对模m同余是整数的一个等价关系。 两个整数a、b,若它们除以整数m所得的余数相等,则称a与b对于模m同余或a同余于b模m。 性质 1.反身性:a≡a...

立志如山,行道如水 3588

手把手教你玩转服务器芯片架构​专栏--​​3.5 实战案例:用SystemC搭建一个多核服务器芯片的NoC模型(含流量分配与延迟仿真)​

通过SystemC搭建多核NoC模型,可深入验证流量分配策略与延迟特性。实际工程中需结合FPGA原型验证(如Xilinx Alveo)进行时序收敛分析,并通过参数化设计适配不同服务器芯片需求。

MHD0815的博客 3052

快速幂及同余定理

同余定理: 给定一个正整数m,如果两个整数a和b满足(a-b)能够被m整除,即(a-b)/m得到一个整数,那么就称整数a与b对模m同余,记作a≡b(mod m)。 性质: 1传递性 若a≡b (mod m),b≡c (mod m),则a≡c (mod m) 2同余式线性运算 若a≡b (mod m),c≡d(mod m),则a c≡b d (mod m) 3同

mwhybmj的博客 850

Cadence OrCAD Capture原理图中为功能模块分配ROOM的方法

本文详解介绍原理图设计阶段为原理图模块分配ROOM的方法。

fydar的博客 1351

Qt实现思维导图功能1『核心树』

利用Qt的QGraphicsView框架实现思维导图功能 百度网盘体验地址: 链接:https://pan.baidu.com/s/15x9F48agCDOQbsyH3VwVXA 提取码:8svh 注:UI风格参考博主:前行中的小猪,博文地址:https://blog.csdn.net/GoForwardToStep/article/details/124692790?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_u

浮生流年的博客 6168

快速幂变形(快速幂取模),同余定理

在一些计算中,数字可能过大,因此,我们可以使用同余定理对程序进行优化同余定理,多个数字对同一个数字取余后的计算与计算后再取余同一个数字结果相同,在大量的数学计算规律中都可以实现;快速幂取模:#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; using namespace std; int P(int a...

someone_and_anyone的博客 491

第一节:Vben Admin介绍和初次运行

本文是【实战Vben Admin + Python】的第一节,本文内容主要是介绍Vben Admin框架和我们后端使用的技术,开发环境和工具的准备本节文章的目的是Vben Admin的简单介绍,通过配置可以让项目前端运行起来,由于项目默认开启了mock,展示的都是静态模拟数据,我们在此基础上,下一节将研究分析下Vben Admin的目录。为我们后端搭建准备。

cuiwin的专栏 7435

POJ -3641Pseudoprime numbers(快速幂+同余定理)

Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not v

菜鸟的自我修养 2005

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

IEM模型,地表粗糙度模拟

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

POJ 3641 Pseudoprime numbers (数论+快速幂)

题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder...

G19850917的博客 188

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

poj 3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6645   Accepted: 2697 Description Fermat's theorem states that for any prime number p a

shengweisong 2057

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

POJ 3641 Pseudoprime numbers(快速幂)

嗯... 题目链接:http://poj.org/problem?id=3641 AC代码: 1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 inline bool is_prime(int x){ 7 if(x...

weixin_30408739的博客 334

poj-3641Pseudoprime numbers

poj-3641Pseudoprime numbers Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10836 Accepted: 4686 Description Fermat’s theorem states that for any prim

weixin_40433319的博客 262

POJ 3641 Pseudoprime numbers【素数+快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 3471 Description Fermat's theorem states that for any prime number p a

希望的专栏 529

POJ 3641 Pseudoprime numbers

HPU专题训练(-1)GCD&amp;amp;&amp;amp;素筛&amp;amp;&amp;amp;快速幂_____K - Pseudoprime numbers题目来源http://poj.org/problem?id=3641第一次做的时候用的是int 没有用long long 范围比较小,出错了而且英文题目,还扯了费马小定理,百度了一下,但是也没用到写两个函数就行了,快速幂,素数判断/*...

鸿杰 的博客 518

POJ 3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8785   Accepted: 3695 Description Fermat's theorem states that for any prime number p and

博客已搬迁到http://www.cnblogs.com/cniwoq/ 689

POJ-3641 Pseudoprime numbers

题意:对于一个非素数p和任意一个数a,若a^p=a(mod p),则输出yes,否则输出no。 思路:判断素数+二分幂。 题目链接:http://poj.org/problem?id=3641 View Code 1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib...

bancaidun1539的博客 118

Pseudoprime numbers(POJ-3641 快速幂)

快速幂:快速幂就是所求的幂次方过大,导致代码所用的时间超限。 如:求2^3,3的二进制是11,(n&1)判断次方数的二进制是否为1,n>>1,向右进位1: 代码: k=1,t=n; while(n) { if(n&1)//判断n的最后一位二进制不为0 { k=k*m; } n=n>>1; m=m*m; } 题目描述: Ferm...

qq_44859533的博客 338

【 POJ 3641 】Pseudoprime numbers

Pseudoprime numbersTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 8702 Accepted: 3658 DescriptionFermat’s theorem states that for any prime number p and for any integer a > 1, ap

Nodies 451

Pseudoprime numbers POJ - 3641(快速幂)

Fermat's theorem states that for any prime numberpand for any integera> 1,ap=a(modp). That is, if we raiseato thepthpower and divide byp, the remainder isa. Some (but not very many) ...

alex1997222的博客 203

poj 3641 Pseudoprime numbers

题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for any prime numberpand for any integera> 1,ap=a(modp). That is, if we raisea...

dong12276的博客 147
上一篇: poj2352&hdu1541stars(树状数组)
下一篇: 模运算总结
luckcircle
博客等级 码龄10年 13粉丝 50原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值