poj 3641 Pseudoprime numbers Miller_Rabin测素裸题
题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数。之后输入p,a问p是否为Carmichael Numbers?
坑点:先是各种RE,因为poj不能用srand()...之后各种WA..因为里面(a,p) ?= 1不一定互素,即这时Fermat定理的性质并不能直接用欧拉定理来判定。。即 a^(p-1)%p = 1判断是错误的。。作的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
typedef long long ll;
int T,kase = ,i,j,k,n,m;
ll mult(ll x,ll y,ll mod) // ·ÀÖ¹x*y±¬long long;
{
ll ans = ;x %= mod;
while(y){
if(y&) ans += x, y--;
if(ans >= mod) ans -= mod;
y >>= ;
x <<= ;
if(x >= mod) x -= mod;
}
return ans;
}
ll pow(ll a,ll n,ll mod)
{
a %= mod;
ll ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n >>= ;
}
return ans;
}
int p[]={,,,,,,,,,,,,,,,};
bool Miller_Rabin(ll n)
{
if(n <= ) return n == ;
if(n% == ) return false;
ll t = n - ;
while(t% == ) t >>= ;
for(int i = ;i < ;i++){
if(p[i] >= n) return true;
if(n % p[i] == ) return false;
ll tmp = t;
ll x = pow(p[i],t,n); // p[i]^t % n;
while(tmp < n){
ll y = mult(x,x,n);
if(y == && x != && x != n-) return false;
x = y;
tmp <<= ;
}
if(x != ) return false; // Fermat theory
}
return true;
}
int main()
{
ll x,y;
while(read2(x,y), x + y){
if(Miller_Rabin(x) || pow(y,x,x) != y) puts("no");
else puts("yes");
}
return ;
}
poj 3641 Pseudoprime numbers Miller_Rabin测素裸题的更多相关文章
- poj 3641 Pseudoprime numbers
题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...
- POJ 3641 Pseudoprime numbers (数论+快速幂)
题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- poj 3641 Pseudoprime numbers(快速幂)
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- POJ 3641 Pseudoprime numbers (miller-rabin 素数判定)
模板题,直接用 /********************* Template ************************/ #include <set> #include < ...
- HDU 3641 Pseudoprime numbers(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11336 Accepted: 4 ...
- POJ 2409 Let it Bead(polya裸题)
题目传送:http://poj.org/problem?id=2409 Description "Let it Bead" company is located upstairs ...
- POJ 2234 Matches Game(Nim博弈裸题)
Description Here is a simple game. In this game, there are several piles of matches and two players. ...
- POJ 2031 Building a Space Station (prim裸题)
Description You are a member of the space station engineering team, and are assigned a task in the c ...
随机推荐
- .NET自动识别HttpWebResponse的编码及是否压缩
请求和响应头 POST的数据 最近项目使用HttpWebRequest请求网页,处理HttpWebResponse返回消息体,发现网页可能是有GZIP压缩等,所得数据乱码,所以相处了解决方案,大家共同 ...
- ubuntu禁用笔记本自带键盘
ubuntu如何禁用笔记本键盘 打开终端运行命令 xinput list Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core ...
- [书目20131114]微软技术丛书•快速软件开发(珍藏版) Rapid Development Taming Wild Software Schedules By Steve McConnell
本文摘自:http://shop.oreilly.com/product/9781556159008.do EFFICIENT DEVELOPMENT Chapter 1 Welcome to Rap ...
- 基本STRUTS标签-学习笔记-Bean标签
<bean:include> 和标准的JSP标签<jsp:include>很相似,都可以用来包含其他Web资源的内容,区别在于<bean:include>标签把其它 ...
- Jersey(1.19.1) - Client API, Overview of the API
To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...
- 换模板,修改了一下css,清新多了~
基于elf template,改了一下字体颜色和背景.布局的宽度: 博客园修改css真是方便,如果可以直接编辑template代码就更赞了- 不过相对前端web自定义,我更在意后端的服务器是否稳定: ...
- krpano资料
- C#——字符操作
题目要求:用户随机输入字母及数字组成的字符串,当用户连续输入字符串‘hello’时,程序结束用户输入,并分别显示用户输入的字母及数字的数目. 代码: using System; using Syste ...
- const define 定义常量的区别
1.用const定义常量在编译的时候,提供了类型安全检查,而define 只是简单地进行字符串的替换 2.const定义的常量,会分配相应的内存空间.而define没有分配空间,只是在程序中与处理的时 ...
- java学习笔记_GUI(3)
如何加入自己定义的Panel import javax.swing.*; import java.awt.event.*; import java.awt.*; 5 class MyPanel ext ...