POJ3641 (快速幂) 判断a^p = a (mod p)是否成立
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是素数,输出no;如果p不是素数,判断a^p对p取余是否等于a。
#include<cstdio>
#include<math.h>
__int64 f(__int64 a,__int64 b)
{
__int64 c=b,t=;
while(b)
{
if(b % != )
{
t=t*a%c;
}
a=a*a%c;
b/=;
}
return t%c;
}
__int64 f2(__int64 a)
{
__int64 i;
if(a <= || a % == ) return ;
for(i=;i<=sqrt(a);i++)
{
if(a % i == ) return ;
}
return ;
}
int main()
{ __int64 p,a;
while(scanf("%I64d %I64d",&p,&a) && p && a)
{
if(f2(p) == ) printf("no\n");
else
{
if(f(a,p) == a) printf("yes\n");
else
printf("no\n");
} }
}
POJ3641 (快速幂) 判断a^p = a (mod p)是否成立的更多相关文章
- 算法竞赛进阶指南--快速幂,求a^b mod p
// 快速幂,求a^b mod p int power(int a, int b, int p) { int ans = 1; for (; b; b >>= 1) { if (b &am ...
- 快速幂(51Nod1046 A^B Mod C)
快速幂也是比较常用的,原理在下面用代码解释,我们先看题. 51Nod1046 A^B Mod C 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. In ...
- (分治法 快速幂)51nod1046 A^B Mod C
1046 A^B Mod C 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. 收起 输入 3个正整数A B C,中间用空格分隔.(1 < ...
- POJ3641(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8529 Accepted: 35 ...
- XTU 1260 - Determinant - [2017湘潭邀请赛A题(江苏省赛)][高斯消元法][快速幂和逆元]
是2017江苏省赛的第一题,当时在场上没做出来(废话,那个时候又不懂高斯消元怎么写……而且数论也学得一塌糊涂,现在回来补了) 省赛结束之后,题解pdf就出来了,一看题解,嗯……加一行再求逆矩阵从而得到 ...
- URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)
题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...
- [CSP-S模拟测试]:随(快速幂+数学)
题目描述 给出$n$个正整数$a_1,a_2...a_n$和一个质数mod.一个变量$x$初始为$1$.进行$m$次操作.每次在$n$个数中随机选一个$a_i$,然后$x=x\times a_i$.问 ...
- uva 10710 快速幂取模
//题目大意:输入一个n值问洗牌n-1次后是不是会变成初始状态(Jimmy-number),从案例可看出牌1的位置变化为2^i%n,所以最终判断2^(n-1)=1(mod n)是否成立#include ...
- POJ3641 Pseudoprime numbers(快速幂+素数判断)
POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...
随机推荐
- SQL 初级教程学习(六)
1.创建视图 CREATE VIEW [Current Product List] ASSELECT ProductID,ProductNameFROM ProductsWHERE Discontin ...
- 2016/10/29 action与form表单的结合使用
1>web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi= ...
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
- Sping Boot返回Json格式自定义
转载请注明http://www.cnblogs.com/majianming/p/8491020.html 在写项目过程中,遇到了需要定义返回的json字段格式的问题 例如在实体属性中,我有一个字段是 ...
- C# 判断是否移动设备
/// <summary> /// 判断是否移动设备. /// </summary> /// <returns></returns> public st ...
- Redis相关问题收集
问题一.强制关闭Redis快照导致不能持久化 MISCONF Redis is configured to save RDB snapshots, but is currently not able ...
- ES3之bind方法的实现模拟
扩展Function原型方法,此处用myBind来模拟bind实现 Function.prototype.myBind = function(o /*,args*/){ //闭包无法获取t ...
- Thinkphp3.23 连接MSSQL方法
Thinkphp 3.23要连接MSSQL,必须配置下,以下是主要的步骤. 1.要安装Microsoft Drivers for PHP for SQL Server驱动 下载驱动以前,要查看一下ph ...
- 初学者SQL shell(psql)无法登陆问题
因为项目第一次接触postgresql,有个问题搞死我了,如果初学,估计大家也会遇见这样的问题,希望可以节约时间. 用户postgres的口令不显示啊!服!
- call, apply, bind 区别
#call, apply, bind 区别及模拟实现call apply bind 三者都可以用来改变this的指向,但是在用法上略有不同 首先说一下call和apply的区别 call和apply ...