EularProject 43: 带条件约束的排列组合挑选问题
Sub-string divisibility
Problem 43
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
Answer:
16695334890
python code:
from functools import reduce
def helpfunc1(x,y):
return 10*x+y
def helpfunc2(lst):
return reduce(helpfunc1,lst)
def Descriminent(k,value):
if k==3:
if value%17==0:
return True
else:
return False
if k==4:
if value%13==0:
return True
else:
return False
if k==5:
if value%11==0:
return True
else:
return False
if k==6:
if value%7==0:
return True
else:
return False
if k==7:
if value%5==0:
return True
else:
return False
if k==8:
if value%3==0 and (value//10)%2==0:
return True
else:
return False
return True
def func(result,charlist):
total=0
length=len(result)
if length>2 and length<9:
if Descriminent(length,helpfunc2(result[0:3]))==False:
return 0
if length==10:
return helpfunc2(result)
if len(charlist)>0:
for i in range(0,len(charlist)):
resultNext=result.copy()
charlistNext=charlist.copy()
resultNext.insert(0,charlistNext[i])
del charlistNext[i]
total+=func(resultNext,charlistNext)
return total
charlist=[0,1,2,3,4,5,6,7,8,9]
result=[]
print(func(result,charlist))
解题思路:使用递归
提高效率点:从后往前递归
计算时间<1s
由于可以整除2的数比整除17的多的太多了,其它的一样道理。因此相比于从后往前。从前往后递归的话无用功将成阶乘阶数添加。
EularProject论坛上的一个非递归版本号
digits = ['1','2','3','4','5','6','7','8','9','0']
divisors = [13, 11, 7, 5, 3, 2, 1]
res = []
res1 = []
j = 1
while j*17 < 1000:
N = j*17
Nstr = str(N)
if len(set(Nstr)) == len(Nstr):
if N < 100: res.append('0' + str(N))
else: res.append(str(N))
j += 1
for div in divisors:
for Nstr in res:
for d in digits:
if d not in Nstr:
Newstr = d + Nstr[:2]
if int(Newstr)%div == 0:
res1.append(d + Nstr)
res = res1
res1 = []
tot = 0
for Nstr in res:
print(Nstr)
tot += int(Nstr)
print(tot)
EularProject 43: 带条件约束的排列组合挑选问题的更多相关文章
- Laravel 5.2数据库--多个关联关系,带条件约束的渴求式加载的问题
### 今天在连表获取数据的时候,老是获取不到想要的,确实有点无力适从的感觉. 归根到底,还是对laravel不够熟悉,至少是数据库操作那块. ### 问题是这样的: 我想要通过连表中间表,拿中间表的 ...
- python自带的排列组合函数
需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯. 分析:提炼 ...
- 【专题】计数问题(排列组合,容斥原理,Prufer序列)
[容斥原理] 对于统计指定排列方案数的问题,一个方案是空间中的一个元素. 定义集合x是满足排列中第x个数的限定条件的方案集合,设排列长度为S,则一共S个集合. 容斥原理的本质是考虑[集合交 或 集合交 ...
- DataFactory使用和注意,排列组合
DataFactory使用和注意 mysql 连接ODBC开放数据库连接(Open Database Connectivity,ODBC)驱动程序 生成数据:int不能用 Build a compos ...
- hdu1521 排列组合(指数型母函数)
题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数. (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...
- 排列 && 组合
最近编程经常遇到需要 排列&&组合(求子集) 的问题:遂整理一下. 1. 数字的排列与组合(递归):O(n!),O(nC(n,k)) * O(n) #include <stdio ...
- js 排列 组合 的一个简单例子
最近工作项目需要用到js排列组合,于是就写了一个简单的demo. 前几天在网上找到一个写全排列A(n,n)的code感觉还可以,于是贴出来了, 排列的实现方式: 全排列主要用到的是递归和数组的插入 比 ...
- HDU 1521 排列组合 指数型母函数
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- 【排列组合】ZSC1076: 数学、不容易系列之三——考新郎
国庆期间,省城刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每位新娘打扮得几乎一模一样,并 ...
随机推荐
- 和为S的两个数
题目 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 思考 注 ...
- 微服务下的契约测试(CDC)解读
1. 前言 有近两周没有在公众号中发表文章了,看过我之前公众号的读者都知道,公众号中近期在连载<RobotFramework接口自动化系列课程>,原本计划每周更新一篇,最近由于博主在带一个 ...
- asp.net应用发布到IIS无法链接到oracle数据库
遇到这个问题纠结了好久,试了好多的方法,其中我的问题是,先安装了.net frameword4然后又安装的IIS. 正确方式应该是先安装IIS 然后安装.net framework;且应用程序池没有启 ...
- JavaScript系列----函数(Function)篇(4)
1.什么是函数? 在W3C中函数的定义是这么说的:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 诚然,从这种抽象的定义中我们得不到什么有价值的东西.下面,举例来列举出函数的几种定义 ...
- CentOS系统中出现错误--SSH:connect to host centos-py port 22: Connection refused
我在第一次搭建自己的 hadoop2.2.0单节点的伪分布集成环境时遇到了此错误,通过思考问题和查找解决方案最终搞定了这个问题,其错误原因主要有以下几种: 1)SSH服务为安装 此时,采用在线安装的方 ...
- maven---settings.xml配置
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- C#.NET 用程序画图,曲线图
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...
- [转载] Linux五种IO模型
转载:http://blog.csdn.net/jay900323/article/details/18141217 Linux五种IO模型性能分析 目录(?)[-] 概念理解 Lin ...
- 前端面试题(5) 列举5种IE haslayout的属性及其值
haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在Internet Explorer中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父 ...
- Python之元类
类型对象负责创建对象实例,控制对象行为.那么类型对象又由谁来创建呢? 元类(metaclass)——类型的类型 New-Style Class的默认类型是type >>> class ...