计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)
题目链接:https://nanti.jisuanke.com/t/25985
题目:
Description:
Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:
Every even integer greater than 2 can be expressed as the sum of two primes.
The
actual verification of the Goldbach conjecture shows that even numbers
below at least 1e14 can be expressed as a sum of two prime numbers.
Many times, there are more than one way to represent even numbers as two prime numbers.
For example, 18=5+13=7+11, 64=3+61=5+59=11+53=17+47=23+41, etc.
Now this problem is asking you to divide a postive even integer n (2<n<2^63) into two prime numbers.
Although
a certain scope of the problem has not been strictly proved the
correctness of Goldbach's conjecture, we still hope that you can solve
it.
If you find that an even number of Goldbach conjectures are
not true, then this question will be wrong, but we would like to
congratulate you on solving this math problem that has plagued humanity
for hundreds of years.
Input:
The first line of input is a T means the number of the cases.
Next T lines, each line is a postive even integer n (2<n<2^63).
Output:
The output is also T lines, each line is two number we asked for.
T is about 100.
本题答案不唯一,符合要求的答案均正确
样例输入
1
8
样例输出
3 5
题意:哥德巴赫猜想:任意一个大于2的偶数即可表示为两个素数的和。运用Miller_Rabin判别法从n/2向两边遍历(由素数的分布可知这样时间复杂度更优)。
代码实现如下:
#include <cstdio>
#include <ctime>
#include <cstdlib> typedef long long ll;
int t;
long long n; ll multi(ll a, ll b, ll mod) {
ll ret = ;
while(b) {
if(b & )
ret = ret + a;
if(ret >= mod)
ret -= mod; a = a + a;
if(a >= mod)
a -= mod;
b >>= ;
}
return ret;
}
ll quick_pow(ll a, ll b, ll mod) {
ll ret = ;
while(b) {
if(b & )
ret = multi(ret, a, mod);
a = multi(a, a, mod);
b >>= ;
}
return ret;
}
bool Miller_Rabin(ll n) {
ll u = n - , pre, x;
int i, j, k = ;
if(n == || n == || n == || n == || n == )
return true;
if(n == || (!(n % )) || (!(n % )) || (!(n % )) || (!(n % )) || (!(n % )))
return false;
for(; !(u & ); k++, u >>= );
srand(time(NULL));
for(i = ; i < ; i++) {
x = rand() % (n - ) + ;
x = quick_pow(x, u, n);
pre = x;
for(j = ; j < k; j++) {
x = multi(x, x, n);
if(x == && pre != && pre != (n - ))
return false;
pre = x;
}
if(x != )
return false;
}
return true;
} int main() {
scanf("%d", &t);
while(t--) {
scanf("%lld", &n);
long long k = n /;
if(Miller_Rabin(k)) {
printf("%lld %lld\n", k, k);
} else {
long long l = k - , r = k + ;
while(!Miller_Rabin(l) || !Miller_Rabin(r)) {
l--;
r++;
}
printf("%lld %lld\n", l, r);
}
}
return ;
}
计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)的更多相关文章
- 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...
- 计蒜客——Goldbach
Goldbach 判断大素数 #include<cstdio> #include<cstdlib> using namespace std; #define N 10000 t ...
- 计蒜客模拟赛5 D2T1 成绩统计
又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...
- 计蒜客 等边三角形 dfs
题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...
- 计蒜客:Entertainment Box
Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...
- 计蒜客 31436 - 提高水平 - [状压DP]
题目链接:https://nanti.jisuanke.com/t/31436 作为一名车手,为了提高自身的姿势水平,平时的练习是必不可少的.小 J 每天的训练包含 $N$ 个训练项目,他会按照某个顺 ...
- 计蒜客 31434 - 广场车神 - [DP+前缀和]
题目链接:https://nanti.jisuanke.com/t/31434 小 D 是一位著名的车手,他热衷于在广场上飙车.每年儿童节过后,小 D 都会在广场上举行一场别样的车技大赛. 小 D 所 ...
- 运用NP求解 “跳跃游戏”---计蒜客
计蒜客里面有一道“跳跃游戏的问题” 给定一个非负整数数组,假定你的初始位置为数组第一个下标. 数组中的每个元素代表你在那个位置能够跳跃的最大长度. 你的目标是到达最后一个下标,并且使用最少的跳跃次数. ...
- 计蒜客 作弊揭发者(string的应用)
鉴于我市拥堵的交通状况,市政交管部门经过听证决定在道路两侧安置自动停车收费系统.当车辆驶入车位,系统会通过配有的摄像头拍摄车辆画面,通过识别车牌上的数字.字母序列识别车牌,通过连接车管所车辆信息数据库 ...
随机推荐
- 团队作业7——第二次项目冲刺(Beta版本)-第二篇
1.工作分工: 团队成员 分工 郭达22120 项目整合,后台代码 刘德培44060 数据库模块 石浩洋22061 前台界面优化 曾繁钦22056 前台界面优化.测试 孙斌22030 后台代码 2.燃 ...
- phpcms v9 thumb(缩略图) 函数说明
打开phcmsc/libs/functions/global.func.php文件,找到如下代码:/** * 生成缩略图函数 * @param $imgurl 图片路径 * @param $wid ...
- java zip 压缩与解压
java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...
- WPF 资源应用
对资源的应用,有好多方法,以下是一些应用,可以参考 1.静态资源: 2.动态资源: 3.项目面板中的资源: 4.图片.声音等资源
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ2074:Line of Sight——题解
http://poj.org/problem?id=2074 题目大意:(下面的线段都与x轴平行)给两条线段,一个点在其中一条线段看另一条线段,但是中间有很多线段阻挡视线.求在线段上最大连续区间使得在 ...
- BZOJ3675 [Apio2014]序列分割 【斜率优化dp】
3675: [Apio2014]序列分割 Time Limit: 40 Sec Memory Limit: 128 MB Submit: 3366 Solved: 1355 [Submit][St ...
- 实验五 TCP传输及加解密
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:陈巧然 ...
- [zhuan]动态链接库中的.symtab和.dynsym
http://blog.csdn.net/beyond702/article/details/50979340 原文如下: shared library (.so) "Program Lib ...
- SDWebImage的使用说明
1. 在需要的地方导入头文件 #import "UIImageView+WebCache.h" webCache:网络缓存,几乎目前所有的浏览器都有一个内置的缓存,它们通常利用客户 ...