NYIST468(Miller_Rabin+定理)
来源:互联网 发布:mac如何使窗口半屏幕 编辑:程序博客网 时间:2024/06/11 01:01
继续整理神奇的公式中。
题意很简单,直接上神奇的公式:
如果
那这个题就可以转化为
for:m=1~n
即for:m=1~n
即:
for:m=1~n
这里要注意的是除了4,其他的所有偶数都会有比2大的因数
所以除了4这个合数之外,n必须不能是合数,但是,素数2不符合定义,所以特判1,2,4三个元素,然后其他的n是素数就输出Yes,否则输出No。
用M_R判断素数就OK了
code:
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<iostream>#include<string>#include <set>//a&3==a%4using namespace std;#define ll long long#define mem(a) memset(a,0,sizeof(a))const double eps=1e-8;const int maxn=30010;const int inf=0x3f3f3f3f;const int S = 10; long long mult_mod(long long a,long long b,long long c){ a %= c; b %= c; long long ret = 0; long long tmp = a; while(b) { if(b & 1) { ret += tmp; if(ret > c) ret -= c; } tmp <<= 1; if(tmp > c) tmp -= c; b >>= 1; } return ret;}long long pow_mod(long long a,long long n,long long mod){ long long ret = 1; long long temp = a%mod; while(n) { if(n & 1) ret = mult_mod(ret,temp,mod); temp = mult_mod(temp,temp,mod); n >>= 1; } return ret;}bool check(long long a,long long n,long long x,long long t){ long long ret = pow_mod(a,x,n); long long last = ret; for(int i = 1;i <= t;i++) { ret = mult_mod(ret,ret,n); if(ret == 1 && last != 1 && last != n-1) return true; last = ret; } if(ret != 1) return true; else return false;}bool Miller_Rabin(long long n){ if( n < 2)return false; if( n == 2)return true; if( (n&1) == 0)return false;// long long x = n - 1; long long t = 0; while( (x&1)==0 ) { x >>= 1; t++; } for(int i = 0;i < S;i++) { long long a = rand()%(n-1) + 1; if( check(a,n,x,t) ) return false; } return true;}int main(){ ll a; while(cin>>a) { if(a==4) { printf("Yes\n"); } else if(a==2) { printf("No\n"); } else if(a==1) { printf("No\n"); } else { if(Miller_Rabin(a)) printf("Yes\n"); else printf("No\n"); } } return 0;}
阅读全文
0 0
- NYIST468(Miller_Rabin+定理)
- HDU4910 (数论:Miller_Rabin+一个定理)
- miller_rabin
- Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
- Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
- Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
- Miller_Rabin素数测试[Fermat小定理][二次探测定理][同余式][Wilson定理]
- Miller_Rabin算法
- Miller_Rabin素数测试
- zjut 1517 Miller_Rabin
- POJ 1811 Miller_Rabin+Pollard_Rho
- Miller_Rabin素数测试
- Miller_Rabin素数测试
- Miller_Rabin素数测试
- Miller_Rabin 判断素数
- Miller_Rabin素数测试
- 【poj1811】Miller_Rabin and Pollard_rho
- 改进Miller_rabin素数测试
- Parts of a list -- 7 kyu
- NOIP 2015提高组 跳石头
- 史上最详细Windows版本搭建安装React Native环境配置
- 计算机网络--http协议面试知识点总结
- 58集团2017前端工程师笔试试题
- NYIST468(Miller_Rabin+定理)
- HDU 1576 A/B
- CVPR2014 Objectness BING 源码编译
- 系统启动流程(五)
- Java并发编程艺术 6 Fork/Join框架
- JVM相关面试题及答案
- 1th Roman_to_Integer
- codevs 1021 玛丽卡
- Java8,静态默认方法,Lambd继承接口写法,与代码覆盖率测试