题意: 考虑所有满足2 ≤ a ≤ 5和2 ≤ b ≤ 5的整数组合生成的幂ab: 22=4, 23=8, 24=16, 25=3232=9, 33=27, 34=81, 35=24342=16, 43=64, 44=256, 45=102452=25, 53=125, 54=625, 55=3125 如果把这些幂按照大小排列并去重,我们得到以下由15个不同的项组成的序列: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 31…
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<iostream> #include<algorithm> using namespace std; //**************************************************************** // Miller_Rabin 算法进…
题目大意: $2<=a,b<=n$ 求 $a^b$能表示多少不同的正整数. 原题中n=100,可以直接暴力求解,常见的两种解法是写高精度或者取对数判断相等. 直觉告诉我应该有更加优秀的解法,于是翻了下discuss,找到了一种复杂度介于O(n)和O(nlognlogn)的解法,拿出来分享一下. 首先来看一个性质: 对于一个$a$,可以找到最小的$a_0$,使得$a=a_0^k$. 比如$8^4=4^6=2^{12}$ 都是等价的. 对于某个$a^b = (a_0^k)^{b}$, 它只可能和某…
题意: 记d(n)为n的所有真因数(小于n且整除n的正整数)之和. 如果d(a) = b且d(b) = a,且a ≠ b,那么a和b构成一个亲和数对,a和b被称为亲和数. 例如,220的真因数包括1.2.4.5.10.11.20.22.44.55和100,因此d(220) = 284:而284的真因数包括1.2.4.71和142,因此d(284) = 220. 求所有小于10000的亲和数的和. 整数因子和: <font color = red , size = 5 >以下图片仅供学习!图片来…
题意: 完全数是指真因数之和等于自身的那些数.例如,28的真因数之和为1 + 2 + 4 + 7 + 14 = 28,因此28是一个完全数. 一个数n被称为亏数,如果它的真因数之和小于n:反之则被称为盈数. 由于12是最小的盈数,它的真因数之和为1 + 2 + 3 + 4 + 6 = 16,所以最小的能够表示成两个盈数之和的数是24.通过数学分析可以得出,所有大于28123的数都可以被写成两个盈数的和:尽管我们知道最大的不能被写成两个盈数的和的数要小于这个值,但这是通过分析所能得到的最好上界.…
题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 × 43646 = 2 × 17 × 19 首次出现连续四个数均有四个不同的质因数时,其中的第一个数是多少? 方法一:普通筛法 /************************************************************************* > File Name…
题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <complex> #include <cmath> #i…
题意: 项的自幂级数求和为 11 + 22 + 33 + - + 1010 = 10405071317. 求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + - + 10001000. 思路: 求最后十位数字 % 1010 即可. 对于快速幂中数据溢出的问题,有两种解决方法: 1. 方法一:对于两个数 x y,现在想求 x * y % MOD,可以将 x 表示成 a * DIGS + b,y 表示成 c * DIGS + d,x * y % MOD 则等价与 ( a * c…
本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime factor # The prime factors of 13195 are 5, 7, 13 and 29. # What is the largest prime factor of the number 600851475143 ? # Answer: 6857 a = 6008514751…
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical order, with any repeats removed, we get the f…
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <math.h> #include <stdlib.h> #include<time.h> #define ll long long #define INF 0x3f3f3f3f #define ma…
本题来自 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…
啊居然要特判,卡了好久QAQ (好像Windows下的rand和Linux下的不一样? QwQ一些东西参考了喵铃的这篇blog:http://www.cnblogs.com/meowww/p/6400841.html  (业界良心) 题目 题意:输入$n$,求$phi(n)$,$n \leq 10^{18}$ 随便抽的题,刚好学习一下相关的算法. 很明显朴素的根号算法时间复杂度补滋兹,线性筛更不用想了,不过这题只需要单个欧拉函数值,还是直接考虑$phi(n)=n*\prod_{i=1}^k(1-…
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出这道题的人) program 4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.…
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <string.h> #define MAXN 30 #define MAXString 64 int Terms[MAXN]; int N; int Count; void Search(int Remainder,int Start,int nTerm) { int i; ){ Count++; !=) pri…
开始做 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…
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范围比较小) 无论素数判定还是因子分解,试除法(Trial Division)都是首先要进行的步骤.令m=n,从2~根n一一枚举,如果当前数能够整除m,那么当前数就是n的素数因子,并用整数m 将当前数除尽为止. 若循环结束后m是大于1的整数,那么此时m也是n的素数因子. 事例如HDU1164:15mm…
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平台打造一个有趣和休闲 的环境. 项目主页:https://projecteuler.net 第一题 Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we ge…
手写求大整数开根号所得到的值,具体计算过程参考别人的资料,最后利用java的大整数得到答案 别人博客链接:http://www.cnblogs.com/Rinyo/archive/2012/12/16/2820450.html 1.举例 上式意为65536的开平方为256.手开方过程类似于除法计算.为了方便表述,以下仍称类似位置的数为“被除数”.“除数”.“商”. 以65536为例,其具体计算过程如下: Step1:将被开方数(为了形象,表述成“被除数”,此例中即为65536)从个位往高位每两位…
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要把询问范围加到 \(10^{18}\) ,再多组询问呢? Miller 和 Rabin 建立了Miller-Rabin 质数测试算法. \(\\\) Fermat 测试 首先我们知道费马小定理: \[ a^{p-1}\equiv 1\pmod p \] 当且仅当 \(p\) 为素数时成立. 逆命题是…
题意:f(1)="a",f(2)="b",f(i)=f(i-1)+f(i-2),"+"表示连接符.给定n,m,求f(n)的前m个字符的“next值”. 思路:并不知道如何一步步推出结论,只能打个表找找规律了:找到最小的i使得f(i)>m+1,则答案就是m-f(i-2).然后就是大整数模板了 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2…
本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice paths Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right…
本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule…
本题来自 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 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythagorean triplet # A Pythagorean triplet is a set of three natural numbers, # a < b < c, for which, a**2 + b**2 = c**2 # For example, 3**2 + 4**2 = 9 +…
Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需要更大数据的问题来说,C++的基本数据类型就不够用了.因此大整数类应运而生. 大整数类实现的基本思路:使用数组来储存数字的每一位,然后用类似于手写"+-×÷"竖式的过程来实现大整数类的加减乘除运算.注意每一个数在数组中都是高位在后,低位在前这样便于进行运算. 代码如下: 详细的东西都在注释…
一,题意: 大整数乘法模板题二,思路: 1,模拟乘法(注意"逢十进一") 2,倒序输出(注意首位0不输出) 三,步骤: 如:555 x 35 = 19425  5 5 5  5 5 5 x   3 5 x    3 5 -----------   ==>    ----------   2 7 7 5 25 25 25    + 1 6 6 5   +15 15 15 -------------  -----------------    1 9 4 2 5 15 40 40 2…
13:大整数的因子 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入 一个非负整数c,c的位数<=30. 输出 若存在满足 c%k == 0 的k,从小到大输出所有这样的k,相邻两个数之间用单个空格隔开:若没有这样的k,则输出"none". 样例输入 30 样例输出 2 3 5 6 思路: 模拟: 来,上代码: #include<cstdio&g…
11:大整数减法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个大的正整数相减的差. 输入 共2行,第1行是被减数a,第2行是减数b(a > b).每个大整数不超过200位,不会有多余的前导零. 输出 一行,即所求的差. 样例输入 9999999999999999999999999999999999999 9999999999999 样例输出 9999999999999999999999990000000000000 思路: 模拟: 来,上代码: #include<s…
10:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 一行,即相加后的结果.结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342. 样例输入 22222222222222222222 33333333333333333333 样例输出 55555555555555555555 来源 程序设计实习2007 思路: 模拟: 来,上代码:…