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( ...
随机推荐
- sublime text3环境与工具搭建
1,ctrl+shift+P ,打开包安装窗口如下: 2,选择 install Package,安装详解插件 1-安装 JsFormat插件,用于格式化js的插件,使用快捷键 Ctrl+Alt+F对J ...
- Mycat分表分库
一.Mycat介绍 Mycat 是一个开源的分布式数据库系统,是一个实现了 MySQL 协议的的Server,前端用户可以把它看作是一个数据库代理,用 MySQL 客户端工具和命令行访问,而其后端可以 ...
- POJ 2375
BFS+强连通.输出max(缩点后出度为0的点数,缩点后入度为0的点数). #include <cstdio> #include <iostream> #include < ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版 Link-Cut-Tree+划分树
题目大意: 给定n个点m条边的无向图.求问当图中仅仅有[编号在[l,r]区间内]的边存在时图中的联通块个数 强制在线 注意联通块是指联通了就是同一块,不是Tarjan求的那种块 看到这题的那一刻我就想 ...
- poj2750--Potted Flower(线段树)
题目链接:点击打开链接 题目大意:给出n个数排成一个环.求环的最大连续子序列,不能是总序列 建一个线段树来求最大子序列假设仅仅是一个序列.那么求最大连续子序列非常easy,可是假设是一个环,那就要考虑 ...
- cocos2d-x的gitignore配置
# Ignore thumbnails created by windows Thumbs.db # Ignore files build by Visual Studio *.obj *.exe * ...
- linux for LVM 创建笔记
LVM: 1.创建pv(物理卷) [root@localhost dev]# pvcreate /dev/sdd /dev/sde /dev/sdf Writing physical volume d ...
- TensorRT加速 ——NVIDIA终端AI芯片加速用,可以直接利用caffe或TensorFlow生成的模型来predict(inference)
官网:https://developer.nvidia.com/tensorrt 作用:NVIDIA TensorRT™ is a high-performance deep learning inf ...
- DNS通道检测 国内学术界研究情况——研究方法:基于特征或者流量,使用机器学习决策树分类算法居多
http://xuewen.cnki.net/DownloadArticle.aspx?filename=BMKJ201104017&dbtype=CJFD<浅析基于DNS协议的隐蔽通道 ...
- sublime-text 编译C
sublime的优点多多. 先下好sublime-text 如果不会下载 在ubuntu 下输入sudo apt-cache search sublime 查找到合适的安装包后apt-get inst ...