PARTITION(number theory) ALSO Explosive number in codewars

问题出于codewars,简言之:寻找和为一个整数的的不同整数组合。https://en.wikipedia.org/wiki/Partition_(number_theory) 

例如:和为6的整数组合有如下11种,

6

5 + 1

4 + 2

4 + 1 + 1

3 + 3

3 + 2 + 1

3 + 1 + 1 + 1

2 + 2 + 2

2 + 2 + 1 + 1

2 + 1 + 1 + 1 + 1 + 1

1 + 1 + 1 + 1 + 1 + 1

解法一:

为了避免重复,我们可以选择逐渐递减第一个加数,直到不小于n的一半(保持分解的数字),然后对第一个加数应用这个策略,还是以6为例:

6

5 + 1

4 + 2

3 + 3

然后对第一个加数5应用这个策略,

4 + 1

3 + 2

接着对分解4和3应用这一策略,得到如下:

6

5 + 1

 4 + 1 + 1

  3 + 1 + 1 + 1

   2 + 1 + 1 + 1 + 1

    1 + 1 + 1 + 1 + 1 + 1

  2 + 2 + 1 + 1

 3 + 2 + 1

4 + 2

  2 + 2 + 2

3 + 3

因为我们已经有了3+2+1,对于4而言就不在分解成3+1+2,此处的限制条件分解4不能产生比2(4后面的2)小的数字,所以只能是2+2,同理,3就不再分解了,因为分解不出比3(后面那个)还大的数了。

 1 def exp_sum(n):
 2     if n < 0:
 3         return 0
 4     if n == 0:
 5         return 1
 6     return help(n, 1)
 7
 8 def help(n, mini):
 9     tmp = 1
10     if n <= 1:
11         return 1
12     for i in range(1, n/2+1):##保证n-i不小于i
13         if i >= mini: ##保证分解得到的数不小于后面的较小的数
14             tmp = tmp + help(n-i, i)
15     return tmp
16 print exp_sum(6)

当然了,这个递归的速度是很慢的,T(n) = T(n-1) + T(n-2) +...+ T(ceil(n/2)),时间复杂度是指数级别的。

解法二:

可以开辟一个大小为n的list存储已经计算的结果:

 1 def exp_sum(n):
 2     if n < 0:
 3         return 0
 4     if n == 0 or n == 1:
 5         return 1
 6     solution = [1] + [0]*n
 7
 8     for i in range(1, n):
 9         for j in range(i, n+1):
10             solution[j] += solution[j-i]
11     return solution[n]+1

解法三:

根据维基百科上的generation function:p(k) = p(k − 1) + p(k − 2) − p(k − 5) − p(k − 7) + p(k − 12) + p(k − 15) − p(k − 22) − ... + (-1)^(k+1)*p(k − (3k^2 - k)/2) + (-1)^(k+1)*p(k − (3k^2 + k)/2)

 1 def exp_sum(n):
 2     solutions = [1]*(n + 1)
 3
 4     for i in xrange(1, n + 1):
 5         j, k, s = 1, 1, 0
 6         while j > 0:
 7                 j = i - (3 * k * k + k) / 2
 8                 if j >= 0:
 9                         s += (-1) ** (k+1) * solutions[j]
10                 j = i - (3 * k * k - k) / 2
11                 if j >= 0:
12                         s += (-1) ** (k+1) * solutions[j]
13                 k += 1
14         solutions[i] = s
15
16     return solutions[n]

参考链接:

  https://en.wikipedia.org/wiki/Partition_(number_theory)

  http://stackoverflow.com/questions/438540/projecteuler-sum-combinations

                        2015-07-15 17:03:25

时间: 2024-08-28 02:51:10

PARTITION(number theory) ALSO Explosive number in codewars的相关文章

hdu-2685I won&#39;t tell you this is about number theory(数论)

题目链接: I won't tell you this is about number theory Problem Description To think of a beautiful problem description is so hard for me that let's just drop them off. :)Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S. Input The fi

Acdream 1114 Number theory 莫比乌斯反演

http://acdream.info/problem?pid=1114 题目大意,给你一个序列a,求出这个序列中互质数的有多少对.其中所有的整数的都小于等于222222. f(d) 为 gcd 恰好为 d 的数的对数, F(d) 为 gcd 为 d 的倍数的对数, μ(d) 表示莫比乌斯函数 F(d) = ∑ f(n) 其中( n % d == 0 ) 莫比乌斯反演一下就可以得到, f(d) = ∑ μ(n / d) * F(n) 其中( n % d == 0) 所以我们最后所要的答案就是 f

2013年北京师范大学新生程序设计竞赛网络赛--D. Number theory(模拟取余)

D. Number theory Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 34055 Font Size:  +   - 数学不仅是简单而且是美的.数学很有趣,但是数学中也有很多难题,比如哥德巴赫猜想.各种欧拉定理.拉格朗日中值定理.费马定理等.今天小若遇

hdu 2685 I won&#39;t tell you this is about number theory

#include<stdio.h>#include<string.h>#define LL __int64 LL mult_mod(LL a,LL b,LL c){    a%=c;    b%=c;    LL ret=0;    while(b)    {        if(b&1){ret+=a;ret%=c;}        a<<=1;        if(a>=c)a%=c;        b>>=1;    }    retur

Number Theory(数论-对数)

X - Number Theory Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. B

A1-2017级算法上机第一次练习赛 P ModricWang&#39;s Number Theory II

题目描述 ModricWang has found a list containing n numbers. He calls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1. ModricWang can perform two types of operations: Choose a number an

CodeForces 1325E - Ehab&#39;s REAL Number Theory Problem【质因子+】

题意: ??给定一个数组 \(a\) ,数组中任意一个元素的因子数不超过 \(7\) ,找出一个最短的子序列,满足该子序列之积为完全平方数.输出其长度. 数据范围:\(1≤n≤10^5,1≤a_i≤10^6\) 分析: ??首先,对于数组中的每个元素,如果其因子中包含有一个完全平方数,那么可以把该完全平方数除去,不影响最后的结果. ??然后,可以发现,当一个数的因子个数 \(\leq 7\) 时,其包含的质因子个数 \(\leq 2\).(如果有3个质因子,那么至少有 \(8\) 个因子)当我们

2017 ACM/ICPC Asia Regional Qingdao Online 1011 A Cubic number and A Cubic Number

A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A cubic number is the result of using a whole number in a mul

hdu 6216 A Cubic number and A Cubic Number【数学】

hdu 6216 A Cubic number and A Cubic Number 题意:判断一个素数是否是两个立方数之差,就是验差分. 题解:只有相邻两立方数之差才可能,,因为x^3-y^3=(x-y)(x^2+xy+y^2),看(x-y),就能很快想到不相邻的立方数之差是不可能是素数的:),,然后把y=x+1代入,得:p=3x^2+3x+1,所以可得判断条件为:①p-1必须能被3整除:②(p-1)/3必须能表示为两个相邻整数之积. 1 #include<iostream> 2 #incl