hdu1905(判断质数+快速幂)
2017-10-02 19:48
190 查看
Pseudoprime numbers
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3192 Accepted Submission(s): 1441
Problem Description
Fermat's theorem states that for any prime number p and for any integer a > 1, a^p == 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 ≤ 1,000,000,000 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
Author
Gordon V. Cormack
#include<stdio.h> #include<iostream> #include<math.h> using namespace std; typedef long long ll; //判断是不是素数+快速幂取模 int prime(int x) { for(int i=2;i*i<x;i++) { if(x%i==0) return 0; } return 1; } ll fsm(ll a,ll b,ll mod) { ll res=1; while(b) { if(b&1) res=res*a%mod; a=a*a%mod; b>>=1; } return res%mod; } int main() { int p,a; while(scanf("%d%d",&p,&a)!=EOF) { if(!p&&!a) break; if(prime(p)) printf("no\n"); else { if(fsm(a,p,p)==a) { printf("yes\n"); } else printf("no\n"); } } return 0; }
相关文章推荐
- (hdu step 2.1.2)How many prime numbers(判断一个数是否是质数)
- http://acm.hdu.edu.cn/showproblem.php?pid=1905 素数判断 + 二分
- HDU 3864 D_num Miller Rabin 质数判断+Pollard Rho大整数分解
- ACM--多边形凹凸判断--HDOJ 2108--Shape of HDU
- HDU 1180 诡异的楼梯(BFS+奇偶步数判断)
- POJ 1308&&HDU 1272 并查集判断图
- 判断某数是否为质数 [已写成函数]
- hdu 2872 Another Snake 爆搜 判断射线与线段相交
- 判断质数5个程序
- HDU 1325 Is It A Tree? ( 有向图判断环
- HDU-2444-The Accomodation of Students [二分匹配][判断二分图][BFS染色法]
- HDU 1272 小希的迷宫 并查集 (判断任意2个点是否有且仅有一条路径可以相通)
- 使用C# 判断给定大数是否为质数的详解
- hdu 4196 经典质数题型
- 判断一个范围内的哪些是质数
- 判断整数是否为质数?
- HDU 4751 Divide Groups(判断是否为二分图)
- 201111621401-白乐乐-判断一个正整数是否为质数的算法。函数签名如下 int isPrime(long a) 输入:一个长整数a 输出:返回1(为质数),返回0(非质数)
- 计蒜客 判断质数,简单裴波那契数列,矩阵翻转
- HDU - 3342 Legal or not(拓扑排序判断成环)