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-a 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 contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.
For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
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
17 1073741824 25 0
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; }