SPOJ Prime or Not - 快速乘 - 快速幂
Given the number, you are to answer the question: "Is it prime?"
Solutions to this problem can be submitted in C, C++, Pascal, Perl, Python, Ruby, Lisp, Hask, Ocaml, Prolog, Whitespace, Brainf**k and Intercal only.
Input
t – the number of test cases, then t test cases follows. [t <= 500]
Each line contains one integer: N [2 <= N <= 2^63-1]
Output
For each test case output string "YES" if given number is prime and "NO" otherwise.
Example
Input: 5 2 3 4 5 6 Output: YES YES NO YES NO
题目大意是说,给你一个能够用有符号64位整型存储的数,判断它是否是素数。
用费马小定理,多次随机生成一个底数a,然后n - 1次幂,判断模n意义下是否是1。
为了充分表示对rand()的嫌弃,于是手写了一个随机数生成器。详细请见[here]
Code
/**
* SPOJ
* Problem#PON
* Accepted
* Time:180ms
* Memory:15360k
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
;
);
) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
) + (u << ) + x - ');
ungetc(x, stdin);
u *= aFlag;
return true;
}
#define LL long long
typedef class Random {
public:
unsigned int pre;
unsigned int seed;
Random():pre(), seed((unsigned) time (NULL)) { }
Random(), seed(seed) { }
/**
* Generate a random number.
* @return this function will return the random number it gernerated
*/
unsigned int rand() {
// unsigned int ret = (seed * 7361238 + seed % 20037 * 1244 + pre * 12342 + 378211) * (seed + 134543);
// unsigned int ret = (seed * 7361238 + seed % 20037 * 1244 + pre * 12342 + 378211 + time(NULL) * pre) * (seed + 134543);
unsigned int ret;
)
ret = (seed * + seed % * + pre * + (time(NULL) * (pre * + seed * + )) + );
else
ret = (seed * + seed % * + pre * + (time(NULL) * (pre * + seed * + )) + );
pre = seed;
seed = ret;
return ret;
}
}Random;
inline void setLLhighBit(long long& x, int a) {
int* p = (int*)&x;
*(p + ) = a;
}
inline void setLLlowBit(long long& x, int a) {
int* p = (int*)&x;
*p = a;
}
inline void cleanLLSignFlag(long long& a) {
a &= (1ull << ) - ;
}
template<typename T>
T mul_mod(T a, T b, T& moder) {
) return a;
T temp = mul_mod(a, b >> , moder);
) return (((temp + temp) % moder) + a) % moder;
return (temp + temp) % moder;
}
template<typename T>
T pow_mod(T a, T pos, T& moder) {
) return a;
T temp = pow_mod(a, pos >> , moder);
) return mul_mod(mul_mod(temp, temp, moder), a, moder);
return mul_mod(temp, temp, moder);
}
int T;
LL n;
Random r;
inline void work() {
readInteger(n);
) == ) {
) puts("YES");
else puts("NO");
return;
}
unsigned int l, h;
LL a, r1;
; t < ; t++) {
l = r.rand();
h = r.rand();
setLLhighBit(a, h);
setLLlowBit(a, l);
cleanLLSignFlag(a);
a = (a % (n - )) + ;
r1 = pow_mod(a, n - , n);
) {
puts("NO");
return;
}
}
puts("YES");
}
int main() {
readInteger(T);
while(T--) {
work();
}
;
}
SPOJ Prime or Not - 快速乘 - 快速幂的更多相关文章
- hdu 5690 2016"百度之星" - 初赛(Astar Round2A) All X 快速二次幂 || 寻找周期
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5690 题意:m个数字全为x mod k ?= c;其中m <= 1010,0 < c,k ...
- 快速傅里叶变换 & 快速数论变换
快速傅里叶变换 & 快速数论变换 [update 3.29.2017] 前言 2月10日初学,记得那时好像是正月十五放假那一天 当时写了手写版的笔记 过去近50天差不多忘光了,于是复习一下,具 ...
- 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c 二.矩 ...
- 快速乘法,幂计算 hdu5666
在实际应用中为了防止数据爆出,在计算a*b%m和x^n%m时,可以采用此方法.在数论中有以下结论: a*b%m=((a%m)*(b*m))%m ; (a+b)%m=(a%m+b%m)%m ; _int ...
- 快速乘+快速幂(用于模数超过int范围)
一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...
- 第四章 istio快速入门(快速安装)
4.1 环境介绍 K8s 1.9 以上版本. 4.2 快速部署Istio 下载: https://github.com/istio/istio/releases/, 下载 1.1.0-snapsh ...
- [开源]OSharpNS - .net core 快速开发框架 - 快速开始
什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...
- 模板 - 数学 - 快速傅里叶变换/快速数论变换(FFT/NTT)
先看看. 通常模数常见的有998244353,1004535809,469762049,这几个的原根都是3.所求的项数还不能超过2的23次方(因为998244353的分解). 感觉没啥用. #incl ...
- 快速沃尔什变换&快速莫比乌斯变换小记
u1s1 距离省选只剩 5 days 了,现在学新算法真的合适吗(( 位运算卷积 众所周知,对于最普通的卷积 \(c_i=\sum\limits_{j+k=i}a_jb_k\),\(a_jb_k\) ...
随机推荐
- Drip is a launcher for the Java Virtual Machine that provides much faster startup times than the java command. The drip script is intended to be a drop-in replacement for the java command, only faster
小结: 1.初始化jvm: 2.第一次唤醒java命令不快,后续快: https://github.com/elastic/logstash Advanced: Drip Launcher Drip ...
- iOS UITextField实时监听获取输入内容,中文状态去除预输入拼音
http://blog.csdn.net/cse110/article/details/51360796 - (void)textFieldDidChange:(UITextField *)textF ...
- flask的session用法
转自:https://www.cnblogs.com/52forjie/p/8282453.html 简介 flask-session是flask框架的session组件,由于原来flask内置ses ...
- Linux目录【持续更新中】
故障排除 服务器为什么这么慢?耗尽了CPU.RAM和磁盘I/O资源 服务 ELK服务基础 基础 常用命令 curl命令 Nginx服务基础 Nginx正向代理配置 Nginx文件下载服务器 Nginx ...
- Servlet----------ServletContext (重要)
1.ServletContext的概述 一个项目只有一个ServletContext对象!application 我们可以在N多个Servlet中获取这个唯一的对象,使用它来给多个Servlet传递数 ...
- 全套 AR 应用设计攻略都在这里!
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/79823908 通过将虚拟内容与现实世界融合,增 ...
- iota 币产生私钥的方法
iota 币的官网是 iota.org, iota 的官网推荐的钱包地址是: https://github.com/iotaledger/wallet iota 币产生私钥是没有什么特殊的要 ...
- [py]django前台处理后端返回的各类数据
参考 要完成的任务 返回str 返回list 返回arr 前端遍历 关键字 if for语句处理str list dict - 遍历字典 for语句 {% for key, value in info ...
- HDU5012:Dice(bfs模板)
http://acm.hdu.edu.cn/showproblem.php?pid=5012 Problem Description There are 2 special dices on the ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...