北大ACM3641——Pseudoprime numbers~~快速求幂法

这一题,主要是快速求幂的方法的应用。

可以看看快速求幂方法的原理:http://blog.csdn.net/qq_25425023/article/details/44316463

题目的大概意思是:

输入两个数p,a,p为素数,则直接输出no,否则判断a^p % p == a?等于就yes,不等于就no。

理解了题目的意思,就很容易了。

下面的是AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

int is_prime(__int64 x)             //判断是否为素数
{
	int flag = 0;
	for(int i = 2; i * i <= x; i++)
	{
		if(x % i == 0)
		{
			flag = 1;
			break;
		}
	}
	if(flag)
		return 0;
	else
		return 1;
}

int main()
{
	__int64 p, a;
	while(scanf("%I64d%I64d", &p, &a) != EOF)
	{
		if(p == 0 && a == 0)
			break;
		if(is_prime(p))                     //判p是否为素数
		{
			cout << "no" << endl;
			continue;
		}
		__int64 sum = 1;
		int mod = p;                       //备份p,后面p的值会改变
		int ans = a;                       //备份a,后面a的值会改变
		while(p > 0)
		{
			if(p & 1)                      //p的二进制数的最后一位为1
				sum = sum * a % mod;       //sum 乘上
			a = a * a % mod;               //不断乘以a
			p >>= 1;                       //p除以2;
		}
		if(sum == ans)
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}
时间: 2024-10-14 01:10:58

北大ACM3641——Pseudoprime numbers~~快速求幂法的相关文章

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g

POJ 3641 Pseudoprime numbers (快速幂)

题意:给出a和p,判断p是否为合数,且满足a^p是否与a模p同余,即a^p%p与a是否相等 算法:筛法打1万的素数表预判p.再将幂指数的二进制形式表示,从右到左移位,每次底数自乘. #include <cstdio> #include <cstring> typedef long long LL; int p[10010]; bool np[100010]; int cntp; void SievePrime(int n) { memset(np, true, sizeof(np)

A sequence of numbers(快速求幂)

题目描述 Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in

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 and for any integer a > 1, ap = a (mod p). That is, if we raise a to the pth power

POJ3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6644   Accepted: 2696 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

POJ2478_Farey Sequence【快速求欧拉函数】

Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12377 Accepted: 4808 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1

Pseudoprime numbers poj3641

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8682   Accepted: 3645 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

poj Pseudoprime numbers 3641

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10903   Accepted: 4710 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

非递归快速求幂算法

快速求正整数次幂,当然不能直接死乘.举个例子:3 ^ 999 = 3 * 3 * 3 * … * 3直接乘要做998次乘法.但事实上可以这样做,先求出2^k次幂:3 ^ 2 = 3 * 33 ^ 4 = (3 ^ 2) * (3 ^ 2)3 ^ 8 = (3 ^ 4) * (3 ^ 4)3 ^ 16 = (3 ^ 8) * (3 ^ 8)3 ^ 32 = (3 ^ 16) * (3 ^ 16)3 ^ 64 = (3 ^ 32) * (3 ^ 32)3 ^ 128 = (3 ^ 64) * (3