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\) ...
随机推荐
- CCCC L2-017. 人以群分 贪心
https://www.patest.cn/contests/gplt/L2-017 题解:贪心,一点小数学 坑:XJB改下标改错了 #include <iostream> #includ ...
- 单机闭环 使用Nginx+Lua开发高性能Web应用
[西域骆驼D1532101213]西域骆驼(VANCAMEL)D1532101213 休闲套脚鞋 卡其43[行情 报价 价格 评测]-京东 http://item.jd.com/1856564.htm ...
- 眠眠interview Question
1. Wkwebkit在异步回调 如何像webview的回调 一样在主线程回调.可以使用runloop 解决么? dispatch get main queue http://www.jiansh ...
- pandas1
https://www.cnblogs.com/nxld/p/6058591.html
- windows上使用logstash-input-jdbc
(一)安装logstash 下载链接 选择下载你要的对应的logstash版本,这个东西解压就能使用了 (二)安装logstash-input-jdbc 就是用执行logstash-plugin.b ...
- Linuxer-"Linux开发人员自己的媒体"第五月稿件和赠书名单
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/78859630 Linuxer已经从一个单纯的读者服务 ...
- 使用gunicorn部署Flask项目
[*] 本文出处:http://b1u3buf4.xyz/ [*] 本文作者:B1u3Buf4 [*] 本文授权:禁止转载 从自己的博客移动过来. gunicorn是一个python Wsgi的WEB ...
- java判断包含contains方法的使用
java中contains方法是判断是否存在包含关系,比如说a =[1,2,3,4], b=1那么a就包含b contains返回的是布尔类型true 和false,包含的话就返回true,不包含的话 ...
- HTML 显示/隐藏DIV的技巧(visibility与display的差别)
参考链接:http://blog.csdn.net/szwangdf/article/details/1548807 div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: ...
- 001-Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...