Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )
题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的。例如,2143就是一个4位全数字数,同时它恰好也是一个素数。
最大的全数字的素数是多少?
思路:
- 最大全排列素数可以从 n = 9 使用 perv_permutation 倒序生成。
- 当 n = 9 或者 n = 8 时生成的全排列构成的数一定不是素数,因为它一定能被 3 整除,所以从 7 开始枚举。
- 因为生成的数字太大,所以采用米勒测试判断素数。
/*************************************************************************
> File Name: euler041.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年07月01日 星期六 14时46分33秒
************************************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <inttypes.h>
#define MAX_ROUND 30
int32_t quick_pow(int64_t a , int64_t b , int64_t mod) {
int64_t ret = 1;
while (b) {
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret % mod;
}
bool R_M_TEST(int64_t n) {
int64_t x;
for (int32_t i = 0 ; i < MAX_ROUND ; i++) {
x = (rand() % (n - 1)) + 1;
if (quick_pow(x , n - 1 , n) != 1) return false;
}
return true;
}
int32_t main() {
int64_t tmp;
int32_t num[9] = {0} , flag = 0;
srand(time(NULL));
for(int32_t i = 7 ; i >= 1 ; i--) {
for (int32_t j = 0 ; j < i ; j++)
num[j] = i - j;
do {
tmp = 0;
for (int32_t j = 0 ; j < i ; j++)
tmp = tmp * 10 + (int64_t)num[j];
if (R_M_TEST(tmp)) {
printf("ans = " "%"PRId64"\n",tmp);
flag = 1; break;
}
} while(std::prev_permutation(num , num + i));
if (flag) break;
}
return 0;
}
Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )的更多相关文章
- Project Euler:Problem 41 Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- Project Euler 50 Consecutive prime sum
题意: 素数41可以写成六个连续素数的和: 41 = 2 + 3 + 5 + 7 + 11 + 13 在小于一百的素数中,41能够被写成最多的连续素数的和. 在小于一千的素数中,953能够被写成最多的 ...
- Project Euler 87 :Prime power triples 素数幂三元组
Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- Project Euler Problem 7-10001st prime
素数线性筛 MAXN = 110100 prime = [0 for i in range(210000)] for i in range(2,MAXN): if prime[i] == 0: pri ...
- Project Euler 38 Pandigital multiples
题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...
- Project Euler 32 Pandigital products
题意:找出所有形如 39 × 186 = 7254 这种,由 1 - 9,9个数字构成的等式的和,注意相同的积不计算两次 思路:如下面两种方法 方法一:暴力枚举间断点 /*************** ...
- Project Euler 27 Quadratic primes( 米勒测试 + 推导性质 )
题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40( ...
随机推荐
- find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...
- mysql稳定的版本号选择及下载说明(2014-11-10)
怎样选择新稳定的版本号 mysql的版本号大概能够分为Alpha.Beta.GA. GA版即mysql官方公布的稳定版本号. 怎样在官方下载Mysql 能够通过http:// ...
- 【cl】cmd相关命令
cd 进入目录 dir 列出当前目录下的文件[在linux上是ls] e: 进入E盘 tab键可以快速进入目录
- UVA 4855 Hyper Box
You live in the universe X where all the physical laws and constants are different from ours. For ex ...
- HDU 4386 Quadrilateral(数学啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4386 Problem Description One day the little Jack is p ...
- poj 1018(枚举+贪心)
通讯系统 We have received an o ...
- [JXOI 2018] 守卫 解题报告 (DP)
interlinkage: https://www.luogu.org/problemnew/show/P4563 description: solution: 注意到对于范围$[l,r]$,$r$这 ...
- ES6和Node容易搞混淆的点
ES6 import 模块名 from XX '模块标识符' -----导入模块 import '路径 ' -----导入CSS样式 export default { } 和export ...
- Java中的命名规范到底是怎样的
内容摘要:命名规范二,java中的方法名,对象名和字段名的第一个单词的首写字母应该小写,而后面的每个单词的首字母都应该小写 要想将java基础学的十分的牢固就必须将java中的命名规范掌握好了.俗话说 ...
- ADO.NET增删改
static void Main1(string[] args) {添加造连接字符串string connstring = "server=.;database=mydb;user=sa;p ...