HDU 1905 + POJ 1730 【快速幂】
来源:互联网 发布:贷款软件 编辑:程序博客网 时间:2024/06/15 15:51
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 210 3341 2341 31105 21105 30 0
nonoyesnoyesyes
题意:给出一个p个a,如果p是素数,则直接输出no
否则判断(a^p)%p是不是等于a
思路:裸题.
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long longll 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.
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.
For each test case, output a line giving the largest integer p such that x is a perfect p th power.
171073741824250
1302题意:给出一个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 1000000007ll 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;}
阅读全文
0 0
- HDU 1905 + POJ 1730 【快速幂】
- 矩阵快速幂 HDU 2157 HDU 5015 POJ 3233
- 快速幂POJ 1061
- poj 1995 快速幂
- POJ 3734 (快速幂)
- POJ 1995 快速幂
- POJ-----3641快速幂
- POJ 3641 快速幂
- poj 3641 快速幂
- poj 1995 快速幂
- poj 3641 快速幂
- 快速幂+素数 HDU 1905 Pseudoprime numbers
- HDU 1905 Pseudoprime numbers【素数】【快速幂】
- HDU 1905Pseudoprime numbers(快速幂)
- hdu 1905 快速幂取模
- HDU 4506 快速幂
- hdu 4506 快速幂
- 快速幂 hdu 2035
- Python入门教程(一)manage.py和django-admin.py的区别
- codeforces 891C
- 搭建一个简易的时间服务器笔记----C++学习之路
- 有关电脑的基本技能
- 使用Construct 2制作HTML5游戏(进阶篇)
- HDU 1905 + POJ 1730 【快速幂】
- Linux系统的软件组成
- 两杯水交换
- Android开发小结——UI开发
- Android UI基础之ListView 和适配器Adapter
- C和指针之字符串之strlen、strcpy、 strcat、strcmp使用总结
- Android 插件化原理解析——Activity生命周期管理
- LoRaWAN终端产品的“身份证”
- HDOJ2081 手机短号