Project Euler 051~053

关于啥是Project Euler 详见 https://projecteuler.net/about
观前声明:
这是个人兴趣使然的对自己入坑pe的记录,仅提供思路和部分代码;各个方面肯定都是有着优化与提升空间的,甚至在许多大佬看来这应该是初学者的浅薄而未经剪枝的丑码,如果能帮到有兴趣的人自是最好,也欢迎能醍醐灌顶的深度讨论。
大佬看到了笑笑就行,还请轻喷。
带着恶意,抬杠者...俺也喷不过您,也不能拿您咋样...毕竟这只是我个人兴趣使然的行为或者说是学习记录分享。 (说是记录,但因为是早先写的所以其实是在各种意义上公开处刑和吐槽自己 并尝试补救优化)
语言是c++,用的VS平台

Prime digit replacements
Problem 51
By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
对于一个2位数*3,*从1取到9,共有6个质数:13, 23, 43, 53, 73, 83;
对于一个5位数56**3,**从00取到99,共有7个质数,且56**3是第一个使之具有7个质数的例子。这个质数族的首位为56003
找到这样的质数族中的第一个数,且这个质数族有8个成员,且第一个质数是满足上述的最小的质数。
首先,由于必须要构成8个素数,所以考虑到十进制下3的倍数是各位数字和为3的倍数的数,那么如果*的个数不是3的倍数个,则构成的10个数中必然会存在至少1个数是3的倍数,因为这10个数构成了等差数列(公差为一堆0与1的顺序组合,有*的个数个1,注意*不一定要连续),而在一个公差不为3的倍数而长度为10的等差数列中,mod 3=0的数至少有3个,那么我们就无法选取8个素数了。所以答案中一定存在至少3个相同的数字(或3的倍数个)作为可替换位,其次,答案中在可替换位上的数字,而答案数字一定是一组数中最小的那个,所以这只能是0或者1或者2,因为3456789只有七个数。
对于一个质数,因为我们只需得到质数族的首位,所以其一定有3的倍数个0,1或2,所以先跑一个质数表,再用一个函数获得其每个数字的出现次数:

下面就是运气时间,盲猜*的个数只有3个,那么写一个检验函数:
int check(int n, int dight, int co)//检验n为质数时dight出现次数为co的时候n是否符合条件;(dight为0,1或2)设定一组a,b,c代表3个*的位置,暴搜所有可能的选取,每选定一组就变换为质数族中下一个可能的数,若能变换出8个质数则满足所求。主函数中从第一个质数开始跑起保证这是最小的质数。详细代码如下:


盲猜成功:*2*3*3
这是第一个难度为15%的题. 文字解释代码起来挺费劲的就懒得详写了...()
ans:121313

Permuted multiples
Problem 52
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
125874与其2倍包含相同的数字,只是顺序不一致。找到最小的数x,使其满足2x, 3x, 4x, 5x, 和6x都包含相同的数字。
水题,对于一个数判断就完事.用数组储存后,因为要检验x的倍数,所以直接使用大整数的处理方法:

之后我惊讶的发现,例子就是答案:
1/7 = 0.142857 142857 142857 ...
2 * 142857 = 285714
3 * 142857 = 428571
4 * 142857 = 571428
5 * 142857 = 714285
6 * 142857 = 857142
but
7 * 142857 = 999999
ans:142857

Combinatoric selections
Problem 53
There are exactly ten ways of selecting three from five, 12345:
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
In combinatorics, we use the notation, C(5,3)=10.
In general, C(n,r)=n!/r!(n-r)!, where r≤n,
n!=n×(n-1)×...×3×2×1, and 0!=1.
It is not until n=23, that a value exceeds one-million: C(23,10)=1144066.
How many, not necessarily distinct, values of C(n,r) for 1≤n≤100, are greater than one-million?
初中排列组合题,建议手算...
代码实现C(n,r)的话,是个人都知道不能把阶乘都算出来因为很容易溢出(如果你用的是MMA当我没说),没有好好钻研过优化,不过还是可以动点小聪明,比如因为C(n,r)一定是个整数,所以分子和分母的阶乘组成中肯定有能约去的公因子,同余定理写个求最大公因子的函数即可。
最大公因数函数:(同余定理自行百度)
int f(int a, int b)
{
return b ? f(b, a % b) : a;
}


ans:4075

苟过期末了. 但咕咕咕真是香a 最近不定期更新吧