Project Euler 不定期更新

官网传送门:https://projecteuler.net/

Problem 1

#include<cstdio>
#include<iostream>
using namespace std;
int ans;
int main()
{
    int i;
    for(i=1;i<=999;i++) 
        if(i%3==0||i%5==0)
            ans+=i;
    printf("%d",ans);
    return 0;
}

Problem 2

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL ML=4000000; // MAX LIMIT
LL ans;
LL f[ML];
int main()
{
    int i;
    int n;
    f[1]=1; f[2]=2;
    for(i=3;i<=10000;i++) {
        f[i]=f[i-1]+f[i-2];
        if(f[i]>ML) {
            n=i-1;
            break;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(~f[i]&1) 
            ans+=f[i];
    }
    cout<<ans;
    return 0;
}

Problem 3

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int main()
{
    LL i,n=600851475143;
    LL ans=0;
    for(i=2;i*i<=n;i++) {
        if(n%i==0) ans=max(ans,i);
        while(n%i==0) { n=n/i; }
    }
    if(n>0) ans=max(ans,n);
    cout<<ans;
    return 0;
}

Problem 4

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int main()
{
    int i,j,tmp,rev,ans=0;
    for(i=100;i<=999;i++) 
        for(j=100;j<=999;j++) {
            tmp=i*j;
            rev=0;
            while(tmp>0) 
            {
               rev=(rev<<1)+(rev<<3)+tmp%10;
               tmp=tmp/10;
            }
            if(rev==i*j)
                ans=max(ans,rev);
        }
    cout<<ans;
    return 0;
}

Problem 5

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
//==========================================
int main()
{
    LL ans=1;
    for(LL i=1;i<=20;i++)
        ans=lcm(i,ans);
    cout<<ans;
    return 0;
}

Problem 6

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
//==========================================
int main()
{
    LL ans1=0,ans2=0,ans;
    for(LL i=1;i<=100;i++) {
        ans1+=i*i;
        ans2+=i;
    }
    ans=ans2*ans2-ans1;
    cout<<ans;
    return 0;
}

Problem 7

#include<vector>
#include<cstdio>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
const int _N=1e6+5;
const int _LI=1e6;
bitset<_N> tag;
vector<int> pm;
void prime()
{
    pm.push_back(0);
    LL i,j;
    for(i=2;i<=_LI;i++) {
        if(tag[i]) continue;
        pm.push_back(i);
        for(j=i*i;j<=_LI;j+=i)
            tag[j]=1;
    }
    return;    
}
//==========================================
int main()
{
    prime();
    cout<<pm[10001];
    return 0;
}

Problem 8

#include<vector>
#include<cstdio>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
const int _N=1e6+5;
const int _LI=1e6;
bitset<_N> tag;
vector<int> pm;
void prime()
{
    pm.push_back(0);
    LL i,j;
    for(i=2;i<=_LI;i++) {
        if(tag[i]) continue;
        pm.push_back(i);
        for(j=i*i;j<=_LI;j+=i)
            tag[j]=1;
    }
    return;    
}
//==========================================
const int N=1000+5;
ULL a[N];
int n=1000;
int main()
{
    int i,j;
    char c;
    ULL tmp,ans=0;
    for(i=1;i<=n;i++) {
        for(c=getchar();c<'0'||c>'9';c=getchar());
        a[i]=c-'0';
    }
    for(i=1;i<=n-12;i++) {
        tmp=1;
        for(j=i;j<=i+12;j++) 
            tmp=tmp*a[j];
        ans=max(ans,tmp);
    }
    cout<<ans;
    return 0;
}

Input

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Output

23514624000

Problem 9

题意:输出a,b,c的积。

#include<vector>
#include<cstdio>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
const int _N=1e6+5;
const int _LI=1e6;
bitset<_N> tag;
vector<int> pm;
void prime()
{
    pm.push_back(0);
    LL i,j;
    for(i=2;i<=_LI;i++) {
        if(tag[i]) continue;
        pm.push_back(i);
        for(j=i*i;j<=_LI;j+=i)
            tag[j]=1;
    }
    return;    
}
//==========================================
const int N=1000+5;
ULL a[N];
int n=1000;
int main()
{
    int i,j;
    int a,b,c;
    for(a=1;a<=1000;a++)
        for(b=a+1;b<=1000&&1000-a-b>b;b++) 
            if(a*a+b*b==(1000-a-b)*(1000-a-b)) {
                cout<<a*b*(1000-a-b);
                return 0;
            }
    return 0;
}

Problem 10

#include<vector>
#include<cstdio>
#include<bitset>
#include<iostream>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
LL gcd(LL a,LL b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return a*b/gcd(a,b);
}
const int _N=1e7+5;
const int _LI=2e6;
bitset<_N> tag;
vector<int> pm;
void prime()
{
    pm.push_back(0);
    LL i,j;
    for(i=2;i<=_LI;i++) {
        if(tag[i]) continue;
        pm.push_back(i);
        for(j=i*i;j<=_LI;j+=i)
            tag[j]=1;
    }
    return;    
}
//==========================================
const LL ML=2000000;
int main()
{
    int i,j;
    prime();
    LL ans=0;
    for(i=1;i<pm.size();i++) {
        if(pm[i]>=ML)
            break;
        ans+=pm[i];
    }
    cout<<ans;
    return 0;
}

Problem 401 Sum of squares of divisors

传送门

The divisors of 6 are 1,2,3 and 6.
The sum of the squares of these numbers is 1+4+9+36=50.

Let sigma2(n) represent the sum of the squares of the divisors of n. Thus sigma2(6)=50.

Let SIGMA2 represent the summatory function of sigma2, that is SIGMA2(n)=∑ sigma2(i) for i=1 to n.
The first 6 values of SIGMA2 are: 1,6,16,37,63 and 113.

Find SIGMA2(10^15) modulo 10^9.

思路:

\[SIGMA2(n)=\sum_{i=1}^{n}\sum_{d|n}d^2 \]

\[=\sum_{d=1}^{n}d^2\sum_{i=1}^{n}[d|i] \]

\[=\sum_{d=1}^{n}d^2 \lfloor {n \over i } \rfloor \]

数论分块即可。

平方和公式:\(\sum_{i=1}^{n}i^2= {n \times (n-1) \times (2\times n+1) \over 6}\)

非常恶心的是 模数是\(10^9\) , 除以 \(6\) 还不能直接乘逆元了。

发现 \(n \times (n-1) \times (2\times n+1)\) 一定是 \(6\) 的倍数,所以分类讨论一下,把 \(6\) 分成 \(3 \times 2\) 分别整除了再计算,注意每一步都要取模,>1e9 的数要单独先取模。

Code:

#include<vector>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>

using namespace std;
typedef long long LL;

const LL MOD=1e9;

LL mul_div(LL x,LL y,LL z,LL k1,LL k2)
{
	if(x%k1==0) x/=k1;
	else if(y%k1==0) y/=k1;
	else if(z%k1==0) z/=k1;
	
	if(x%k2==0) x/=k2;
	else if(y%k2==0) y/=k2;
	else if(z%k2==0) z/=k2;
	
	x%=MOD; y%=MOD; z%=MOD;
	return x*y%MOD*z%MOD;
}
LL sum(LL x) { return mul_div(x,x+1,2*x+1,2,3); }

int main()
{
    LL i,j,t,ans=0;
    LL n=1e15;
    for(i=1;i<=n;i=j+1) {
        t=n/i; j=n/t;
        ans=(ans+(sum(j)-sum(i-1)+MOD)%MOD*(t%MOD)%MOD)%MOD;
    }
    cout<<ans<<endl;
    return 0;
}

Answer: 281632621

posted @ 2020-08-22 12:40  cjlworld  阅读(214)  评论(0编辑  收藏  举报