HDU 1905 + POJ 1730 【快速幂】

Pseudoprime numbers


 

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-pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-apseudoprimes for all a.)

Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-apseudoprime.

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个a,如果p是素数,则直接输出no
否则判断(a^p)%p是不是等于a
思路:裸题.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
#define ll long long
ll ppow(ll a,ll b,ll mod)
{
    ll ans=1;
    ll base=a;
    while(b!=0)
    {
        if(b&1!=0)
            ans=ans*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return ans;
}
int judge(ll n)
{
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return 1;
    return 0;
}
int main()
{
    ll p,a;
    while(cin>>p>>a)
    {
        if(p==0&&a==0)
            break;
        //cout<<judge(p)<<endl;
        if(judge(p)==0)
            cout<<"no"<<endl;
        else if(ppow(a,p,p)==a)
            cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

Perfect Pth Powers

 

   
   
We say that x is a perfect square if, for some integer b, x = b  2. Similarly, x is a perfect cube if, for some integer b, x = b  3. More generally, x is a perfect pth power if, for some integer b, x = b  p. Given an integer x you are to determine the largest p such that x is a perfect p  th power.
Input
Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.
Output
For each test case, output a line giving the largest integer p such that x is a perfect p  th power.
Sample Input
17
1073741824
25
0
Sample Output
1
30
2
题意:给出一个n,判断他的最大次幂 也就是x^p=n 确定p的最大取值
思路:乍一看没什么难度,大师有坑
n是可以小于0的,对于8和-8来说最大都是3分别是2^3   (-2)^3    对于64来说+64=(2)^6    -64=(-4)^3
所以说n<0的时候p是不可能是偶数的   由于已经给出了上限 负数时只去奇数即可
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
#define ll long long
#define mod 1000000007
ll ppow(ll a,ll b)
{
    ll ans=1;
    ll base=a;
    while(b!=0)
    {
        if(b&1!=0)
            ans=ans*base%mod;
        base=base*base%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    int n;
    int i;
    while(cin>>n)
    {
        if(n==0)
            break;
         if(n>0)
         {
            for(i=31;i>=1;i--)
            {
                int temp=(int)(pow(1.0*n,1.0/i)+0.1);
                int temp2=(int)(pow(1.0*temp,1.0*i)+0.1);
                if(n==temp2)
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
        else
        {
            n=-n;
            for(i=31;i>=1;i-=2)
            {
                int temp=(int)(pow(1.0*n,1.0/i)+0.1);
                int temp2=(int)(pow(1.0*temp,1.0*i)+0.1);
                if(n==temp2)
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值