1. 程式人生 > 實用技巧 >Project Euler 不定期更新

Project Euler 不定期更新

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;
}