Project Euler 6-10题

Project Euler|欧拉计划 1~10 (python 3.6) 一,Project Euler. Problem 1: Multiples of 3 and 5 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 the multiples of 3 or 5 below 10 阅读详情

为什么感觉6-10题比1-5题暴力了好多,没什么好的可改进点-_-!!!

第6题

这里写图片描述
题目来源ProjectEuler

这个题求 (ni=1i)2(ni=1i2) ,是 O(1) 的。

int main(){
    long long ans=0,n=100;
    ans=n*(n+1)/2;
    ans*=ans;
    ans-=n*(n+1)*(2*n+1)/6;
    cout<<ans<<endl;
    return 0;
}

此外: ni=1i3=(ni=1i)2=n(n+1)/2


第7题

这里写图片描述
题目来源ProjectEuler
这个题是求第10001个素数。
关于小于N的素数的个数,有这么一个估计公式: π(N)Nln(N)
详情可参考维基百科
考虑到 lnxlnxlnx 所以第10001个素数大小大概为 10001ln(10001)132878 ,跟答案在一个数量级内。
而求素数算法中,有一个近似线性时间(在 O(n)O(nlogn) )内求出所有小于n的素数的算法,叫线性筛,也叫欧拉筛。

int prime[10000000],cnt;
int num[10000000];

int main(){
    cnt=0;int n=10000000-1;
    for(int i=2;i<=n;i++){
        if(num[i]==0)   prime[++cnt]=i;
        for (int j=1;j<=cnt&&i*prime[j]<=n;j++){
            num[i*prime[j]]=1;
        }
    }
    cout<<prime[10001]<<endl;
    return 0;
}

大致思路是这样的,我们从2-n依次选择一个数 i ,若i未被标记,我们将其加入到素数队列中。若 i 未被标记为合数,则将其加入到素数队列中去。然后对于i,我们将 i 的所有已知素数倍的小于n的结果标为合数。
因为每个合数 x 都可以表示为x=kp,kp,其中p是素数。(若 k<p ,则 k 一定有一个<p的素因子 p )。在上述代码中, x 会在当i==kprime[j]==p时,被标记。

复杂度 f(n) 计算方法为 O(n)<f(n)=ni=2min(π(i),ni)<ni=2ninlnn


第8题

这里写图片描述
题目来源ProjectEuler

这个题是给你一千个数字,求最大的连续13个数字的积。
分析一下可以发现,如果这13个数字中有一个0,那么这个结果肯定不能要。
那么我们的策略就显然了,我们先令ans=0,记录一下当前的product是连续cnt个非零数字的积。那么当cnt==13的时候,我们将product除掉第一个数字,乘上现在这个数字,得到一个新的product,ans更新为product和ans的最大值;当cnt<13的时候,我们将product直接乘上当前这个数字,并且cnt++。在结束当前循环前,判断product是否为0,若product==0,则将cnt归零。
复杂度 O(n+len)

char s[1050];

int main(){
    for (int i=0;i<20;i++){
        scanf("%s",s+i*50);
    }
    int len=13;long long ans=0,product=1,cnt=0;
    for(int i=0;i<1000;i++){
        if (cnt<len){
            product*=(s[i]-'0');
            cnt++;
        }
        else{
            product/=(s[i-len]-'0');
            product*=(s[i]-'0');
            ans=max(ans,product);
        }
        if (product==0){
            product=1;
            cnt=0;
        }
    }
    cout<<ans<<endl;
    return 0;
}



第9题

这里写图片描述
题目来源ProjectEuler

这个是求a,b,c,满足 a2+b2=c2 a+b+c=1000
枚举a,b,令c=1000-a-b,判断 a2+b2=c2 是否成立,成立则输出并退出程序。
因为 c>max(a,b) 所以a,b只需要枚举到500就可以了。
输出a,b,c可以看到 2002+3752=4252

int main(){
    for (int a=1;a<500;a++){
        for (int b=1;b<500;b++){
            int c=1000-a-b;
            if (a*a+b*b==c*c){
                printf("%d %d %d %d\n",a,b,c,a*b*c);
                return 0;
            }
        }
    }
}



第10题

这里写图片描述
题目来源ProjectEuler

这里是求所有小于2,000,000的素数的和,使用第7题里面的线性筛,求和即可。

int prime[10000000],cnt;
int num[10000000];

int main(){
    cnt=0;int n=10000000-1;
    for(int i=2;i<=n;i++){
        if(num[i]==0)   prime[++cnt]=i;
        for (int j=1;j<=cnt&&i*prime[j]<=n;j++){
            num[i*prime[j]]=1;
        }
    }
    long long ans=0;
    for (int i=1;i<=cnt;i++){
        if (prime[i]<2000000) ans+=prime[i];
        else break; 
    }
    cout<<ans<<endl;
    return 0;
}
关于PE458(project euler 458 Permutations of Project)的思考 本文回顾了PE458的解过程中遇到的问,介绍了trie,AC自动机,自动机化简算法. 阅读详情

相关推荐

25、压电泵致动器变形计算与测量及转弯腔射流特性的有限元分析

本文围绕压电泵致动器的变形计算与测量以及转弯腔射流特性的有限元分析展开研究。通过建立有限元模型和抛物线拟合方法,对压电泵致动器的位移和变形体积进行了详细计算,并探讨了其对传感器性能的影响。同时,利用ANSYS软件对转弯腔在不同角速率输入下的射流特性进行了仿真分析,揭示了流场分布随角速率变化的规律。研究成果为压电流体角速率传感器的设计优化提供了理论依据和技术支持。

ooo22的博客 107

ProjectEuler(欧拉计划)系列解目录【持续更新中】

ProjectEuler(欧拉计划)介绍 目来源:51Nod-ProjectEuler ProjectEuler 1(2171) ProjectEuler 2(2174) ProjectEuler 3(2176)

东瓜blog 1212

Android14 Qcom 从Framework到Driver带你连接WIFI(一)

前面已经讲完了WIFI打开的流程,那么通过UI这时候是可以选择需要连接的网络进行连接。从这里开始我们看连接的流程。

IAYLBL的博客 672

CODY Contest 2020 Project Euler I 全10

第一 Problem 230. 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 the multiples of 3 or 5 below the inp...

Type真是太帅了 2439

51Nod 2172 ProjectEuler 6 c/c++

[目描述[(http://www.51nod.com/Challenge/Problem.html#problemId=2172) 前10个正整数的平方和是 12+22+⋯+102=385 前10个正整数和的平方是 (1+2+⋯+10)2=3025 和的平方减去平方和是3025 - 385 = 2640。 输入n,求前n个正整数和的平方 减去 平方和。 输入 输入第一行组数T, 接下来T行,每...

东瓜blog 405

projecteuler第六

[code="java"]public class Task_6 { /** * 求(1+2+…+100)²-(1²+2²+22²+…+100²) * @param args */ public static void main(String[] args) { int n = 100; int sum1 = 0; int sum2 = 0; ...

菜鸟乐园 234

Project Euler6 第六

 //The sum of the squares of the first ten natural numbers is, //12 + 22 + ... + 102 = 385 //The square of the sum of the first ten natural numbers is, ...

weixin_34128839的博客 167

欧拉计划 Project Euler 68(魔力五边形环)

为了获得最大的 16 位数字串,10 必须是外侧节点之一。为了使起始数字(最小外侧节点)最大,外侧节点集合必须包含尽可能大的数字,同时包含 10。最大的这种集合是 {6, 7, 8, 9, 10},此时最小外侧节点为 6,内侧节点为 {1, 2, 3, 4, 5},每条线总和为 14。在此基础上,从外侧节点 6 开始,通过贪婪选择后续数字,可以构建出最大的 16 位数字串。在如下的“魔力”五边形环中,在其中填入1至1010个数,根据不同的填写方式,可以组成16位或17位数字串。

m0_53387935的博客 930

Project Euler

这个方阵不太方便直接弄到程序里,所以当做输入数据就可以了。会超时,但是实际上远远跑不满,所以直接枚举。直接枚举直接枚举直接枚举。怎么前面的都是无脑啊?拉插一下就可以得到目中所说的“最优多项式” 了。我写的是高精度,代码比较长,目很简,就不贴了。注意不一定第一个比第二个大,要取绝对值。直接写一个高精度,或者用 Python 即可。猜一波小的质数不会太大,于是直接枚举。那么可以使用 C++ 自带的(从。算出来,然后直接对着每个。最后可能是个大质数。函数将数转成字符串。

Eason_cyx的博客 2343

Project Euler 31-35

第31 目来源ProjectEuler这一求200可以由1,2,5,10,20,50,100,200以多少种不同的方式相加而成。 将硬币分别标记为f[1]=1;f[2]=2;f[3]=5;f[4]=10;f[5]=20;f[6]=50;f[7]=100];f[8]=200; 使用dp[i][j]dp[i][j]表示利用最大面额为f[j]的纸币组成i有多少种方法。 转移就是dp[i][j

PFCC的博客 1292

Project Euler 41-45

第41 目来源ProjectEuler求由1-n的数字的全排列构成的数中,最大的素数。 显然n小于等于九。而若n=9,则∑9i=1i=45\sum_{i=1}^9i=45,是9的倍数,所以n≤8n \leq 8。 我们利用prev_permutation()函数,求一个全排列的上一个全排列,而运行len!len!次后将会回到初始排列。我们枚举全排列的长度,由长至短依次检验,一旦成功

PFCC的博客 884

Project Euler: Problem 1 to 10

Project Euler: Problem 1 to 10...

因吉的博客 879

project euler 6~10

Problem 6 Sum square difference #include <iostream> #include <cstdio> using namespace std; int work(int n) { int t1 = 0, t2 = 0; for(int i = 1; i <= n; i++) { t1 += i...

jsszwc的博客 211

Project Euler 6~10

Problem 6: The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025

Charlie Pan's Blog 855

Project Euler - 6

欧拉项目 第6: problem :

Daker's Blog 517

Project Euler6

The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the ...

Lam Peter's blog 241

No.6 The difference between the sum of the squares and the square of the sum

Q: 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 natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 Hence ...

诗意的栖居 430

Project Euler Problem 6 (C++和Python代码实现和解析)

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 natural numbers is, (1 + 2 + … + 1...

jiafengfu的CSDN博客 324

Project Euler 6

''' Created on 2014年8月29日 Sum square difference The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2

u010646472的专栏 332

Project Euler Problem 6

public class E6 { public static void main(String[] args) { int soq=0,qos=0; for(int i=1;i100;i++) { soq+=i*i; qos+=i; } qos=qos*qos;

liuboman的博客 374

图像处理文献综述.doc

图像处理文献综述.doc

上一篇: Project Euler 1-5题
下一篇: Project Euler 11-15题
PFCC
博客等级 码龄9年 6粉丝 15原创
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值