Pseudoprime numbers---费马小定理
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13406 | Accepted: 5792 |
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的值判断,若p为素数,就断定不是伪素数,若p不是素数,则判断式子(a^n)%p==a;若相等,
则输出yes,否则输出no;由于数据较大,故容易超时,费马小定理;*/
难受的是sqrt函数返回的是double型,在类型转换的时候poj一直提示compile error,浪费了我好多时间
这里再说一下sqrt()函数:
sqrt()函数,里面的形参是double型的,所以调用的时候,要强制转换成double型。
sqrt()函数都最后返回值是double型,而n是int型,所以要强制转换n=(int)sqrt((double)x);
- #include<iostream>
- #include<string.h>
- #include<math.h>
- #define max 0x3f3f3f3f
- #define ll long long
- #define mod 1000000007
- using namespace std;
- ll vis[];
- ll cnt = ;
- int isprime(ll p)
- {
- int x=(double)sqrt(p)+0.5;
- for(ll i=;i<=x;i++)
- if(p%i==)
- return ;
- return ;
- }
- ll f(ll a, ll b, ll n) //定义函数,求a的b次方对n取模
- {
- ll t, y;
- t = ;
- y = a;
- while (b != )
- {
- if ((b & ) == )
- t = t * y%n;
- y = y * y%n;
- b = b >> ;
- }
- return t;
- }
- int main()
- {
- ll a, p;
- while (cin >> p >> a)
- {
- if (a == && p == )
- break;
- else
- {
- if (isprime(p))
- cout << "no" << endl;
- else if (a == f(a, p, p))
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- }
- }
- return ;
- }
Pseudoprime numbers---费马小定理的更多相关文章
- CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造
题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k&l ...
- HDU——5667Sequence(矩阵快速幂+费马小定理应用)
Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- nyoj1000_快速幂_费马小定理
又见斐波那契数列 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 斐波那契数列大家应该很熟悉了吧.下面给大家引入一种新的斐波那契数列:M斐波那契数列. M斐波那契数列 ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- 数论初步(费马小定理) - Happy 2004
Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...
- 【BZOJ1951】【SDOI2010】古代猪文 Lucas定理、中国剩余定理、exgcd、费马小定理
Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
随机推荐
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Zynq 在Ubuntu上搭建编译环境
http://bbs.elecfans.com/jishu_487981_1_1.html 以下操作均在root用户下完成1,下载交叉编译器在ubuntu里下载arm-2010.09-62-arm-x ...
- 测试中常用到的linux命令
1. man 格式化以及列出命令的(在线)操作手册. 使用方法 man [ -options ] name man man ...
- Ubuntu安装开发版pidgin支持lwqq插件
sudo add-apt-repository ppa:lainme/pidgin-lwqq """添加pidgin-lwqq源""" s ...
- MongoDB整理笔记の索引
MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引是凌驾于数 ...
- C#生成静态文件
一般生成文件都是通过读取模板文件,然后替换标签. 这些古老的方法使用起来不但麻烦而且效率还不怎么样. 这里给添加介绍一个方法. 如果你用过asp.net.mvc (Razor),你就应该明白 chtm ...
- arp欺骗进行流量截获-2
上一篇讲了原理,那么这一篇主要讲如何实现.基本上也就是实现上面的两个步骤,这里基于gopacket实现,我会带着大家一步步详细把每个步骤都讲到. ARP 欺骗 首先就是伪造ARP请求,让A和B把数据包 ...
- .Net Core .Net Core的学习
.Net Core 学习 一.什么是.net core? 百度百科: https://baike.baidu.com/item/.net%20core/20130686?fr=aladdin 个人总结 ...
- 「JSOI2009」球队收益 / 球队预算
题目链接 戳我 \(Solution\) 我们发现这道题目并不好做,因为要考虑两个因素对答案的影响.于是我们假设接下来的\(m\)场比赛双方都输了.这要我们就只要考虑赢一场对答案的影响了,那每赢一场输 ...
- 第一篇 Python的数据类型
Python的标准数据类型有五种: (1)字符串 (2)数字(包括整数,浮点数,布尔,复数) (3)列表(list) (4)元组(tuple) (5)字典(dict) 注:使用type函数可以查看对象 ...