POJ 3641 pseudoprime Numbers (fast Power + prime)

Source: Internet
Author: User
Tags bool pow

Title Link: http://poj.org/problem?id=3641

Test instructions: Given a,p, if p is a prime number and AP = A (mod p) that is, a P-modulo remainder is a, output "yes", otherwise output "no".

Solving: fast Power + judging primes.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
BOOL IsPrime (int n)
{
	bool flag=true;
	for (int i=2;i*i<n;i++)
	{
		if (n%i==0)
		{
			flag=false;
			break;
		}
	}
	return flag;
}
ll POW (ll x,ll n,ll MoD)
{
	ll res=1;
	while (n>0)
	{
		if (n&1) res=res*x%mod;
		X=x*x%mod;
		n>>=1;
	}
	return res;
}
int main ()
{
	//freopen ("In.txt", "R", stdin);
	Freopen ("OUT.txt", "w", stdout);
	int p,a;
	while (scanf ("%d%d", &p,&a))
	{
		if (p==0&&a==0) break;
		if (POW (a,p,p) ==a&&!isprime (p))
		{
			printf ("yes\n");
		}
		else 
			printf ("no\n");
	}
	return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.