In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentagonal number, I can only wonder if we have to deal with septagonal numbers in Problem 46. Anyway the problem reads Pentagonal numbers are generated by t…
基础的动态规划...数塔取数问题. 状态转移方程: dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]);…
直接python搞过.没啥好办法.看了下别人做的,多数也是大数乘法搞过. 如果用大数做的话,c++写的话,fft优化大数乘法,然后快速幂一下就好了.…
Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. The code resemble : import math limit = 2000000 crosslimit = int(math.sqrt(limit)) #sieve = [False] * limit sieve =…
题意须要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.而且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下即可. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i…
ORZ foreverlasting聚聚,QQ上问了他好久才会了这题(所以我们又聊了好久的Gal) 我们先来尝试推导一下\(S\)的性质,我们利用狄利克雷卷积来推: \[2^\omega=I\ast|\mu|\] 这个很好理解吧,考虑一下它的组合意义即可 然后两边同卷上\(I\)有: \[2^\omega \ast I=I\ast I\ast |\mu|=d\ast |\mu|\] 后面还是同样,考虑\(d\ast |\mu|\)的组合意义,一正一反的情况下其实就是\(d(n^2)\) 因此我们…
1.If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 100 (计算1000以内能被3或5整除的数之和) #include <iostream> using namespace std; i…
题目的大意很简单 做法的话. 我们就枚举1~10000的数的立方, 然后把这个立方中的有序重新排列,生成一个字符串s, 然后对于那些符合题目要求的肯定是生成同一个字符串的. 然后就可以用map来搞了 这里偷懒用了python import string dic = {} def getstr(n): sn = str(n) arr = [] for c in sn: arr.append(c) arr.sort() return ''.join(arr) for i in xrange(1, 1…
这题略水啊 首先观察一下. 10 ^ x次方肯定是x + 1位的 所以底数肯定小于10的 那么我们就枚举1~9为底数 然后枚举幂级数就行了,直至不满足题目中的条件即可break cnt = 0 for i in range(1, 10): e = 1 while True: if len(str(i**e)) != e: break e += 1 cnt += 1 print cnt…
题意很明了. 然后我大概的做法就是暴搜了 先把每个几边形数中四位数的处理出来. 然后我就DFS回溯着找就行了. 比较简单吧. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <vector> using namespace std; vector<int>g[7],…
看样子,51nod 1035 最长的循环节 这道题应该是从pe搬过去的. 详解见论文的(二)那部分:http://web.math.sinica.edu.tw/math_media/d253/25311.pdf…
全排列的生成,c++的next_permutation是O(n)生成全排列的.具体的O(n)生成全排列的算法,在 布鲁迪 的那本组合数学中有讲解(课本之外,我就看过这一本组合数学),冯速老师翻译的,具体在哪个地方讲的忘记了..…
直接暴力搞就行,优化的地方应该还是计算因子和那里,优化方法在这里:http://www.cnblogs.com/guoyongheng/p/7780345.html 这题真坑,能被写成两个相同盈数之和的数字也算能被写成两个盈数之和,题目描述不清楚...…
先说最暴力的算法,直接对一万内的每个数字暴力分解因子(对每个数字的时间复杂度是O(sqrt(n)的),然后,用个数组记录下来因子和,然后寻找 亲密数. 好一点:要先打个素数表,然后对每个数字,分解素因子, 假设因子和函数为,则, 推导后: ------>证明过程见<初等数论及其应用>(原书第六版)184-185页 有个坑就是a!=b,在这里错了好几发.... 看了下官方的题解,最终用的方法就是我图片里的那个公式. ------------->开启了支持数学公式,可是还是不支持,很蛋…
组合数,2n中选n个.向右走有n步,向下走有n步.共2n步.有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n). 或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i][j] = step[i-1][j]+step[i][j-1],递推即可. #include <iostream> using namespace std; typedef long long ll; ll C(ll n, ll m) { ll res = 1; for(ll i = 1; i &…
记忆化搜索来一发.没想到中间会爆int #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int dp[MAXN]; int dfs(long long num) { if(num <= MAXN && dp[num]) return dp[num]; if(num == 1) return (dp[num] = 1); if(num&1) { if(num <=…
最直接的想法就是暴力搞搞,直接枚举,暴力分解因子.再好一点,就打个素数表来分解因子.假设num=p1^a1p2^a2...pn^an,则所有因子个数为(a1+1)(a2+1)...(an+1). 再好一点呢,还是要利用积性函数.假设因子个数函数为f(x),因为num=n(n+1)/2,所以num2=n(n+1),因为n和n+1为相邻的正整数,所以互质,所以f(2num)=f(n)f(n+1), 则f(num)=f(n)f((n+1)/2)(n+1为偶数)或者f(num)=f(n/2)*f(n+1…
还是素数线性筛 MAXN = 2000000 prime = [0 for i in range(MAXN+1)] res = 0 for i in range(2,MAXN+1): if prime[i] == 0: res += i prime[0] += 1 prime[prime[0]] = i j = 1 while j <= prime[0] and prime[j]<=MAXN/i: prime[prime[j]*i] = 1 if i%prime[j] == 0: break…
我是俩循环暴力 看了看给的文档,英语并不好,有点懵,所以找了个中文的博客看了看:勾股数组学习小记.里面有两个学习链接和例题. import math def calc(): for i in range(1,1000): j = i+1 while j <= 1000: c = i*i+j*j c = int(math.sqrt(c)) if i+j+c > 1000: break if i < j < c and i+j+c == 1000 and i*i+j*j == c*c:…
直接暴力 写完才想到,代码写矬了,扫一遍就出结果了,哪还用我写的这么麻烦 ss = '''73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 6…
素数线性筛 MAXN = 110100 prime = [0 for i in range(210000)] for i in range(2,MAXN): if prime[i] == 0: prime[0] += 1 prime[prime[0]] = i j = 1 while j <= prime[0] and prime[j]<=MAXN/i: prime[prime[j]*i] = 1 if i%prime[j] == 0: break j += 1 print prime[100…
对每个数字分解素因子,最后对每个素因子去其最大的指数,然后把不同素因子的最大指数次幂相乘,得到的就是最小公倍数 python不熟练,代码比较挫 mp = {} def process(n): i = 2 while i*i <= n: cnt = 0 if n%i == 0: while n%i == 0: cnt += 1 n /= i if i in mp: mp[i] = max(mp[i], cnt) else: mp[i] = cnt i += 1 if n > 1: if n in…
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we list all the natural numbers below 10 # that are multiples of 3 or 5, we get 3, 5, 6 and 9. # The sum of these multiples is 23. # Find the sum of all…
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b…
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n − 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i…
本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. # Answer: 5537376230 numbers = '''371072875339021027987979982…
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divisible triangular number # The sequence of triangle numbers is generated by adding the natural numbers. # So the 7th triangle number would be 1 + 2 + 3…
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest product in a grid # In the 20×20 grid below, four numbers along a diagonal line have been marked in red. # The product of these numbers is 26 × 63 × 78 ×…
本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation of primes # The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. # Answer: 142913828922 def f(x):…
本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur…