POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g…
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 Description 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…
输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; typedef long long ll; ll p,a; int whether(int p) { ; ;i*i<=p;i++) ) {…
-->Carmichael Numbers  Descriptions: 题目很长,基本没用,大致题意如下 给定一个数n,n是合数且对于任意的1 < a < n都有a的n次方模n等于a,这个数就是Carmichael Number. 输出The number n is a Carmichael number. n是素数 输出 n is normal. Input 多组输入,第一行给一个n (2 < n < 65000) .n = 0 表示输入结束并不需要处理 Output 对…
Description 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 ps…
题目链接: https://vjudge.net/problem/POJ-3641 题目大意: 问p是不是伪素数.伪素数条件:①p不是素数.② ap = a (mod p). 思路: 直接快速幂模板+素数判断 #include<iostream> #include<vector> #include<queue> #include<algorithm> #include<cstring> #include<cstdio> #includ…
题意:给你一个数,让你判断是否是非素数,同时a^n%n==a (其中 a 的范围为 2~n-1) 思路:先判断是不是非素数,然后利用快速幂对每个a进行判断 代码: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #define ll long long using namespace std; bool isprime(ll num) { ) return…
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时01分45秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #…
题意 给出两个数字 P 和 A 当p 不是素数 并且 满足a^p≡a(mod p) 就输出 yes 否则 输出 no 思路 因为 数据范围较大,用快速幂 AC代码 #include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath>…
Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: 3210 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth…
ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> #include<cmath> using namespace std; long long M,i; #define LL long long int _work(LL a,LL n) { LL ans=1; while(n){ if(n&1){ ans=(ans*a)%M; n--; }…
Description 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 ps…
给你两个数字p,a.如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”. 很简单的板子题.核心算法是幂取模(算法详见<算法竞赛入门经典>315页). 幂取模板子: int pow_mod(int a,int n,int m) { ) ; , m); long long ans = (long long)x * x % m; ) ans = ans * a % m; return (int)ans; } 题目代码也比较简单,有一个坑点是如果用筛素数打表,数组开不了这么大…
链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). 我们知道Miller-Rabin素数测试的算法原理就是基于费马小定理的,因为我们在测试底数的时候只是随机一些 a ,所以可能有的合数就脸一白通过了测试,于是就产生了伪素数这一概念,现在给你一对 p and a,判断 p 是否是以 a 为基的伪素数 思路:对于素数来说是不…
Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know…
题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL Ksm(LL a, LL b, LL p) { LL ans = 1; while(b) { if(b & 1) { ans = (ans * a) % p; } a = (a * a) % p; b >>= 1; } return ans; } int main() { LL p, a…
http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> #include<cmath> using namespace std; typedef long long ll; bool judge_prime(ll k) { ll i; ll u=int(sqrt(k*1.0)); ;i<=u;i++) { ) ; } ; } ll mod_p…
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { ],b[…
#include <cstdio> typedef long long ll; int quick_pow(ll a,ll b,ll mod){ ll ans=; ))ans=(ans*a)%mod; return ans; } int main(){ int z,m,h,a,b,ans; for(scanf("%d",&z);z--;){ scanf(; while(h--)scanf("%d%d",&a,&b),ans=(an…
题目链接:POJ 3641 Description 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, know…
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,…
Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以 p 的余数等于 a .p 的某些 (但不是很多) 非素数的值,被称之为以 a 为底的伪素数,对于某个 a 具有该特性.并且,某些 Carmichael 数,对于全部的 a 来说,是以 a为底的伪素数. 给定 2 < p ≤ 1000000000 且 1 < a < p ,判断 p 是否为以 …
  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one…
Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11336   Accepted: 4891 Description 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…
大家都知道RSA的加密的安全性就是能够找到一个合适的大素数,而现在判断大素数的办法有许多,比如Fermat素性测试或者Miller-Rabin素性测试,而这里我用了Miller-Rabin素性测试的算法,具体的理论我写到下面. 算法的理论基础: Fermat定理:若n是奇素数,a是任意正整数(1≤ a≤ n−1),则 a^(n-1) ≡ 1 mod n. 2.  如果n是一个奇素数,将n−1表示成2^s*r的形式,r是奇数,a与n是互素的任何随机整数,那么a^r ≡ 1 mod n或者对某个j…
-->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1  B1 A2  B2 A3  B3 ......... AH  BH 有Z组数据   求(A1B1+A2B2+ ... +AHBH)mod M. Sample Input 3 16 4 2 3 3 4 4 5 5 6 36123 1 2374859 3029382 17 1 3 18132 Sample Output 2 13195 13 题目链接https://v…
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(ll l,ll r,ll &x,ll &y) { if(r==0){x=1;y=0;return l;} else { ll d=exgcd(r,l%r,y,x); y-=l/r*x; return d; } } 3.求a关于m的乘法逆元 ll mod_inverse(ll a,ll m){ l…
A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4550    Accepted Submission(s): 1444 Problem Description Xinlv wrote some sequences on the paper a long time ago, they might…
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace std; #define LL long long LL mul_mod(LL a,LL b,int n) { return a*b%n; } LL pow_mod(LL a,LL p,LL n) { ) ; LL ans=pow_mod(a,p/,n); ans=ans*ans%n; ==) ans=an…
Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: 3461 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth…